我们经常需要一个按钮,在按下时,后台执行Task,这时不能再次按下按钮。

我们使用自定义控件,首先新建一个类,我把它命名是ProgressButton

一个进度条按钮,也就是我们按下时发生进度条,完成时他又是按钮。

我们需要一个值让我们知道是不是已经完成了后台,按钮可以按下,在按下时,自动让按钮IsEnable为false。

我们需要模板有TextBlock,显示文字,ProgressRing显示进度条。

于是我们使用TemplatePart

    [TemplatePart(Name = "TextBlock", Type = typeof(TextBlock))][TemplatePart(Name = "Progress", Type = typeof(Windows.UI.Xaml.Controls.ProgressRing))]public class ProgressButton : Windows.UI.Xaml.Controls.Button

依赖属性其实很简单,我们需要在VS上大propdp 按Tab 就可以看到vs帮我们写的依赖属性。

我们需要修改属性名称,属性类型,默认值。

我这里的Text ,需要他修改时使用函数,这个叫CallBack。

依赖函数使用DependencyProperty.Register

他参数: name 是 属性名, propertyType 是属性类型, ownerType 是属于的类的类型, typeMetadata 是默认值和修改时使用函数

我们来说下 typeMetadata

typeMetadata 可以传入一个默认值,这个值就是我们不在依赖属性赋值,就给他一个默认的值。然后我们还可以给他一个在属性修改时使用的函数。

注意我们给他的函数不是必需,一般都不需要。

如果需要给他一个函数,这个函数需要有参数DependencyObject sender, DependencyPropertyChangedEventArgs e

其中 sender 是发送的实例,我们知道属性属于哪个类,我在这里,是属于ProgressButton ,我就可以使用 ProgressButton button = sender as ProgressButton;得到类,注意我们写的函数是静态的,所以sender才有用,我们可以使用sender获得类的属性

e 是有 NewValue 和 OldValue , NewValue 是我们要修改的值, OldValue 是原来的值。

大概需要的依赖属性在我们这个控件有 Text Complete 就没了。

Text是我们按钮的文字,Complete 是我们的后台是不是在执行,如果是的话,按钮就无法点击,显示进度条。

代码:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;namespace lindexi.uwp.control.Button.Control
{[TemplatePart(Name = "TextBlock", Type = typeof(TextBlock))][TemplatePart(Name = "Progress", Type = typeof(Windows.UI.Xaml.Controls.ProgressRing))]public class ProgressButton : Windows.UI.Xaml.Controls.Button{public ProgressButton(){DefaultStyleKey = typeof(ProgressButton);Click += ProgressButton_Click;}private void ProgressButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e){Complete = false;}private Windows.UI.Xaml.Controls.TextBlock _textBlock;private Windows.UI.Xaml.Controls.ProgressRing _proress;public string Text{get { return (string)GetValue(TextProperty); }set { SetValue(TextProperty, value); }}// Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...public static readonly DependencyProperty TextProperty =DependencyProperty.Register("Text", typeof(string), typeof(ProgressButton), new PropertyMetadata("",(d, e) =>{ProgressButton temp = d as ProgressButton;if (temp == null){return;}if(temp._textBlock!=null){temp._textBlock.Text = (string) e.NewValue;}}));public bool Complete{get { return (bool)GetValue(CompleteProperty); }set { SetValue(CompleteProperty, value); }}// Using a DependencyProperty as the backing store for Complete.  This enables animation, styling, binding, etc...public static readonly DependencyProperty CompleteProperty =DependencyProperty.Register("Complete", typeof(bool), typeof(ProgressButton), new PropertyMetadata(true,OnComplete));private static void OnComplete(DependencyObject d, DependencyPropertyChangedEventArgs e){ProgressButton button = d as ProgressButton;if (button == null){return;}bool temp = (bool)e.NewValue;//button._textBlock.Visibility = temp ? Visibility.Visible : Visibility.Collapsed;button._proress.Visibility = temp ? Visibility.Collapsed : Visibility.Visible;button.IsEnabled = temp;}protected override void OnApplyTemplate(){base.OnApplyTemplate();_textBlock = GetTemplateChild("TextBlock") as Windows.UI.Xaml.Controls.TextBlock;_proress = GetTemplateChild("Progress") as Windows.UI.Xaml.Controls.ProgressRing;if (_textBlock != null){_textBlock.Visibility = Visibility.Visible;_textBlock.Text = Text;}if (_proress != null){_proress.Visibility=Visibility.Collapsed;}}}
}

我们在控件 OnApplyTemplate 拿到 _textBlock _proress 我们需要写一个Style

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:control="using:lindexi.uwp.control.Button.Control"><Style TargetType="control:ProgressButton"><Setter Property="Background" Value="{ThemeResource ButtonBackground}"/><Setter Property="Foreground" Value="{ThemeResource ButtonForeground}"/><Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}"/><Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/><Setter Property="Padding" Value="8,4,8,4"/><Setter Property="HorizontalAlignment" Value="Left"/><Setter Property="VerticalAlignment" Value="Center"/><Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/><Setter Property="FontWeight" Value="Normal"/><Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/><Setter Property="UseSystemFocusVisuals" Value="True"/><Setter Property="FocusVisualMargin" Value="-3"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="control:ProgressButton"> <Grid x:Name="RootGrid" Background="{TemplateBinding Background}"><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualState x:Name="Normal"><Storyboard><PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/></Storyboard></VisualState><VisualState x:Name="PointerOver"><Storyboard><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="Foreground"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}"></DiscreteObjectKeyFrame></ObjectAnimationUsingKeyFrames><PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/></Storyboard></VisualState><VisualState x:Name="Pressed"><Storyboard><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPressed}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}"/></ObjectAnimationUsingKeyFrames><PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/></Storyboard></VisualState><VisualState x:Name="Disabled"><Storyboard><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}"/></ObjectAnimationUsingKeyFrames><ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="Foreground"><DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}"></DiscreteObjectKeyFrame></ObjectAnimationUsingKeyFrames></Storyboard></VisualState></VisualStateGroup></VisualStateManager.VisualStateGroups><ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}"Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/><TextBlock x:Name="TextBlock" Margin="10,10,10,10"HorizontalAlignment="Center"VerticalAlignment="Center"></TextBlock><ProgressRing x:Name="Progress" IsActive="True"></ProgressRing></Grid></ControlTemplate></Setter.Value></Setter></Style>
</ResourceDictionary>

这个是从Button复制,就改了Button为 control:ProgressButton

我们要使用按钮,需要在资源写

    <Page.Resources><ResourceDictionary Source="Control/ProgressButton.xaml"></ResourceDictionary></Page.Resources>

然后就可以使用 ProgressButton ,我写ProgressButton在control文件夹,我需要在命名空间xmlns:control="using:lindexi.uwp.control.Button.Control"

 <control:ProgressButton Text="确定"Complete="{x:Bind View.Complete,Mode=TwoWay}"Click="ButtonBase_OnClick"></control:ProgressButton>

我上面是测试,点击是进行100秒,过了就完成,代码简单,如果想知道,戳此链接 https://github.com/lindexi/UWP/tree/master/uwp/control/Button

那么如果我们有好多个页面都用到 ProgressButton ,我们需要在所有页面都写 ResourceDictionary 这样不好,我们有一个简单方法,让页面不用写这个。

在解决方案新建一个文件夹Themes,注意命名一定是Themes,注意有个名称后面有个s,我就在这坑好多天了。

然后新建资源字典 Generic.xaml ,注意名称也是不能自己修改。

在 Generic.xaml 合并字典

    <ResourceDictionary.MergedDictionaries><ResourceDictionary Source="ms-appx:///Control/ProgressButton.xaml"></ResourceDictionary></ResourceDictionary.MergedDictionaries>

这样我们就可以在页面直接用。

如果使用遇到问题,欢迎讨论。

参见:http://www.cnblogs.com/ms-uap/p/5520872.html


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。

转载于:https://www.cnblogs.com/lindexi/p/7694405.html

win10 uwp 按下等待按钮相关推荐

  1. 鼠标键盘唤醒计算机,除了按下电源按钮唤醒计算机,WIN10也可以使用鼠标或键盘来唤醒...

    哈喽,大家晚上好,我是一天不写点什么就感觉浑身不舒坦的沐沐君说软件.书接上回,今天小编将继续为小伙伴们分享Windows 10操作系统如何设置电源管理. 启用或禁用鼠标和键盘唤醒处于睡眠模式的计算机 ...

  2. java线程等待按钮点击_java如何用多线程使线程在sleep时等待按钮按下?

    比如这是一个八皇后的程序packagecom.equeen;importjava.applet.Applet;importjava.awt.Button;importjava.awt.Color;im ...

  3. win10 uwp 毛玻璃

    原文:win10 uwp 毛玻璃 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博 ...

  4. win10 uwp 让焦点在点击在页面空白处时回到textbox中

    原文:win10 uwp 让焦点在点击在页面空白处时回到textbox中 在网上 有一个大神问我这样的问题:在做UWP的项目,怎么能让焦点在点击在页面空白处时回到textbox中? 虽然我的小伙伴认为 ...

  5. win10 UWP Controls by function

    Windows的 XAML UI 框架提供了很多控件,支持用户界面开发库. 我现在做的一个中文版的,很多都是照着微软写,除了注释 我们先学微软做一个简单的frame,新建Page,里面放title和跳 ...

  6. 如何在计算机中增加硬盘分区,win10系统电脑下增加新磁盘分区的操作方法

    有关win10系统电脑下增加新磁盘分区的操作方法想必大家有所耳闻.但是能够对win10系统电脑下增加新磁盘分区进行实际操作的人却不多.其实解决win10系统电脑下增加新磁盘分区的问题也不是难事,小编这 ...

  7. win10 uwp 如何使用DataTemplate

    这是数据模板,一般用在数组的绑定,显示数组中的元素. 假如我们有一个列表,列表里是书,包括书名.作者.还有出版,那么我们只有源信息,如何把它显示到我们的ListView,就需要DataTemplate ...

  8. win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序

    本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...

  9. win10 uwp 商业游戏 1.1.5

    本文是在win10 uwp 商业游戏 基础上继续开发,添加一些无聊的游戏 因为在发布几个月,下载量很少,小伙伴说游戏就玩不到几分钟就不想玩,于是我就想加入其他游戏 下面我来告诉大家如何在游戏中添加多个 ...

最新文章

  1. 面试官问:硬盘里的苍老师每天以TB级别数据增长,你咋处理?我懵了。。。...
  2. Linux下grep显示前后几行信息
  3. React Hooks-概览
  4. pytorch笔记——autograd和Variable
  5. Server.MapPath(path)的使用
  6. Spring Boot + Shiro 集成
  7. rog live service是什么_双11手机怎么买?ROG游戏手机3“独一份”体验,值得剁手...
  8. WCF从理论到实践(10):异常处理 (转)
  9. 【李宏毅2020 ML/DL】P77 Generative Adversarial Network | Theory behind GAN
  10. 阿里历年经典Java面试题汇总
  11. Hadoop大数据开发基础课后答案
  12. html5的geolocation 定位误差大的解决办法
  13. java+javascript获得两个日期之间的所有月份
  14. 整理的几种适用于GROMACS输入的小分子拓扑文件获取流程
  15. iOS打包ipa文件
  16. 检测android 小米 系统更新,小米健康重磅更新!系统级心率检测功能来了
  17. 【linux】内核模块管理:lsmod、insmod、rmmod、modinfo、modprobe、depmod命令
  18. unity 震动脚本_Unity - Cinemachine实现相机抖动
  19. vue2.0 结合HTML5原生Audio标签在移动端的使用实现方式,应用场景为钉钉微应用
  20. 知识产品经理需要掌握什么知识?

热门文章

  1. sendmail邮件服务器支持账户名大小写
  2. C#Winform控件随窗体缩放
  3. 【Quartz】Quartz
  4. 普通管理类程序开发之难度系数、层次之说法,可以看看自己停留在哪个层次,不足之处,请大家一起补充...
  5. 第五部份 01 深入JavaScript与.NET Framework中的日期时间 JavaScript中的Date类型
  6. kali 安装输入法
  7. C#基础笔记(第九天)
  8. POJ 1733 Parity game(带权并查集)
  9. Windows XP Mode for Windows 7
  10. Android性能优化——腾讯、字节、阿里、百度、网易等互联网公司项目实战+案例分析(附PDF)