class 库中的全局可访问样式
Globally accessible style in class library
我有一个 class 库,我在其中定义(基本上是扩展)一些控件,例如 TextBox
、Button
等。我也在使用 MaterialDesignInXamlToolkit
用于样式化控件。所以我的 class 库本质上将包含具有我自己的扩展功能的控件,它们看起来像 MaterialDesignInXamlToolkit
.
中定义的样式
现在我的问题是,由于我在class库项目中没有App.xaml
,我应该在哪里编写XAML代码来导入[=15=的样式],以便将它们应用于我的扩展控件? class 库中的什么地方可以指定可全局访问并应用于所有控件的样式?
我对此进行了搜索,但没有找到我想要的内容。请帮忙。
更新:这是我的代码(不工作)。
MaterialTextBox.cs
using System.Windows.Controls;
namespace MaterialControls
{
public class MaterialTextBox : TextBox
{
... some extra features here (no XAML file for this class, just this .cs)...
}
}
Themes.xaml(这将包含所有全局样式)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialControls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary>
<Style TargetType="local:MaterialTextBox">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Height" Value="100"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
现在我希望将这些样式应用于 MaterialTextBox
,以便无论我在哪里使用它,它都应该具有开箱即用的外观和功能。
What is the place in class library where you can specify styles which are globally accessible and are applied to all the controls?
真的有none。在单个资源字典中,您可以使用 <ResourceDictionary.MergedDictionaries>
来导入您在资源字典中定义的资源所基于的资源,例如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication8">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="...">
<!-- style based on MaterialDesignTheme -->
</Style>
</ResourceDictionary>
但是在 class 库中没有 App.xaml
或某种 "global resource cache" 的概念。
找到解决方案。
我使用的是 Class Library
项目,而实际上我应该使用 WPF Custom Control Library
项目。这里的项目类型很重要,否则你将不得不使用 .csproj
文件来使其工作。
所以现在创建了一个新的 WPF Custom Control Library
项目(New Project > Windows > Classic Desktop > WPF Custom Control Library
模板)。此项目有 Themes\Generic.xaml
个文件,该文件将用作样式的默认位置。
我有一个 class 库,我在其中定义(基本上是扩展)一些控件,例如 TextBox
、Button
等。我也在使用 MaterialDesignInXamlToolkit
用于样式化控件。所以我的 class 库本质上将包含具有我自己的扩展功能的控件,它们看起来像 MaterialDesignInXamlToolkit
.
现在我的问题是,由于我在class库项目中没有App.xaml
,我应该在哪里编写XAML代码来导入[=15=的样式],以便将它们应用于我的扩展控件? class 库中的什么地方可以指定可全局访问并应用于所有控件的样式?
我对此进行了搜索,但没有找到我想要的内容。请帮忙。
更新:这是我的代码(不工作)。
MaterialTextBox.cs
using System.Windows.Controls;
namespace MaterialControls
{
public class MaterialTextBox : TextBox
{
... some extra features here (no XAML file for this class, just this .cs)...
}
}
Themes.xaml(这将包含所有全局样式)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialControls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary>
<Style TargetType="local:MaterialTextBox">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Height" Value="100"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
现在我希望将这些样式应用于 MaterialTextBox
,以便无论我在哪里使用它,它都应该具有开箱即用的外观和功能。
What is the place in class library where you can specify styles which are globally accessible and are applied to all the controls?
真的有none。在单个资源字典中,您可以使用 <ResourceDictionary.MergedDictionaries>
来导入您在资源字典中定义的资源所基于的资源,例如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication8">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="...">
<!-- style based on MaterialDesignTheme -->
</Style>
</ResourceDictionary>
但是在 class 库中没有 App.xaml
或某种 "global resource cache" 的概念。
找到解决方案。
我使用的是 Class Library
项目,而实际上我应该使用 WPF Custom Control Library
项目。这里的项目类型很重要,否则你将不得不使用 .csproj
文件来使其工作。
所以现在创建了一个新的 WPF Custom Control Library
项目(New Project > Windows > Classic Desktop > WPF Custom Control Library
模板)。此项目有 Themes\Generic.xaml
个文件,该文件将用作样式的默认位置。