WPF 03

staticResource和dynamicResource的区别

首先看一个案例

MainWindow.xaml

<Window x:Class="WpfDay03.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:WpfDay03"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="SolidColor" Color="Red"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Content="update" Margin="5" Click="Button_Click"></Button>
            <Button Content="btn1" Margin="5" BorderBrush="{StaticResource SolidColor}" BorderThickness="5"></Button>
            <Button Content="btn2" Margin="5" BorderBrush="{DynamicResource SolidColor}" BorderThickness="5"></Button>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfDay03
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
        }
    }
}

当update按钮点击之后只有使用动态资源的btn2边框颜色改变,而使用静态资源的btn1不发生改变。

将样式单独写在xaml中

ButtonStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="SolidColor" Color="Red"/>
    <Style x:Key="DefaultButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Blue"></Setter>
        <Setter Property="FontSize" Value="15"></Setter>
    </Style>
</ResourceDictionary>

需要在App.xaml中添加该资源字典

<Application x:Class="WpfDay03.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfDay03"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="WpfDay03.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:WpfDay03"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Button Content="update" Margin="5" Click="Button_Click"></Button>
            <Button Content="btn1" Margin="5" BorderBrush="{StaticResource SolidColor}" BorderThickness="5"></Button>
            <Button Content="btn2" Margin="5" BorderBrush="{DynamicResource SolidColor}" BorderThickness="5"></Button>
            <Button Content="useStyle" Margin="5" Style="{StaticResource DefaultButtonStyle}"></Button>
				</StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfDay03
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
          	//查看资源
            var solidColor = App.Current.FindResource("SolidColor");
            var style = App.Current.FindResource("DefaultButtonStyle");
        }
    }
}

动画基础

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDay03
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);

            //var solidColor = App.Current.FindResource("SolidColor");
            //var style = App.Current.FindResource("DefaultButtonStyle");

            //创建一个双精度动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.By = -30;
            //animation.From = btn.Width; //设置动画的初始值
            //animation.To = btn.Width - 30; //设置动画的结束值
            animation.Duration = TimeSpan.FromSeconds(1); //设置动画的持续时间
            animation.AutoReverse = true; //是否往返执行
            animation.RepeatBehavior = new RepeatBehavior(3); //RepeatBehavior.Forever; 执行周期
            animation.Completed += Animation_Completed;
            btn.BeginAnimation(Button.WidthProperty, animation);

        }
        private void Animation_Completed(object sender, EventArgs e)
        {
            btn.Content = "动画已完成";
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfDay03.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:WpfDay03"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources> 
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Width="100" Height="40" x:Name="btn" 
						Content="update" Margin="5" Click="Button_Click"></Button>
        </StackPanel>
    </Grid>
</Window>
相关推荐
芝麻科技9 小时前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退1 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆1 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退1 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu1 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退5 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆5 天前
WPF中的XAML详解
wpf·xaml
ithouse5 天前
使用WPF实现一个快速切换JDK版本的客户端工具
java·开发语言·wpf
河西石头5 天前
WPF之UI进阶--控件样式与样式模板及词典
ui·wpf·样式·模板·控件样式·样式模板·样式词典
TA远方6 天前
【WPF】桌面程序开发之窗口的用户控件详解
c#·wpf·usercontrol·用户控件·控件属性