title author date CreateTime categories
win10 uwp 使用资源在后台创建控件
lindexi
2018-08-10 19:17:19 +0800
2018-07-21 09:34:16 +0800
Win10 UWP

本文告诉大家如何使用资源在后台创建控件,本文使用按钮做例子,包括如何绑定资源,找到资源。

定义资源

在 App.xaml 定义的资源样式可以在整个程序拿到,但是不建议在 App.xaml 直接写资源,建议是写一个资源文件,例如是 SormarMapay.xaml 在 App.xaml 用ResourceDictionary.MergedDictionaries合并

    <Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SormarMapay.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

现在可以打开 SormarMapay.xaml 写样式,这里需要写一个按钮的样式,就需要设置TargetType="Button",例如这个按钮需要一张图片和标题、次标题

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="CustomButtonLarge" TargetType="Button">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="White" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Margin" Value="10,10,10,10"></Setter>
<Setter Property="Height" Value="200" />
<Setter Property="Width" Value="100" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" >
<Grid x:Name="AlbumContentGrid" Background="{TemplateBinding Background}" >
<Grid.RowDefinitions>
<RowDefinition Height="200*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> <Image x:Name="AlbumCover" Grid.Row="0" Source="{Binding Path=Image}"/>
<TextBlock x:Name="Title" Grid.Row="1" Margin="10,10,10,10" Text="{Binding Title}" Foreground="{TemplateBinding Foreground}"/>
<TextBlock x:Name="SubTitle" Grid.Row="2" Margin="10,10,10,10" Text="{Binding SubTitle}" Foreground="{TemplateBinding Foreground}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

这里需要解释一下,使用的<Grid x:Name="AlbumContentGrid" Background="{TemplateBinding Background}" >是为了让按钮的背景有用,如果没有设置这个值,也就是按钮的背景设置了是没有用的。

里面的控件使用的是x:Name="AlbumCover"而不是 x:Key ,因为只能使用name的方法。

为了在后台代码可以修改按钮的内容,就需要使用绑定 DataContext ,这时绑定只能用 Binding 的方法,如果大家发现如何在这里使用 x:bind 请告诉我

定义数据

这里使用的数据需要自己定义,下面代码定义一直类

    public class Foo
{
public BitmapImage Image { get; set; } public string Title { get; set; } public string SubTitle { get; set; }
}

创建按钮

在 Main 页面添加下面代码,用来创建多个按钮

        public MainPage()
{
this.InitializeComponent(); Loaded += MainPage_Loaded;
} private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
int numberOfButtons = 3; for (int i = 0; i < numberOfButtons; i++)
{
var foo = new Foo
{
Image = new BitmapImage(new Uri("ms-appx:///Assets/Square44x44Logo.scale-200.png")),
Title = "title" + i,
SubTitle = i.ToString()
}; Button btn = new Button();
Style style = Application.Current.Resources["CustomButtonLarge"] as Style; btn.Style = style; btn.DataContext = foo; StackAlbums.Children.Add(btn);
}
}

上面的 StackAlbums 就是一个 StackPanel ,现在运行代码可以看到下面界面

添加动画

如果使用了上面的代码可以看到,这个界面按钮是不存在按下的动画,因为没有写 VisualStateManager 现在打开 SormarMapay.xaml 在 AlbumContentGrid 添加下面代码

                        <VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="AlbumContentGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AlbumContentGrid"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0"
Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Title"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="#aaaaaa" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SubTitle"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0"
Value="#aaaaaa" />
</ObjectAnimationUsingKeyFrames>
<PointerUpThemeAnimation Storyboard.TargetName="AlbumContentGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AlbumContentGrid"
Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AlbumContentGrid"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Title"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SubTitle"
Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
</ObjectAnimationUsingKeyFrames>
<PointerDownThemeAnimation Storyboard.TargetName="AlbumContentGrid" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

然后在 AlbumContentGrid 绑定一下 BorderBrush ,请看代码

Grid x:Name="AlbumContentGrid" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"

现在代码看起来就是这样

尝试运行一下代码,可以看到按下动画

2018-8-10-win10-uwp-使用资源在后台创建控件的更多相关文章

  1. UWP入门(四)--设置控件样式

    原文:UWP入门(四)--设置控件样式 官方定义:可以使用 XAML 框架通过多种方式自定义应用的外观. 通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 可分享至不同e ...

  2. WPF 10天修炼 第七天- WPF资源、样式、控件模板

    WPF资源 对象资源 WPF允许在XAML标记的任意位置定义资源.比如在特定的控件.窗口或应用程序级别定义资源,WPF资源系统提供的对象资源有如下好处: 1.  高效:使用对象资源可以在一个地方定义而 ...

  3. 模仿win10样式,基于jquery的时间控件

    工作需要,写了一个基于jquery的时间控件,仿win10系统时间控件格式. 目前基本功能都有了,但时间格式只实现少数,但由于结构设计已经充分优化,填充起来非常容易. 这个控件相对网上其他的时间控件, ...

  4. 张高兴的 UWP 开发笔记:用 Thumb 控件仿制一个可拖动 Button

    在 WPF 上可用的控件拖动方法在 UWP 上大多没用,那干脆用 Thumb 仿制一个吧. 关于 Thumb 控件的教程也不多,毕竟在 WPF 控件拖动有很多种方法, Thumb 就显得很鸡肋了.下面 ...

  5. IOS学习资源收集--开发UI控件相关

    收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...

  6. 从零开始学习前端开发 — 10、HTML5新标签及表单控件属性和属性值

    一.html5新增标签 1.结构性标签 header 定义网页的头部 nav 定义网页的导航 footer 定义网页的底部 section 定义网页的某个区域 article 定义网页中的一篇文章 a ...

  7. WPF 10天修炼 第六天- 系统属性和常用控件

    WPF系统属性和常用控件 渐变的背景色 WPF中的前景色和背景色不同于传统Winform的设置,这些属性都是Brush类型的值.在XAML中,当为这些属性设置指定的颜色后将被转换为SolidColor ...

  8. uwp,c#,listView与gridView列表控件进阶

    listView与gridView使用类似,这里讲解gridView的一些数据绑定(x:Bind)基础知识. 顺便学习下如何使用属性通知.(后台中的数据变化会直接显示在UI上,实现动态变化,默认是没有 ...

  9. win10 当前操作环境不支持支付宝控件 完美解决办法

    第一步,修改系统配置 在运行中输入“gpedit.msc”打开本地组策略编辑器: 打运行窗口的方法是:按win键+R (按下win键再按R键之后 同时松开)  win键 即windows 的微标键 如 ...

随机推荐

  1. bzoj1012题解

    [解题思路] 强制在线线段树/树状数组,没什么好说的..复杂度O(mlog2m)(线段树)或O(mlog22m)(树状数组). [参考代码] (还naive的时候写的zkw真是翔..) #includ ...

  2. macOS cataline 10.15 升级后问题一览

    1. git无法使用.报错如下 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), m ...

  3. (转)RSA加密解密及数字签名Java实现

    转:http://my.oschina.net/jiangli0502/blog/171263?fromerr=hc4izFe2  RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rives ...

  4. 永久修改 putty字体大小

    修改前: 修改操作: 1. Window -> Appearance -> Font settings  -> Change 修改 修改 2.  返回登陆主界面  Session   ...

  5. JS-copy到剪贴板

    因为 clipboard.js 兼容性受限

  6. C4D中python初探

    use_name = input('请输入账号') password = input('请输入密码') if use_name == 'alex' and password == 'alex3714' ...

  7. sql 循环执行游标

    ---定义开始和结束时间 declare @st_dt datetime declare @en_dt datetime ---时间赋值 ' ' ---定义中间变量 declare @dt datet ...

  8. Spark RDD API(scala)

    1.RDD RDD(Resilient Distributed Dataset弹性分布式数据集)是Spark中抽象的数据结构类型,任何数据在Spark中都被表示为RDD.从编程的角度来看,RDD可以简 ...

  9. double to string 损失精度的问题

    https://blog.csdn.net/magieclarence/article/details/6792511?utm_source=blogxgwz0 类似于这样 double 的精度是一个 ...

  10. leetcode-241-为运算表达式设置优先级*

    题目描述: 方法:分治* class Solution: def diffWaysToCompute(self, input: str) -> List[int]: if input.isdig ...