博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Win10 UWP开发系列:开发一个自定义控件——带数字徽章的AppBarButton
阅读量:4569 次
发布时间:2019-06-08

本文共 4620 字,大约阅读时间需要 15 分钟。

原文:

最近有个项目有一个这样的需求,在文章浏览页底部有几个AppBarButton,其中有一个是评论按钮,需要在评论按钮上显示一个红色数字,类似微信的新消息提醒:

这种设计在iOS和Android平台都是很常见的,但UWP上并没有提供现成的控件。所以只能自己实现一个了。

 

做出来效果是这样的:

 

分析一下实现的思路。首先这还是一个AppBarButton,只是其中增加了一个数字徽章的显示,这个属性应该是可以绑定到其他属性的,如果绑定的值不为0,则显示数字,如果为0则隐藏数字。因此我们可以通过继承AppBarButton,并修改其模板来实现这个控件。

 

下面动手实践一下吧。我已经安装了最新的VS2017,不过使用VS2015的话也是差不多的。首先创建一个名为AppBarBadgeButtonSample的UWP项目:

 

选择在Blend中打开:

在页面上添加一个AppBar:

添加后是这样的结构:

 

然后需要将默认AppBarButton的模板导出来,给我们自定义的控件用。在按钮上右键选择编辑模板,编辑副本:

 

输入一个名字,为了简单起见我们就在当前文件中创建这个样式好了,点击确定。

切换到XAML模式,可以看到页面的XAML代码中添加了这样的资源:

这个就是AppBarButton的控件模板了。我们需要在这个默认模板的基础上,实现自定义的AppBarBadgeButton。

现在返回到VS2017,在当前项目中添加一个名为Controls的文件夹,再添加一个名为AppBarBadgeButton的文件夹,然后添加一个模板控件:

确定后,可以看到项目中添加了两个文件,一个是cs文件,一个是一个xaml文件:

这个Generic.xaml即控件的模板,但是如果每个控件的模板都写在这里的话,会显得比较乱。最好是每个控件的cs文件和xaml文件都放在一块。所以在AppBarBadgeButton目录下添加一个名为AppBarBadgeButton.xaml的样式文件,这样方便管理。同时把控件的命名空间从AppBarBadgeButtonSample.Controls.AppBarBadgeButton改为AppBarBadgeButtonSample.Controls。

现在可以把之前生成的默认的AppBarButton的模板复制过来了:

注意,要把key删掉,TargetType都要改成AppBarBadgeButton:

现在需要把这个模板导入到Generic.xaml中,把Generic.xaml中自动生成的删掉,改为下面这样:

 

这样所有的控件的模板都跟自己的cs文件放在一块,Generic.xaml文件中只放引用位置,项目结构就清晰多了。

 

现在回到AppBarBadgeButton.cs,把控件的基类从Control改成AppBarButton。这样控件的所有行为都和默认的AppBarButton一致了。

我们需要一个数字属性,和一个控制数字是否可见的属性。添加以下代码:

public string Count{    get { return (string)GetValue(CountProperty); }    set { SetValue(CountProperty, value); }}// Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...public static readonly DependencyProperty CountProperty =    DependencyProperty.Register("Count", typeof(string), typeof(AppBarBadgeButton), new PropertyMetadata("0", null));

 

这是一个依赖属性。注意为什么数字的类型是string呢?为什么不设置成int?

卖个关子,先做完再说。

现在可以修改控件的模板了。仔细分析模板就能找到需要改的地方,有一个名为ContentRoot的Grid,应该就是这里了,在名为Content的ContentPresenter后面添加一个Border和一个TextBlock来显示数字:

 

注意,这里要使用TemplateBinding将文本绑定到Count属性。

现在来试试。我们设置Page的x:Name为rootPage,把刚才在主页面添加的AppBarButton改成自定义的AppBarBadgeButton,不要忘了引入命名空间:xmlns:localControls="using:AppBarBadgeButtonSample.Controls"

再设置Page的DataContext,并添加一个Count属性,把AppBarBadgeButton的Count绑定到cs文件的Count属性上:

xaml:

 

cs:

public sealed partial class MainPage : Page{    public MainPage()    {        this.InitializeComponent();        this.Count = 10;        this.rootPage.DataContext = this;    }    public int Count { get; set; }}

 

我这里没有用标准的MVVM,只是简单演示一下。现在运行下看看:

效果出来了。

现在思考刚才提到的问题,为什么在DataContext里的属性可以设置为int,而控件本身的依赖属性必须设置为string呢?如果有兴趣可以将依赖属性改为int试试,结果就是按钮上什么也显示不出来:

 

这是因为,在使用TemplateBinding的时候,必须保证source属性和target属性的值的类型是匹配的,TextBlock的Text属性是string,那么用于绑定的属性的类型也必须是string。在使用TemplateBinding时,没有机会进行自动的类型转换。如果类型不匹配,则无法绑定。可参考:

现在可以显示数字了,但是还需要做的更完善一点。有数字的时候显示,如果为0的时候就隐藏掉。所以再做一点小小的工作:

添加一个控制是否显示数字的属性:

public Visibility BadgeVisibility{    get { return (Visibility)GetValue(BadgeVisibilityProperty); }    set { SetValue(BadgeVisibilityProperty, value); }}// Using a DependencyProperty as the backing store for BadgeVisibility.  This enables animation, styling, binding, etc...public static readonly DependencyProperty BadgeVisibilityProperty =    DependencyProperty.Register("BadgeVisibility", typeof(Visibility), typeof(AppBarBadgeButton), new PropertyMetadata(Visibility.Collapsed, null));

 

 

当Count值变化时,将其设置为隐藏:

public string Count        {            get { return (string)GetValue(CountProperty); }            set { SetValue(CountProperty, value); }        }        // Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty CountProperty =            DependencyProperty.Register("Count", typeof(string), typeof(AppBarBadgeButton), new PropertyMetadata("0", OnCountChanged));        private static void OnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            int count = 0;            int.TryParse(e.NewValue.ToString(), out count);            if (count != 0)            {                ((AppBarBadgeButton)d).SetValue(BadgeVisibilityProperty, Visibility.Visible);            }            else            {                ((AppBarBadgeButton)d).SetValue(BadgeVisibilityProperty, Visibility.Collapsed);            }        }

 

最后将数字的Visibility属性绑定到这个值上:

 

 

代码已开源:

posted on
2017-09-21 10:20 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/7567209.html

你可能感兴趣的文章
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
算法第5章作业
查看>>
7.9 练习
查看>>
基于ArcGIS JS API的在线专题地图实现
查看>>
learnByWork
查看>>
lua 函数
查看>>
Git的基本命令
查看>>
四平方和
查看>>
第十八周 12.27-1.2
查看>>
C# IP地址字符串和数值转换
查看>>
TCHAR和CHAR类型的互转
查看>>
常用界面布局
查看>>
C语言—— for 循环
查看>>
IBM lotus9.0测试版即将公测
查看>>
xml常用方法
查看>>
Cube Stacking(并差集深度+结点个数)
查看>>
AndroidStudio3更改包名失败
查看>>
jq 删除数组中的元素
查看>>
js URL中文传参乱码
查看>>