WPF应用程序使用资源及多语言设置学习-1
2009-05-26 17:08
381 查看
最近学习dotnet多语言资源的设置,很多地方没有搞清楚,于是参考msdn写了点代码验证下想法。
测试程序是一个wpf默认程序,语言和资源均不做设置。
字符串资源
在默认资源文件中添加一条string1=teststring,编译之后用工具查看程序集可以发现字符串资源保存在名为"MySampleApp.Properties.Resources.resources"的资源中,其中MySampleApp为程序集的命名空间,"Properties.Resources"是默认资源文件的名称,".resources"是资源扩展名。
读取此字符串资源代码如下:
//直接调用资源编辑器生成的类方法
Mywindow.Title = MySampleApp.Properties.Resources.string1;
//或者使用通用方法
var rm = new ResourceManager("MySampleApp.Properties.Resources", Assembly.GetExecutingAssembly());
Mywindow.Title = rm.GetString("string1");
读取字符串资源的xaml代码如下:
<StackPanel Name="root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:MySampleApp.Properties"
x:Class="MySampleApp.MyPage">
<TextBlock Name="Text1" Text="{x:Static res:Resources.string1}"></TextBlock>
</StackPanel>
注意如果想让上面的xaml代码能正常读取系统默认字符串资源,需要设置资源访问属性为public,如图所示:
图片资源
在资源编辑器中添加一个png图片,名为img,资源中的图片格式为System.Drawing.Bitmap。
读取img就是调用rm.GetObject("img"),返回对象是Bitmap类型,此对象无法直接用在WPF的Image控件上,必须做一些转换工作,转换Bitmap为BitmapImage.
var rm = new ResourceManager("MySampleApp.Properties.Resources", Assembly.GetExecutingAssembly());
var bmp = rm.GetObject("img") as Bitmap;
var stream = new MemoryStream();
var bi = new BitmapImage();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
第二种方法:转换Bitmap为BitmapSource.
IntPtr bmpPt = bmp.GetHbitmap();
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmpPt, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
bitmapSource.Freeze();
DeleteObject(bmpPt);
image1.Source = bitmapSource;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int DeleteObject(IntPtr o);
测试程序是一个wpf默认程序,语言和资源均不做设置。
字符串资源
在默认资源文件中添加一条string1=teststring,编译之后用工具查看程序集可以发现字符串资源保存在名为"MySampleApp.Properties.Resources.resources"的资源中,其中MySampleApp为程序集的命名空间,"Properties.Resources"是默认资源文件的名称,".resources"是资源扩展名。
读取此字符串资源代码如下:
//直接调用资源编辑器生成的类方法
Mywindow.Title = MySampleApp.Properties.Resources.string1;
//或者使用通用方法
var rm = new ResourceManager("MySampleApp.Properties.Resources", Assembly.GetExecutingAssembly());
Mywindow.Title = rm.GetString("string1");
读取字符串资源的xaml代码如下:
<StackPanel Name="root"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:MySampleApp.Properties"
x:Class="MySampleApp.MyPage">
<TextBlock Name="Text1" Text="{x:Static res:Resources.string1}"></TextBlock>
</StackPanel>
注意如果想让上面的xaml代码能正常读取系统默认字符串资源,需要设置资源访问属性为public,如图所示:
图片资源
在资源编辑器中添加一个png图片,名为img,资源中的图片格式为System.Drawing.Bitmap。
读取img就是调用rm.GetObject("img"),返回对象是Bitmap类型,此对象无法直接用在WPF的Image控件上,必须做一些转换工作,转换Bitmap为BitmapImage.
var rm = new ResourceManager("MySampleApp.Properties.Resources", Assembly.GetExecutingAssembly());
var bmp = rm.GetObject("img") as Bitmap;
var stream = new MemoryStream();
var bi = new BitmapImage();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
第二种方法:转换Bitmap为BitmapSource.
IntPtr bmpPt = bmp.GetHbitmap();
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmpPt, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
bitmapSource.Freeze();
DeleteObject(bmpPt);
image1.Source = bitmapSource;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int DeleteObject(IntPtr o);
相关文章推荐
- WPF应用程序使用资源及多语言设置学习-3
- WPF应用程序使用资源及多语言设置学习
- WPF应用程序使用资源及多语言设置学习-2
- WPF 使用net 资源多语言
- MonoRail学习笔记二十:资源文件的使用和多语言支持
- c#使用资源文件实现多语言应用程序
- WPF使用资源文件设置托盘(NotifyIcon)图标
- wpf获取和设置应用程序范围的资源
- MonoRail学习笔记二十:资源文件的使用和多语言支持
- wpf获取和设置应用程序范围的资源
- 汇编语言学习笔记-使用菜单资源
- WPF开发学习:资源在内外部定义的不同方式和怎么引用,Style样式的四种使用(对比CSS)
- MonoRail学习笔记二十:资源文件的使用和多语言支持
- Linux学习7之linux下使用alias设置指令别名并使其永远生效
- 使用Scala语言开发Spark应用程序是
- Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- swift学习笔记之navigationController的设置以及使用
- 汇编语言学习-Dos下的调试工具debug的使用教程
- 使用ArcGIS Runtime WPF SDK (1)创建自己的地图应用程序