WPF 资源 总结

资源


=========================================
图片:,右键图片文件,设为始终复制到生成目录,把生成操作改为 内容


XAML中设置路径:


  <Image Width="100" Height="100" Source="/wpfLab5;component/Images/hills.jpg"></Image>
  <Image Width="100" Height="100" x:Name="img2"></Image>




CS中(三种写法都可以):

  //    img2.Source = new BitmapImage(new Uri("pack://application:,,,/Images/hills.jpg"));
            img2.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/Images/hills.jpg"));
            img3.Source = new BitmapImage(new Uri("/Images/hills.jpg",UriKind.Relative));




================================================


定义静态资源


   <Window.Resources>
        <SolidColorBrush x:Key="redBrush" >Red</SolidColorBrush>
    </Window.Resources>


<Button Background="{StaticResource ResourceKey=redBrush}" Width="30" Height="24" Content="A"></Button>




===================================================


使用资源字典


MainWindow.xaml


<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>


  <Button Background="{StaticResource ResourceKey=redBrush}" Width="30" Height="24" Content="A"></Button>
  <Button Background="{StaticResource ResourceKey=yellowBrush}" Width="30" Height="24" Content="B"></Button>




Dictionary1.xaml


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="yellowBrush">Yellow</SolidColorBrush>
    <SolidColorBrush x:Key="redBrush" >Red</SolidColorBrush>
</ResourceDictionary>








=========================================================


使用properties保存字符串


Properties.Resources.img2Path





你可能感兴趣的:(WPF 资源 总结)