wpf从另一个文件夹/命名空间加载资源
如何从同一项目中的另一个文件夹加载资源(例如样式资源)?有什么想法吗?wpf从另一个文件夹/命名空间加载资源,wpf,xaml,namespaces,Wpf,Xaml,Namespaces,如何从同一项目中的另一个文件夹加载资源(例如样式资源)?有什么想法吗? 我的示例如下所示,我希望在ButtonTest.xaml中使用ButtonStyles.xaml。在设计模式中,在启动应用程序之前。使用App.xaml中的资源在运行时非常有效。将自定义样式添加到所需文件夹的ResourceDictionary中- 在我的示例中,文件是/Resources/Themes/Button.xaml。为样式指定一个键: <Style x:Key="MyButtonStyle" TargetT
我的示例如下所示,我希望在ButtonTest.xaml中使用ButtonStyles.xaml。在设计模式中,在启动应用程序之前。使用App.xaml中的资源在运行时非常有效。将自定义样式添加到所需文件夹的ResourceDictionary中- 在我的示例中,文件是/Resources/Themes/Button.xaml。为样式指定一个键:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
// define your style, for example
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Style>
//例如,定义您的样式
如果您希望添加多个样式文件,最好合并ResourceDictionary,
例如,导入名为Generic.xaml的文件:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/Resources/Themes/Button.xaml"/>
<ResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/[path and file name]"/>
// add all your resourcedictionaries
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
//添加所有资源字典
在要使用样式的位置,将合并的ResourceDictionary添加为资源。例如,在UserControl中,
添加如下内容:
<UserControl.Resources>
<inf:DesignTimeResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/Resources/Themes/Generic.xaml"/>
</UserControl.Resources>
然后,当您添加按钮时,可以使用您提供的键将样式用作动态资源
<Button Style="{DynamicResource MyButtonStyle}"/>
确保更改所有名称以匹配项目和文件名。将自定义样式添加到所需文件夹的ResourceDictionary中- 在我的示例中,文件是/Resources/Themes/Button.xaml。为样式指定一个键:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
// define your style, for example
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Style>
//例如,定义您的样式
如果您希望添加多个样式文件,最好合并ResourceDictionary,
例如,导入名为Generic.xaml的文件:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/Resources/Themes/Button.xaml"/>
<ResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/[path and file name]"/>
// add all your resourcedictionaries
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
//添加所有资源字典
在要使用样式的位置,将合并的ResourceDictionary添加为资源。例如,在UserControl中,
添加如下内容:
<UserControl.Resources>
<inf:DesignTimeResourceDictionary Source="pack://application:,,,/PROJECT_NAME;component/Resources/Themes/Generic.xaml"/>
</UserControl.Resources>
然后,当您添加按钮时,可以使用您提供的键将样式用作动态资源
<Button Style="{DynamicResource MyButtonStyle}"/>
确保更改所有名称以匹配项目和文件名