目录

WPF对象资源

目录

参数复用

资源的定义

使用方式


资源统一设置:对象资源属性/向上搜索:APP、系统资源

参数复用

需求:想和C#代码一样,写一个全局的参数,都能使用。

复制代码
 <!--以下三个命名空间都可以在xaml中使用C#中的属性-->
 <!--xmlns:sys="clr-namespace:System;assembly=mscorlib"-->
 <!--xmlns:sys="clr-namespace:System;assembly=netstandard"-->
 <!--xmlns:sys="clr-namespace:System;assembly=System.Runtime"-->

<Window x:Class="XH.StyleLesson.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XH.StyleLesson"
        xmlns:sys="clr-namespace:System;assembly=System.Runtime"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
<Window.Resources>
  <!--参数定义-->
  <sys:Double x:Key="value">120</sys:Double>
  <SolidColorBrush x:Key="brush">red</SolidColorBrush>
  <Style TargetType="Button" x:Key="btnStyle">
      <!--参数使用-->
      <Setter Property="Width" Value="{StaticResource value}"/>
      <Setter Property="Height" Value="200"/>
      <Setter Property="Background" Value="{StaticResource brush}"/>
  </Style>
</Window.Resources>
  <Grid>
    <Button Style="{StaticResource btnStyle}"/>    
  </Grid>
</Window>

注意:xaml中使用参数时候,数据类型一定要属性需要的数据类型保持一致,否则会报错。

资源的定义

Resources 对象的资源管理

xaml中任何对象都包含Resources资源管理

也是有作用域的,搜索方式是父对象资源的查找

也直接在App.xaml也可以设置

StaticResource 静态资源 逐行扫描 从前往后扫描 找不到就报错

DynamicResource动态资源 先加载完运行完再扫描 找不到不会报错 就是不显示

使用区别:希望资源在运行过程中指定:使用动态;如果固定资源:使用静态

性能上:首推静态,能用静态不使用动态

使用方式

如果在Style中,需要当前控件下都默认使用的话,就不用设置key,如果是特定的,需要使用key进行绑定。

复制代码
<Style TargetType="Button" x:Key="btnStyle">
    <Setter Property="Width" Value="200"/>
    <Setter Property="Height" Value="200"/>
    <Setter Property="Background" Value="{StaticResource brush}"/>
</Style>

<!--绑定即可-->
<Button Style="{StaticResource btnStyle}"/>
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
yagerfgcs1 分钟前
【配置Visual Assistant VC助手 for Visual Studio 2017】
ide·c#·visual studio
PfCoder2 小时前
WinForm真入门(13)——ListBox控件详解
windows·c#·visual studio·winform
佟格湾5 小时前
聊透多线程编程-线程池-6.C# APM(异步编程模型)
开发语言·后端·c#·多线程
我不是程序猿儿6 小时前
【C#】一种优雅的基于winform的串口通信管理
stm32·单片机·c#
向宇it11 小时前
【unity游戏开发入门到精通——动画篇】Animator反向动力学(IK)
开发语言·unity·c#·编辑器·游戏引擎
Liu_某12 小时前
c# 运用策略模式与模板方法模式实例
c#·策略模式
ALex_zry14 小时前
构建高可靠C++服务框架:从日志系统到任务调度器的完整实现
开发语言·c++·wpf
du fei14 小时前
C# 组件的使用方法
java·开发语言·c#
xiaowu08017 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
花之亡灵1 天前
.net6 中实现邮件发送
笔记·c#·.net·代码规范