我一直在试图弄清楚如何组织我的ResourceDictionary文件以便重用并与我团队的其他成员共享.
我一直遇到"Generic.xaml",但如果我在MSDN上查看Generic.xaml或者只是进行谷歌搜索,我似乎只会发现博客文章和论坛问题 - 我似乎无法打击任何真正权威和清晰的东西.
Generic.xaml和MyRandomlyNamedResourceDictionary.xaml有什么区别?看起来无论哪种方式,我都必须使用Source属性引用存储在库中的ResourceDictionaries.例如,:
<Application.Resources>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml"
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
那么Generic.xaml有什么优势呢?如果我不试图给我的应用程序多个"外观"(即,如果我只有一个主题),它是否有任何目的?
我试图在我的app.xaml中为我的WPF Windows应用程序中的每个窗口设置默认样式.到目前为止,我在app.xaml中有这个:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="Background" Value="Blue" />
</Style>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
通过专门告诉窗口通过以下方式使用此样式,我可以在运行应用程序(但不是VS设计器)时让窗口显示此样式:
Style="{DynamicResource WindowStyle}
Run Code Online (Sandbox Code Playgroud)
这有效,但并不理想.那么我该怎么做:
谢谢!
我似乎无法将合并的字典添加到XAML中的合并字典集合中.
Theme.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
<ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
应用资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>
<!--
<ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>
<ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
注意:如果我将两个ResourceDictionaries放在Appication.Resources MergedDictionary中(注释掉theme.xaml并取消注释其他两个词典),它们都会正确加载.但是,我们的资源定义方式,这可能意味着将加载相当多的资源,而对于动态加载,我希望能够定义模板.
我有一个WPF .net 4.5应用程序,我在合并资源字典时遇到问题.
我有与此问题和本问题完全相同的问题,但接受的解决方案对我不起作用.
我在app.xaml中声明了一个资源字典,如下所示(为简洁起见,简化):
<Application.Resources>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skin/ResourceLibrary.xaml" />
<ResourceDictionary Source="Skin/Brushes/ColorStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
问题: 应用程序可以在app.xaml中列出时"查看"ColorStyles dictonary,但是如果我将其移动/嵌套在ResourceLibrary.xaml中,则应用程序不会"看到"ColorStyles.xaml以及有关缺少静态资源的错误出现.
以下是我创建ResourceLibrary.xaml字典的方法(简化):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<!-- BRUSHES AND COLORS -->
<ResourceDictionary Source="Brushes/ColorStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
更改原因:我当前的资源字典组织很糟糕,我需要更改它(因为我不止一次创建对象).我希望在"Skin"文件夹中有一个资源字典,然后在子文件夹中组织剩余的样式字典,这些字典将全部合并到ResourceLibrary.xaml文件中,而后者将在app.xaml中调用.
我尝试过: 是的我尝试使用上面链接中的解决方案:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skin/ResourceLibrary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Dummy Style, anything you won't use goes -->
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
但我在虚拟样式行上得到以下错误:
错误2属性元素不能位于元素内容的中间.它们必须在内容之前或之后.
由于lisp注释,将代码更改为以下内容消除了上述错误:
<Application.Resources>
<ResourceDictionary>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<!-- Dummy Style, anything …Run Code Online (Sandbox Code Playgroud) 我在app.xaml中有一个DataTemplate,它将视图绑定到viewmodel.
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
如果没有样式,则不会应用上述模板.我放风格的那一刻,像...
<Application.Resources>
<DataTemplate DataType="{x:Type vm:someviewmodeltype}">
<vw:somevwcontrol />
</DataTemplate>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
datatemplate被拿起并产生预期的结果......我不确定那里发生了什么......有人可以澄清一下吗?
谢谢.
为了优化我的应用程序,我创建了一个SharedResourceDictionary.有了这个,我在运行时节省了大约250 MB的内存,所以它运行良好.
我的问题出在设计时,在Blend 4中,我无法访问我的资源.要修改模板(女巫在我的资源文件中),我通常右键单击我的控件,然后选择编辑模板 - >编辑当前,如下图所示:

我需要修改我的模板,它比我的资源文件快100倍,找到好的...我有很多资源......
我的SharedResourceDictionary是这样实现的(在网上找到):
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get
{
if (IsInDesignMode)
return base.Source;
return _sourceUri;
}
set
{ …Run Code Online (Sandbox Code Playgroud) TLDR;
屏幕ResourceDictionary在运行时正确地引用VS2015样式的外部文件,但不是在设计时.是什么赋予了?
在工作中,我们有一个WinForms产品,其中包含许多 WinForms屏幕,一个开发人员主动添加新的屏幕,以及一些WPF屏幕,我添加了新的.在现有的WPF屏幕中注意到很多重复的代码/样式,我创建了一个单独的项目来容纳这个 - 由所有现有/未来的WPF屏幕引用.
项目: WpfHelper
WpfHelper.dll 部署到 ...\Trunk\Project\Externals...\Trunk\Utilities\WpfHelper\WpfHelper\Resources\GlobalResources.xaml
我已...\Trunk\Project\Externals\WpfHelper.dll在六个项目中引用,将以下代码添加到每个资源文件中:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfHelper;Component/Resources/GlobalResources.xaml" />
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
所有屏幕都位于...\Trunk\Project\Plugins.
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? ? Resource Works? ? Platform Target ? Target Framework ? Reference File Path ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Project1 ? Succeeded ? Any CPU ? .NET 4.6 ? ...\Project1\Project1\Resources\Resources.xaml ?
? Project2 ? Succeeded ? x86 ? .NET 4.6 ? ...\Project2\Project2\Resources\Resources.xaml ?
? Project3 ? Succeeded ? Any …Run Code Online (Sandbox Code Playgroud) 我最近升级到VS 2012,因为我需要开始使用.net 4.5,但这不仅仅是重点.我的问题如下:
我ResourceDictionary在我的主要项目中有一个叫做AppStyles.xaml,并且App.Xaml我有以下内容:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AppStyles.xaml"/>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在我ResourceDictionary有一个样式,然后我可以通过设置将此样式应用于我的任何项目中的任何按钮Style={StaticResource MyButton}.
升级到VS 2012之后,我注意到我在标题中出现了很多错误,有时它会阻止我进行调试,有时却没有!
有没有不同的方式我应该这样做或VS 2012中有问题?
如果我多次使用MergedDictionaries定义样式,它在运行时不起作用,但在VS2010的WPF Designer中它可以工作.如果在运行时使用代码加载MergedDictionaries,它也可以工作.
为什么会这样?这只是我的问题还是?以及如何解决?
I am using WPF4 and loading themes/styles from an assembly at application level.
Run Code Online (Sandbox Code Playgroud)
不工作
<!--Application.xaml-->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<!--Theme.xaml-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/Shared.xaml" />
<ResourceDictionary Source="Theme/Button.xaml" />
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
工作
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Shared.xaml" />
<ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Button.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud) wpf ×9
xaml ×6
.net ×2
c# ×2
styles ×2
themes ×2
app.xaml ×1
c#-3.0 ×1
datatemplate ×1
performance ×1
silverlight ×1