2018-8-10-win10-uwp-使用资源在后台创建控件
| 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-使用资源在后台创建控件的更多相关文章
- UWP入门(四)--设置控件样式
原文:UWP入门(四)--设置控件样式 官方定义:可以使用 XAML 框架通过多种方式自定义应用的外观. 通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 可分享至不同e ...
- WPF 10天修炼 第七天- WPF资源、样式、控件模板
WPF资源 对象资源 WPF允许在XAML标记的任意位置定义资源.比如在特定的控件.窗口或应用程序级别定义资源,WPF资源系统提供的对象资源有如下好处: 1. 高效:使用对象资源可以在一个地方定义而 ...
- 模仿win10样式,基于jquery的时间控件
工作需要,写了一个基于jquery的时间控件,仿win10系统时间控件格式. 目前基本功能都有了,但时间格式只实现少数,但由于结构设计已经充分优化,填充起来非常容易. 这个控件相对网上其他的时间控件, ...
- 张高兴的 UWP 开发笔记:用 Thumb 控件仿制一个可拖动 Button
在 WPF 上可用的控件拖动方法在 UWP 上大多没用,那干脆用 Thumb 仿制一个吧. 关于 Thumb 控件的教程也不多,毕竟在 WPF 控件拖动有很多种方法, Thumb 就显得很鸡肋了.下面 ...
- IOS学习资源收集--开发UI控件相关
收集的一些本人了解过的iOS开发UI控件相关的代码资源(本文持续补充更新) 内容大纲: 1.本人在github上也上传了我分装好的一些可重复利用的UI控件 2.计时相关的自定义UILabel控件 正文 ...
- 从零开始学习前端开发 — 10、HTML5新标签及表单控件属性和属性值
一.html5新增标签 1.结构性标签 header 定义网页的头部 nav 定义网页的导航 footer 定义网页的底部 section 定义网页的某个区域 article 定义网页中的一篇文章 a ...
- WPF 10天修炼 第六天- 系统属性和常用控件
WPF系统属性和常用控件 渐变的背景色 WPF中的前景色和背景色不同于传统Winform的设置,这些属性都是Brush类型的值.在XAML中,当为这些属性设置指定的颜色后将被转换为SolidColor ...
- uwp,c#,listView与gridView列表控件进阶
listView与gridView使用类似,这里讲解gridView的一些数据绑定(x:Bind)基础知识. 顺便学习下如何使用属性通知.(后台中的数据变化会直接显示在UI上,实现动态变化,默认是没有 ...
- win10 当前操作环境不支持支付宝控件 完美解决办法
第一步,修改系统配置 在运行中输入“gpedit.msc”打开本地组策略编辑器: 打运行窗口的方法是:按win键+R (按下win键再按R键之后 同时松开) win键 即windows 的微标键 如 ...
随机推荐
- Android中的APK,TASK,PROCESS,USERID之间的关系
开发Android已经有一段时间了,今天接触到底层的东西,所以对于进程,用户的id以及Android中的Task,Apk之间的关系,要做一个研究,下面就是研究结果: apk一般占一个dalvik,一个 ...
- Codeforces768B-Code For 1-类似线段树-枚举+单点更新or区间更新
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 每次把\(n\ ...
- 如何用DOS命令查看占用某端口的程序及PID号
果学过JSP编程的朋友可以会发现,若用Eclipse运行JSP文件时常常会弹出某某端口正在使用,从而导致代码无法运行.如何查找出特定端口的使用情况以及对应的程序呢,针对该问题,本文介绍利用DOS命令查 ...
- go beego
一. 引入 go get github.com/astaxie/beego go get gitgub.com/beego/bee go get -u gitxxx.... 更新框架 编写 packa ...
- jeecg下实现自动默认模糊查询
也许jeecg的作者深受SAP毒害吧,没考虑到广大使用JEECG的人群为SAP用户,及所开发的项目均为中小项目,无惧大数据模糊查询带来的功能影响. 经网友“&&康&&& ...
- 高并发神器 Nginx,到底该怎么学?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 无论开发还是运维,工作上都会遇到性能优化.高并发的问题,而Nginx是一个万能药,它可以在百万并发连接下实现高吞吐量的 We ...
- python学习3—数据类型之整型、字符串和布尔值
python学习3-数据类型之整型.字符串和布尔值 数据类型 python3支持的数据类型共有6种: 1 Number 2 String 3 List 4 Tuple 5 Set 6 Dictiona ...
- vue 配置微信分享
参考:https://www.cnblogs.com/goloving/p/9256212.html 1. main.js import WXConfig from '../../assets/js/ ...
- "_CMTimeGetSeconds", referenced from:
CMTime is defined in the CoreMedia.framework. Add that framework to your project.
- HttpWebRequest请求返回非200的时候 HttpWebResponse怎么接受返回错误提示
当我们使用HttpWebRequest发送请求的时候如果服务器返回的不是200状态,那么请求代码肯定会异常,其实请求和返回并没有什么异常,只是.net内部就认定了 返回的不要是200 就是异常 那么我 ...