Windows store app ResourceLoader at design time

后端 未结 2 1823
一个人的身影
一个人的身影 2021-02-14 03:07

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 .res

2条回答
  •  佛祖请我去吃肉
    2021-02-14 03:39

    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.

    
    

    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.

      
          
              
          
      
      

    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.

    
    

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

    
    

提交回复
热议问题