Windows商店应用程序ResourceLoader在设计时 [英] Windows store app ResourceLoader at design time

查看:83
本文介绍了Windows商店应用程序ResourceLoader在设计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始为Windows 8.1创建Windows Store应用程序,现在遇到了与本地化有关的问题。



我想从显示一个字符串资源。在设计时使用resw文件,但是每次尝试都失败了,即使它在运行时也有效。



使用x:Uid属性时,我仍然必须提供Text属性(即TextBlock的属性),并且我不想重复写两次文本。



我还尝试在viewmodel上为字符串创建一个属性:

 公共字符串标题
{
get {return ResourceLoader.GetForCurrentView( Strings)。GetString( MainView_Title); }
}

此功能在运行时有效,但在设计时为空白。 / p>

所以问题是,有没有办法在XAML设计器中显示.resw文件中的资源?



更具体地说,ResourceManager类是否允许在设计时读取.resw文件?



感谢您的帮助,
Lucas

解决方案

旧方法



因此,您可以做几件事。



第一个(也是最简单的,因为您已经在使用 x:Uid )是仅提供文本进入文本字段。与 x:Uid 相关的值将覆盖其中的所有内容。

 < TextBlock Text = MyText x:Uid = MainView_Title /> 

第二种方法是使用已经拥有的属性,然后检查应用程序是否处于设计时间(通过几种不同的方法),如果是则返回一个常数,如果不是,则返回一个资源。

  public string Title 
{
if(ViewModelBase.IsInDesignTimeStatic)// Mvvm Light的简单访问器
返回我的文本;
返回ResourceLoader.GetForCurrentView( Strings)。GetString( MainView_Title);
}

希望这会帮助您并且编码愉快!



编辑:至少从Windows 8.1开始,似乎有一种新的方法。



新方法




  • 创建一个引用 ResourceLoader 的类(类似于上述属性)。

  • 创建一个索引属性访问器,该访问器接受字符串键并从 ResourceLoader 返回值。

     公共类LocalizedStrings 
    {
    public string this [string key]
    {
    get
    {
    返回App.ResourceLoader.GetForViewIndependentUse()。GetString(key);
    }
    }
    }


  • 在您的 App.xaml ,定义此类型的 StaticResource

     < Application.Resources> 
    < ResourceDictionary>
    < common:LocalizedStrings x:Key = Localized />
    < / ResourceDictionary>
    < /Application.Resources>




现在,当您要访问物业时输入键 MainView_Title 使用此功能。它比较冗长,但应同时在设计器和应用程序本身中进行翻译。

 < TextBlock Text = {Binding Source = {StaticResource Localized},Path = [MainView_Title]} /> 

如果需要,您可以对其进行改编以提高可读性,例如:

 < TextBlock Text = {Binding [MainView_Title],Source = {StaticResource Localized}} /> 


I've started creating a Windows Store App for Windows 8.1 and now I encountered a problem concerning localization.

I would like to display a string resource from a .resw file at design time, but every attempt to do so has failed, even though it works at runtime.

When using the x:Uid attribute, I still have to supply the Text property (i.e. for a TextBlock) and I don't like to write the text twice.

I also tried creating a property for the string on the viewmodel:

public string Title
{
    get { return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title"); }
}

This is working at runtime, but at design time it is blank.

So the question is, is there a way to display resources from a .resw file in the XAML-designer?

More specifically, does the ResourceManager class allow .resw files to be read at design time?

Thanks for your help, Lucas

解决方案

Old Method

So, there are a couple of things you can do.

The first (and simplest, given that you're using x:Uid already) is to just supply the text into the Text field. The x:Uid-related value will overwrite whatever is in there.

<TextBlock Text="MyText" x:Uid="MainView_Title"/>

The second method is to use the property like you already have, and then check to see if the app is in Design Time (through a couple of different methods), then return a constant value if it is and the Resource if it is not.

public string Title
{
     if(ViewModelBase.IsInDesignTimeStatic) //Mvvm Light's easy accessor
         return "My Text";
     return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title");
}

Hope this helps and happy coding!

Edit: There appears to be a new way to do this, at least as of Windows 8.1.

New Method

  • Create a class which references a ResourceLoader (similar to the property described above).
  • Create an indexed property accessor which accepts a string key and return the value from the ResourceLoader.

    public class LocalizedStrings
    {
        public string this[string key]
        {
            get
            {
                return App.ResourceLoader.GetForViewIndependentUse().GetString(key);
            }
        }
    }
    

  • In your App.xaml, define a StaticResource of this type.

    <Application.Resources>
        <ResourceDictionary>
            <common:LocalizedStrings x:Key="Localized"/>
        </ResourceDictionary>
    </Application.Resources>
    

Now, when you want to access your property with entry key MainView_Title, use this. It's more verbose, but it should translate both in the designer and in the app itself.

<TextBlock Text="{Binding Source={StaticResource Localized}, Path=[MainView_Title]}" />

You can shuffle it around to be a bit more readable if you'd like, such as:

<TextBlock Text="{Binding [MainView_Title], Source={StaticResource Localized}}" />

这篇关于Windows商店应用程序ResourceLoader在设计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆