
Xamarin.Forms Button
Button标签
就是按钮标签。下面将列出常用属性:
属性与事件 | 描述 |
---|---|
Text | 获取或设置显示为按钮内容的文本。 |
BackgroundColor | 获取或设置将填充 VisualElement 背景的颜色。 |
TextColor | 获取或设置按钮文本的颜色。 |
FontSize | 获取或设置按钮文本的字体大小。 |
FontAttributes | 获取用于指示按钮文本的字体是粗体、斜体还是两者皆否的值。 |
Command | 获取或设置激活按钮时要调用的命令。 |
CommandParameter | 获取或设置要传递给 Command 属性的参数。 |
BorderColor | 获取或设置描述按钮边框笔划颜色的颜色。 |
BorderWidth | 获取或设置边框的宽度。 |
CornerRadius | 获取或设置按钮的圆角半径(以与设备无关的单位为单位)。 |
ClassId | 获取或设置用于标识语义相似元素集合的值。 |
Clicked | 单击 Button 时发生。 |
Demo
创建一个新的ViewPage取名为Buttons.xaml
,并添加相关代码。
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mycustomcontrl="clr-namespace:LableApp.MyCustomContrl"
x:Class="LableApp.Buttons"
BackgroundColor="#112B47">
<ContentPage.Content>
<StackLayout Margin="20,35,20,20">
<Button Text="Save" Clicked="Button_Clicked" BackgroundColor="#F11111" TextColor="#FFFFFF" FontSize="Large" FontAttributes="Bold" />
<Label x:Name="lblMessage" FontSize="Large" TextColor="White"></Label>
<Button
Text="Click Command Button"
BackgroundColor="Yellow"
Command="{Binding ButtonCommand}"
CommandParameter="Tnblog"
></Button>
<Button
Text="Click Command Button"
BackgroundColor="Green"
Command="{Binding ButtonCommand}"
CommandParameter="Xamarin Forms"
></Button>
<Button
VerticalOptions="Center"
BackgroundColor="Transparent"
BorderColor="ForestGreen"
BorderWidth="1.5"
CornerRadius="50"
TextColor="White"
FontAttributes="Bold"
FontSize="Large"
Text="Save"
ClassId="mybutton"
Clicked="Button_Clicked_1"
></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
在后台添加相关事件与命令。
public partial class Buttons : ContentPage
{
public Buttons ()
{
InitializeComponent ();
BindingContext = this;
}
public ICommand ButtonCommand => new Command<string>(ButtonCommandClick);
void Button_Clicked(System.Object sender, System.EventArgs e)
{
lblMessage.Text = "Save Success";
}
void ButtonCommandClick(string str)
{
lblMessage.Text = str;
}
void Button_Clicked_1(System.Object sender, System.EventArgs e)
{
DisplayAlert("Alert", "You have been alerted", "OK");
}
}
接着我们在安卓中,运行测试。我们可以看到,它前面几个按钮通过点击会去改变Label绑定了lblMessage
变量的值。最后一个Save将会通过DisplayAlert
弹出一个对话框。
接着我们在IOS中进行测试。
我们发现它在IOS中,Save按钮的样式不符合我们的理想,所以需要重写它。我们在它的以前的代码中,对该按钮添加了ClassId
值为mybutton
,所以我们在重写时,通过ClassId
来找到该组件并将它的CornerRadius
改为20
。
在LabelApp.IOS下创建新的MyButtonRenderer
类来重写,代码如下:
using LableApp.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace LableApp.iOS
{
public class MyButtonRenderer:ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
if (e.NewElement.ClassId == "mybutton")
{
Control.Layer.CornerRadius = 20;
}
}
}
}
}
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术
是伍尚金哇_v
秀儿
瑾语
好看哎