Using one class library (and theme) as basis to another class library

时光毁灭记忆、已成空白 提交于 2019-12-12 03:05:47

问题


Still learning this stuff on WPF, themes, derivations, etc.

I understand basics on the "Themes\Generic.xaml", and then setting your application's app.xaml file to include the resource dictionary pointing to the theme in question.

So, thats fine from an application project perspective. Now, how about from a class library/dll file. I have a DLL project which I want to use as the basis of all controls in my project. In that, I have the Themes/Generic.xaml and have it coded up with some basics to confirm visual design implementation (originally confirmed ok, when tested under an App/exe project).

Now, I want this theme at a level BEFORE the actual application. Again, this is the baseline. Now, I add a second library of custom grouped controls (for example, a user control for address information... multiple address lines, city, state, zip, labels, etc). I want this second library to reference the first library with the themes, so I can see visually at design time what it will look like (alignments, colors, fonts, etc).

What / where should I be looking to let one DLL know about the merge dictionaries that are the basis in the first DLL. Hope this makes sense.

-- EDIT -- for clarification

First Class Library... "MyThemeLibrary" compiles into a .dll In this dll is path/file of "/Themes/MyTheme.xaml"

As suggested by first answer, if I have a Resource Dictionary in the first library, I can reference it in anything else that I will derive from it. So, I have

<ResourceDictionary x:Name="MyGenericTheme"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Themes/MyTheme.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Second Class Library... "SecondLevel" compiles into a .dll In this, I have a user control that I want to put a grid for columns/rows, labels and textbox controls into. I want the controls to respect the colors, fonts, sizes, alignments as defined in the "MyTheme.xaml" of the first dll.

<UserControl x:Class="SecondLevel.multipleControlContainer"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

   <Grid>
      <Grid.ColumnDefinitions>
         <ColumnDefinition />
         <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition />
      </Grid.RowDefinitions>

      <Label Content="Something" 
         Grid.Row="0" Grid.Column="0" />

      <TextBox Text="Testing" Grid.Row="1" Grid.Column="1" />
   </Grid>
</UserControl>

So, how / what should I do the necessary reference, declaration, inclusion of resource dictionary from the first library into the second.


回答1:


make a reference to your dll and if you know where your theme is located you can do this one

<Application x:Class="My.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <!-- Common base theme -->
        <ResourceDictionary Source="pack://application:,,,/Your.Base.Dll;component/YourResDictionary/YourTheme.xaml" />

        <!-- here comes your custom theme -->

      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

do this in the App.xaml

EDIT after clarification (look at the comments)

<UserControl x:Class="SecondLevel.multipleControlContainer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <!-- Common base theme -->
        <ResourceDictionary Source="pack://application:,,,/Your.Base.Dll;component/YourResDictionaryFolder/MyGenericTheme.xaml" />
        <!-- Custom theme -->
        <ResourceDictionary Source="pack://application:,,,/Another.Dll;component/AnotherResDictionaryFolder/MyCustomTheme.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </UserControl.Resources>

  <Grid>
    <!-- all controls in this usercontrol respect the styles in MyGenericTheme.xaml"
         if you use implicite styles-->
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
    </Grid.RowDefinitions>
    <Label Content="Something"
           Grid.Row="0"
           Grid.Column="0" />
    <TextBox Text="Testing"
             Grid.Row="1"
             Grid.Column="1" />

    <!-- if you use explicit styles then you must do this -->

    <TextBox Style="{StaticResource myTextBoxStyle}"
             Text="Testing"
             Grid.Row="1"
             Grid.Column="1" />
  </Grid>
</UserControl>


来源:https://stackoverflow.com/questions/8142049/using-one-class-library-and-theme-as-basis-to-another-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!