tnblog
首页
视频
资源
登录

Xamarin.Forms Button

5237人阅读 2022/4/29 17:18 总访问:3467501 评论:0 收藏:0 手机
分类: Xamarin

Xamarin.Forms Button

Button标签


就是按钮标签。下面将列出常用属性:

属性与事件 描述
Text 获取或设置显示为按钮内容的文本。
BackgroundColor 获取或设置将填充 VisualElement 背景的颜色。
TextColor 获取或设置按钮文本的颜色。
FontSize 获取或设置按钮文本的字体大小。
FontAttributes 获取用于指示按钮文本的字体是粗体、斜体还是两者皆否的值。
Command 获取或设置激活按钮时要调用的命令。
CommandParameter 获取或设置要传递给 Command 属性的参数。
BorderColor 获取或设置描述按钮边框笔划颜色的颜色。
BorderWidth 获取或设置边框的宽度。
CornerRadius 获取或设置按钮的圆角半径(以与设备无关的单位为单位)。
ClassId 获取或设置用于标识语义相似元素集合的值。
Clicked 单击 Button 时发生。

Demo


创建一个新的ViewPage取名为Buttons.xaml,并添加相关代码。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ContentPage
  3. xmlns="http://xamarin.com/schemas/2014/forms"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mycustomcontrl="clr-namespace:LableApp.MyCustomContrl"
  5. x:Class="LableApp.Buttons"
  6. BackgroundColor="#112B47">
  7. <ContentPage.Content>
  8. <StackLayout Margin="20,35,20,20">
  9. <Button Text="Save" Clicked="Button_Clicked" BackgroundColor="#F11111" TextColor="#FFFFFF" FontSize="Large" FontAttributes="Bold" />
  10. <Label x:Name="lblMessage" FontSize="Large" TextColor="White"></Label>
  11. <Button
  12. Text="Click Command Button"
  13. BackgroundColor="Yellow"
  14. Command="{Binding ButtonCommand}"
  15. CommandParameter="Tnblog"
  16. ></Button>
  17. <Button
  18. Text="Click Command Button"
  19. BackgroundColor="Green"
  20. Command="{Binding ButtonCommand}"
  21. CommandParameter="Xamarin Forms"
  22. ></Button>
  23. <Button
  24. VerticalOptions="Center"
  25. BackgroundColor="Transparent"
  26. BorderColor="ForestGreen"
  27. BorderWidth="1.5"
  28. CornerRadius="50"
  29. TextColor="White"
  30. FontAttributes="Bold"
  31. FontSize="Large"
  32. Text="Save"
  33. ClassId="mybutton"
  34. Clicked="Button_Clicked_1"
  35. ></Button>
  36. </StackLayout>
  37. </ContentPage.Content>
  38. </ContentPage>


在后台添加相关事件与命令。

  1. public partial class Buttons : ContentPage
  2. {
  3. public Buttons ()
  4. {
  5. InitializeComponent ();
  6. BindingContext = this;
  7. }
  8. public ICommand ButtonCommand => new Command<string>(ButtonCommandClick);
  9. void Button_Clicked(System.Object sender, System.EventArgs e)
  10. {
  11. lblMessage.Text = "Save Success";
  12. }
  13. void ButtonCommandClick(string str)
  14. {
  15. lblMessage.Text = str;
  16. }
  17. void Button_Clicked_1(System.Object sender, System.EventArgs e)
  18. {
  19. DisplayAlert("Alert", "You have been alerted", "OK");
  20. }
  21. }


接着我们在安卓中,运行测试。我们可以看到,它前面几个按钮通过点击会去改变Label绑定了lblMessage变量的值。最后一个Save将会通过DisplayAlert弹出一个对话框。


接着我们在IOS中进行测试。


我们发现它在IOS中,Save按钮的样式不符合我们的理想,所以需要重写它。我们在它的以前的代码中,对该按钮添加了ClassId值为mybutton,所以我们在重写时,通过ClassId来找到该组件并将它的CornerRadius改为20
在LabelApp.IOS下创建新的MyButtonRenderer类来重写,代码如下:

  1. using LableApp.iOS;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.Platform.iOS;
  4. [assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
  5. namespace LableApp.iOS
  6. {
  7. public class MyButtonRenderer:ButtonRenderer
  8. {
  9. protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
  10. {
  11. base.OnElementChanged(e);
  12. if (Control != null)
  13. {
  14. if (e.NewElement.ClassId == "mybutton")
  15. {
  16. Control.Layer.CornerRadius = 20;
  17. }
  18. }
  19. }
  20. }
  21. }


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

是伍尚金哇_v

2020/7/8 16:28:43

秀儿

剑轩:@是伍尚金哇_v这个拿来整个啥音乐播放还可以

2020/7/8 19:39:59 回复

瑾语

2020/7/10 11:06:41

好看哎

这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术