使用一个类库(和主题)作为另一个类库的基础

发布于 2024-12-15 17:18:32 字数 1893 浏览 2 评论 0原文

仍在学习有关 WPF、主题、派生等的内容。

我了解“Themes\Generic.xaml”的基础知识,然后设置应用程序的 app.xaml 文件以包含指向相关主题的资源字典。

因此,从应用程序项目的角度来看,这很好。现在,如何从类库/dll 文件中获取。我有一个 DLL 项目,我想将其用作项目中所有控件的基础。其中,我有 Themes/Generic.xaml 并用一些基础知识对其进行编码以确认视觉设计实现(最初确认没问题,在 App/exe 项目下测试时)。

现在,我希望这个主题处于实际应用之前的水平。再说一次,这是基线。现在,我添加第二个自定义分组控件库(例如,用于地址信息的用户控件...多个地址行、城市、州、邮政编码、标签等)。我希望第二个库引用第一个库的主题,这样我就可以在设计时直观地看到它的外观(对齐方式、颜色、字体等)。

我应该在什么/哪里让一个 DLL 了解作为第一个 DLL 基础的合并字典。希望这是有道理的。

-- 编辑 -- 为了澄清

一流库...“MyThemeLibrary”编译成 .dll 在这个dll中是“/Themes/MyTheme.xaml”的路径/文件

正如第一个答案所建议的,如果我在第一个库中有一个资源字典,我可以在我将从它派生的任何其他内容中引用它。所以,我有

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

二级库...“SecondLevel”编译成 .dll 在此,我有一个用户控件,我想将列/行、标签和文本框控件的网格放入其中。我希望控件遵循第一个 dll 的“MyTheme.xaml”中定义的颜色、字体、大小、对齐方式。

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

那么,我应该如何/做什么进行必要的引用、声明、将资源字典从第一个库包含到第二个库中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-12-22 17:18:33

引用你的 dll,如果你知道你的主题位于哪里,你可以

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

在 App.xaml

EDIT 澄清后执行此操作(查看评论)

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文