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

前端 未结 3 1714
无人及你
无人及你 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 01:15

    I've run into this problem once, and I resolved it by dropping the whole "Resources are objects indexed by key in canonical dictionaries" thing.

    I mean, the simple fact of defining a resource in one project and referencing it in another by it's "key" should give goosebumps to any sane person. I wanted strong references.

    My solution to this problem was to create a custom tool that converts my resource xaml files to static classes with a property for each resource:

    So MyResources.xaml:

    
      
      
    
    

    Becomes MyResources.xaml.cs

    public static class MyResources {
    
      static MyResources() {
        // load the xaml file and assign values to static properties
      }
    
      public static SolidColorBrush LightBrush { get; set; }
      public static SolidColorBrush DarkBrush { get; set; }
    
    }
    

    For referencing a resource, you can use the x:Static instead of StaticResource:

    
    

    Now you got strong references, autocompletion and compile time check of resources.

提交回复
热议问题