WPF 03

staticResource和dynamicResource的区别

首先看一个案例

MainWindow.xaml


    
        
    
    
        
            
            
            
        
    

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不发生改变。

WPF 03_第1张图片

将样式单独写在xaml中

ButtonStyle.xaml


    
    

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


    
        
            
                
            
        
    

MainWindow.xaml


    
        
            
            
            
            
				
    

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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    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


     
    
    
        
            
        
    

你可能感兴趣的:(C#笔记,wpf)