Is there any way to use StaticResource in a WPF control library and be able to view at design-time?

前端 未结 3 1721
无人及你
无人及你 2021-02-04 00:29

I have a WPF Control Library that is being added to a windows forms application. We want to allow the controls to be localizable, however I am not sure how to FULLY accomplish t

3条回答
  •  你的背包
    2021-02-04 00:53

    Here's my partial solution to your problem. I haven't tried to handle loose resources, but I have some success with sharing resources between WinForms and WPF.

    • Create a class library to contain your resources in .ResX files (e.g. Resources.resx, Resources.fr.resx, etc)
    • Create your WPF controls in a WPF user control library
    • Create your WinForms host
    • Reference the resources in your resource library from WPF using the Infralution.Localization.Wpf markup extension and culture manager, e.g.

      
      
    • Put the content of your WPF user controls into one or more resource dictionaries as control templates,e,g

      
          
              
                  
                  
              
      
              
          
      
      
    • Use the resource template in your user controls

      
          
              
                  
                      
                  
              
          
          
      
      
    • Add a couple of lines of code to make things work

      public partial class UserControl1 : UserControl
      {
          // we require a reference to the resource library to ensure it's loaded into memory
          private Class1 _class1 = new Class1();
      
          public UserControl1()
          {
              // Use the CultureManager to switch to the current culture
              CultureManager.UICulture = Thread.CurrentThread.CurrentCulture;
      
              InitializeComponent();
          }
      }
      

    Here's a simple demo app called WindowsFormsHost.7z

提交回复
热议问题