WPF资源加载


资源的加载

同一程序集下加载URI:"Pack://application:,,,/Images/Logo.png" 或者"/Images/Logo.png"

不同程序集下加载URI:"Pack://application:,,,/Zhaoxi.Assets;component/Images/Logo.png"或者"/Zhaoxi.Assets;component/Images/Logo.png"

音视频文件的加载:

 设置内容拷贝到根目录下,用相对路径方式加载"/Audio/1.mp3"

this.img.Source = new BitmapImage(new Uri("Pack://application:,,,/Zhaoxi.Assets;component/Images/Logo.png", UriKind.RelativeOrAbsolute));//代码方式的加载

var result = this.Resources["color1"];//查找当前指定资源,传入key:color1
new TextBlock().Foreground = this.grid.Resources["color1"] as Brush;
result = this.FindResource("color");//另外一种方式查找指定资源

多个文件的资源合并






在App.xaml添加资源当前程序集都可访问.

资源动态跟静态加载

"{DynamicResource btnText}" 动态加载当资源更改资源动态更改

"{StaticResource btnText}" 静态加载,当资源更改,内容不会动态更改

WPF