WPF-使用对象资源
来源:互联网 发布:淘宝打包员累吗? 编辑:程序博客网 时间:2024/05/22 03:33
(1)窗口级别资源
<Button Margin="25" Height="200" Width="200" FontSize="20">
<Button.Background>
<RadialGradientBrush>
<GradientStop Color="#ffc44ec4" Offset="0" />
<GradientStop Color="#ff829ceb" Offset="1" />
<GradientStop Color="#ff793879" Offset="0.669" />
</RadialGradientBrush>
</Button.Background>
</Button>
(2){ StaticResource}标记
将上面的Background封装为资源的属性,在Background属性的Extract value to Resource(将值提取到资源)选项,将资源命名。
<Window.Resources>
<RadialGradientBrush x:Key="myBrush">
<GradientStop Color="#ffc44ec4" Offset="0" />
<GradientStop Color="#ff829ceb" Offset="1" />
<GradientStop Color="#ff793879" Offset="0.669" />
</RadialGradientBrush>
</Window.Resources>
将button的Background绑定StaticResource。
<Button Background="{StaticResource myBrush}"/>
不过这样的Resources,只能在当前的窗体中调用,不能实现跨窗体资源调用,要实现跨窗体,需要在app.xaml中定义:
<Application.Resources>
<RadialGradientBrush x:Key="myBrush">
<GradientStop Color="#ffc44ec4" Offset="0" />
<GradientStop Color="#ff829ceb" Offset="1" />
<GradientStop Color="#ff793879" Offset="0.669" />
</RadialGradientBrush>
</Application.Resources>
在button中Background属性,单击Apply Resource(应用资源),查找到myBrush。
定义资源字典
多个WPF项目复用资源,创建Resource Dictionary(WPF)文件,添加资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<RadialGradientBrush x:Key="myBrush">
<GradientStop Color="#ffc44ec4" Offset="0" />
<GradientStop Color="#ff829ceb" Offset="1" />
<GradientStop Color="#ff793879" Offset="0.669" />
</RadialGradientBrush>
</ResourceDictionary>
然后在App.xaml中添加:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
最后在button中Background属性,单击Apply Resource(应用资源),查找到myBrush。
- WPF-使用对象资源
- WPF资源字典使用
- wpf资源使用
- wpf使用资源字典组织资源
- WPF使用资源绑定自定义类型
- WPF使用资源绑定自定义类型
- C# wpf 使用资源文件 resx
- WPF 使用net 资源多语言
- 使用对象管理资源
- WPF 资源
- WPF资源
- WPF资源
- WPF资源
- WPF资源
- WPF资源
- Wpf资源
- WPF对象级资源的定义与查找
- 使用对象来管理资源
- CentOS中升级Python到3.2
- asp.net PostedFile.ContentType的所有类型
- 客户端请求服务器过程
- url重写—适用html为伪静态后真实的html无法访问的解决方法
- 代码生成器插件开发---代码生成项目框架的创建(2)
- WPF-使用对象资源
- Telerik——第三方组件(For winform and asp.net)
- 快速排序
- 使用 SOAP 协议调用 Web服务(WebService)
- = =大家一起黑马忠啊
- XC笔试题
- <转>消息如何流动 ----------- 注意 GetMessageMap为虚函数
- CRM的七大罪状批注
- Mysql 下部分总结