Silverlight Resource

1. Resource Files

Silverlight 可以将资源文件嵌入到程序集,或者打包到 XAP 文件中,当然也支持直接访问网站文件。

(1) Content

在项目属性窗口将资源 Build Action 设为 Content,那么资源将会被打包到 XAP 文件中,支持子目录。

 





用 WinRAR / WinZIP 之类的工具打开 XAP 文件,你会看到所添加的资源。利用 Application.GetResouceStream,我们很容易读取资源文件内容。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var info = Application.GetResourceStream(new Uri("Resources/Test.txt", UriKind.Relative));

    using (var reader = new StreamReader(info.Stream))
    {
        var text = reader.ReadToEnd();
        this.TextBox1.Text = text;
    }
}


如果在 XAML 中使用资源,就更简单了。

<Image Source="/Resources/1.jpg" />


我们也可以将资源打包到其他的 ZIP / XAP 文件中,这样可以根据需要按需下载。下面的例子中,我们创建了一个包含 "2.jpg" 和 "Hello.txt" 的压缩文件 Test.zip,并将其放到 ClientBin 目录中。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var client = new WebClient();

    client.OpenReadCompleted += (s, ex) =>
    {
        var info = new StreamResourceInfo(ex.Result, null);

        var bitmapInfo = Application.GetResourceStream(info, new Uri("2.jpg", UriKind.Relative));

        var bitmap = new BitmapImage();
        bitmap.SetSource(bitmapInfo.Stream);
        this.Image1.Source = bitmap;

        var textInfo = Application.GetResourceStream(info, new Uri("Hello.txt", UriKind.Relative));
        var text = new StreamReader(textInfo.Stream).ReadToEnd();
        this.TextBox1.Text = text;
    };

    var url = Application.Current.Host.Source.AbsolutePath.Replace("Learn.Silverlight.xap", "Test.zip");
    client.OpenReadAsync(new Uri(url, UriKind.Relative));
}


(2) Resource

注意 Build Action 应该是 Resource,而不是 Embedded Resource。这是因为 Silverlight 和 WPF 一样,将嵌入式资源文件打包成 "*.g.resources" 文件。



在 XAML 中可以直接用简短路径。

<Image Name="Image1" Source="Resources/1.jpg" />


程序代码中,需要使用特定的 URI 格式。

/assemblyShortName;component/resourceLocation


试试看。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var uri = new Uri("/Learn.Silverlight;component/Resources/1.jpg", UriKind.Relative);
    var info = Application.GetResourceStream(uri);

    var bitmap = new BitmapImage();
    bitmap.SetSource(info.Stream);
    this.Image1.Source = bitmap;
}


当然也可以是 XAP 包中其他程序集的嵌入资源。

var uri = new Uri("/Learn.Silverlight.Library;component/Images/Panda.jpg", UriKind.Relative);
...


(3) Loose File

这种直接放置在网站目录下(On the Site of Origin)的松散文件(Loose File),可以直接用 "http://..." 进行访问。

<Image Name="Image1" Source="http://localhost:9393/Images/abc.jpg" />


如果资源文件放置在 ClientBin 目录,可以使用相对路径。Silverlight 默认相对路径的根是 XAP 所在目录,它首先查找 XAP 文件,然后是 XAP 所在目录。

<Image Name="Image1" Source="http://localhost:9393/ClientBin/xxx.jpg" />
<Image Name="Image1" Source="/xxx.jpg" />
<Image Name="Image1" Source="xxx.jpg" />


这三种写法是一样的。如果我们把文件放在其他 Web 目录,则必须提供完整路径。

2. Resource Dictionaries

可参考 《WPF 学习笔记 - 4. XAML》《WPF 学习笔记 - 7. Resource》

Silverlight 的逻辑资源字典除了某些功能无法使用外,多数规则和 WPF 一致。

(1) 可以用 x:Name 替代 x:Key。
(2) 资源字典中的对象必须可以在多处被引用(Shared)。
(3) 不支持 FindResource。
(4) 不支持 {DynamicResource}。
(5) FrameworkTemplate 没有 Resources 属性。
(6) 不支持 x:Shared 和 x:Static。
(7) 不支持 <StaticResource ... />

3. Localization

Silverlight 同样支持用资源作为多语言解决方案。

(1) 首先在项目中建一个子目录作为语言资源文件(.resx)的存放位置。



创建默认资源文件(Strings.resx),使用 VS 自带的编辑器打开,将 "Access Modifier" 设置为 "Public"。输入相关字符串,这些字符串拥有一个唯一的名称(Name)。



当 Silverlight 找不到符合当前线程区域信息(CultureInfo) 的语言资源时,默认就会使用该资源文件中的字符串。

相关语言翻译人员,依据这个文件创建相应的语言资源文件,注意 "Access Modifier" 应该设置为 "No code generation",并且文件名必须符合规范。

中文: zh-CN



英文: en-US



(2) 语言资源通常是全局的,因此我们在 App.xaml Resources 中创建逻辑资源。

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="Learn.Silverlight.App"
    xmlns:Lng="clr-namespace:Learn.Silverlight.Resources">
    
    <Application.Resources>
        <Lng:Strings x:Key="Strings" />
    </Application.Resources>
</Application>


注意引入 Namespace,并给一个唯一的 x:Key。

(3) 在 UserControl XAML 文件中使用静态资源绑定。

<UserControl x:Class="Learn.Silverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

        <TextBlock Name="TextBlock1" Text="{Binding a, Source={StaticResource Strings}}"/>
    
    </Grid>
</UserControl>


使用标准绑定语法 "{Binding 字符串名称, Source={StaticResource 资源字典中的 Key}}"。

要在代码中获取资源信息就更简单了,Strings.resx -> Strings.Designer.cs -> Strings 类直接提供了静态属性,让我们可以非常便捷获取资源字符串。

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.TextBox1.Text = Learn.Silverlight.Resources.Strings.a;
}


(4) 试运行看看效果。

在 Application.Startup 事件中指定语言信息。

public partial class App : Application
{
    public App()
    {
        this.Startup += this.Application_Startup;
        this.Exit += this.Application_Exit;
        this.UnhandledException += this.Application_UnhandledException;

        InitializeComponent();
    }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
        Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;

        this.RootVisual = new Grid();
        (this.RootVisual as Grid).Children.Add(new MainPage());
    }

    ...
}


按 F5 运行项目,触发 XamlParseException 异常,提示 "System.MissingMethodException: 没有为该对象定义无参数的构造函数"。这是自动生成文件 Strings.Designer.cs 中 Strings.ctor 的访问权限为 internal 引起的。我们将其修改成 public 即可 (每次修改 Strings.resx 后都需要手工调整成 public)。

public class Strings 
{      
    [SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    public Strings() {}
}


继续,这回能正常运行,只是字符串依然是 Strings.resx 中的,而不是 Strings.zh-CN.resx。很显然,语言指定没有起作用。

打开 .xap 文件,你就会发现问题所在。原来资源资源文件 (.resources.dll) 并没有被打包进去。虽然我们可以手工添加,但每次编译后都要处理,显然很麻烦。解决方法是:关闭 VS,用文本编辑器打开 Silverlight Project File (*.csproj),在 SupportedCultures 中添加语言资源信息。

Learn.Silverlight.csproj

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="...">
    <PropertyGroup>
        ...
        <SupportedCultures>zh-CN;en-US</SupportedCultures>
        ...


重新编译,这回资源文件被顺利添加到 .xap 文件中。

F5 运行,语言切换正常。

-------------

参考:《Localization solution in Silverlight》

转载于:https://www.cnblogs.com/allanxyq/archive/2011/07/19/2110456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值