Skip to content
分类目录:

Resource

Post date:
Author:
Number of comments: no comments

程序集资源:P181

添加资源:添加图片,将图片的 BuildAction 修改为Resource

检索资源:

方式 1:

StreamResourceInfo sri = Application.GetResourceStream(new Uri(“folder/filename”,UriKind.Relative));

sri 包含两个属性:Content-描述数据类型的字符串,UnmanagedMemoryStream

方式 2:

Application.GetResourceStream 方法本质上是封装了 ResourceManager 类和 ResourceSet 类,所以可以利用 ResourceManager 和 ResourceSet 来读取程序集资源,存储所有 WPF 资源的地方为:AssemblyName.g.resources;

代码如下:

读取程序集的图片资源并保存;

Assembly ass = Assembly.GetAssembly(this.GetType());
String resourceName = ass.GetName().Name + ".g";
 ResourceManager rm = new ResourceManager(resourceName, ass);
using(ResourceSet set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))   {
    UnmanagedMemoryStream s;
    s= set.GetObject("desktop_1.jpg", true) as UnmanagedMemoryStream;
    FileStream fs = new FileStream(@"D:\test11.jpg", FileMode.Create);
    byte[] bufs = new byte[100];
    while(true){
        int re = s.Read(bufs, 0, bufs.Length);
        if (re == 0)
        {
            break;
        }
        fs.Write(bufs, 0, re);
    }
    s.Close();
    fs.Close();
}

方式 3:利用 pack Uri 方式:

refer to WPF 编程宝典,Chapter7.3 ,p184

WPF 使用 pack URI 语法寻址编译过的资源(VS IDE 中设置为Resource);

WPF专业编程指南:P193

pack://authority/path

authority包括两种:application,siteoforigin

application:/// 用作访问再编译时已经知道的文件,不能时嵌入式资源

siteoforigin:/// 用作访问在编译时不知道的文件

如:

//访问当前程序集的资源
img.Source=new BitmapImage(new Uri(“pack://application:,,,/images/w.jpg”));

//访问位于其他程序集中的资源:
pack://application:,,,/AssemblyName;component/ResourceName
//或者相对路径:
//ImageLibrary;component/images/winter.jpg

三个逗号实际上是三个转义的斜杠,即:application:///

WPF 声音类不支持程序集资源。

WPF 中查找 Control.Resources 资源

Xaml 中使用 StaticResource resousekey=…

Code 中使用 this.TryFindResrouce(resourcekey)

资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib"
  >
  <FontFamily x:Key="Accunome.Fonts.Family.Default">Microsoft YaHei</FontFamily>
	<!--定义Int类型 -->
  <system:Double x:Key="Accunome.Fonts.Size.Default">26</system:Double>

  <ImageBrush ImageSource="/StockBond;component/Images/Menu-Nomal.png" />
  <!--简化版 -->
  <!--假如 xaml文件在Themes/xxx.xaml,而图片在Images/Menu-Nomal.png-->
  <!-- 
   		ImageSource如果以..开头,则是相对路径,如果是以 / 开头,则是绝对路径,指向根目录
 			image资源需要设置属性为Copy
  -->
  <ImageBrush ImageSource="../Images/Menu-Nomal.png" />
  

</ResourceDictionary>



//pack://application:,,,/<libraryName>;component/<FolderName>/<Menu-Nomal.png>
  startPanelIcon.Source = new BitmapImage(new Uri("pack://application:,,,/StockBond;component/Images/Menu-Nomal.png"));

当为应用程序添加ResourceDictionary.xaml文件时,务必将 Build Action 设置为 Page,这样可保证为了获得最佳性能而将资源字典编译为 BAML。

不过将 Build Action 设置为 Resource 也可以,这样会被嵌入到程序集中,但是不会被编译为 BAML。

为了使用资源字典,需要将其合并到程序的资源集合中。

<Application.Resources>
 <ResourceDictianry>
 <ResourceDictionary.MergedDictinaries>
  <ResourceDictionary Source="AppBrushes.xaml"/>
 </ResourceDictionary.MergedDictionaries>
 <ImageBrush x:Key=”simple” …./>
</ResourceDictionary>
</Application.Resources>

代码中读取 ResourceDictionary 文件中的内容:

ResourceDictionary rd=new ResourceDictionary();
rd.Source=new Uri("pack://ResourceLibrary;component/BurshDictionary.xmal”,UriKind.Relative");
var value=rd[Key];

Application.Current.TryFindResource(string key);

StaticResource的使用方式:

1.静态资源定义:可以放在 resource 中,也可以放在 static 的成员变量中“`

<!--定义 -->
<Color x:Key="ToolWindowContentBackgroundColor">#424242</Color>
<!--使用 -->
<SolidColorBrush Color="{StaticResource ToolWindowContentBackgroundColor}" x:Key="ToolWindowContentBackgroundBrush"/>

public class Common{
    public static String Source{get;set;}
}

x:Static

Application.Resources

<Application   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:Wpf.Test"
   xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="Wpf.Test.App"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources\ButtonBaseControlTemplate.xaml"/>
                <ResourceDictionary Source="Resources\TabItemStyle.xaml" />
                <ResourceDictionary Source="Resources\TextBoxBaseControlTemplate.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Resources\TextBoxBaseControlTemplate.xaml文件内容如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="clr-namespace:Wpf.Test.Resources"
   xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
 >
    <ControlTemplate x:Key="TextBoxBaseControlTemplate1" TargetType="{x:Type TextBoxBase}">
        <Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="True">
            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </Themes:ListBoxChrome>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
            <Trigger Property="UIElement.IsFocused" Value="True">
                <Setter Property="BorderBrush" Value="#EE7621"/>
                <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

样式在具体控件上的使用方式:

<TextBox x:Name="txtFolderPath" Template="{DynamicResource TextBoxBaseControlTemplate1}" ToolTip="常用路径">D:\Users\hym\QINSW\Source_Code\NMS\Host\Bin</TextBox>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

豫ICP备2021008859号-1