我正在构建一个通用的 WP7 程序集,它将显示我的应用程序的通用帮助/有关信息,每个应用程序程序集将指定一对 StackPanel,其中包含一些应用程序特定的信息(我们称为 Legal.xaml 和 WhatsNew.xaml)。
\n\n理想情况下,这些应用程序特定的 XAML 文件应采用纯文本形式(而不是在代码中实例化的内容),以便可通过 HTTP 加载或作为嵌入式资源字符串加载。
\n\n加载 XAML 工作正常,直到我尝试将某些样式定义分解到另一个文件中,然后 XamlReader.Load() 失败并显示以下注释: \xe2\x80\x9cAttribute AboutPageDocs/CommonStyles.xaml 值超出范围。[行:43 位置:45]\xe2\x80\x9d
\n\n加载 Legal.xaml 时会发生该错误,当我们环顾四周时,如 43,我们发现我试图加载现在包含自定义样式的 ResourceDictionary:
\n\n<StackPanel.Resources>\n <ResourceDictionary>\n <ResourceDictionary.MergedDictionaries>\n <ResourceDictionary Source="AboutPageDocs/CommonStyles.xaml"/>\n </ResourceDictionary.MergedDictionaries>\n </ResourceDictionary>\n</StackPanel.Resources>\nRun Code Online (Sandbox Code Playgroud)\n\n这是错误的...如果简单地复制并粘贴 StackPanel 代码(在运行时动态加载)并将其放入 UserControl 中...一切工作正常。
\n\n不需要在 Legal.xaml 和 WhatsNew.xaml 中内联定义我的样式...有没有办法让 XamlReader.Load() 属性查找 CommonStyles.xaml?
\n\n考虑到源路径不正确,我尝试通过两个程序集将 CommonStyles.xaml 的副本放置在不同位置...并尝试使用 pack:// uri 语法...到目前为止都无济于事。
\n\n我缺少什么?
\n我有一个资源字典,它是在其上定义的数据网格样式以及该数据网格样式内的样式。
<Style TargetType="{x:Type DataGrid}" x:Key="CatalogDataGrid">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}" x:Key="RightAlignedDataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border
Padding="5,0"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)
然后在我的 XAML 中我尝试使用RightAlignedDataGridCell以使我的列右对齐。
<DataGrid... Style="{StaticResource CatalogDataGrid}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
</DataGridTextColumn>
<DataGridTextColumn Header="Total" Binding="{Binding Total}"
CellStyle="{StaticResource RightAlignedDataGridCell}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我收到资源未找到异常。如果我将该样式放在资源字典根上。它可以工作。但我想RightAlignedDataGridCell留在
<Style.Resources>里面CatalogDataGrid。
如何RightAlignedDataGridCell在我的 XAML 上使用它而不将其移动到资源字典根目录?
提前致谢。
我想将PresentationFramework.Aero 主题添加到我的ResourceDictionary 中。ResourceDictionary 本身位于一个名为 ProjectResources 的程序集中。在此程序集中,我定义了项目的所有样式。所以这个 ResourceDictionary 被我的项目中的几个不同的程序集使用。到目前为止,它对于我定义的所有词典都运行良好。
现在我想添加 Aero 主题。但我总是遇到例外:
PresentationFramework.dll 中发生“System.Windows.Markup.XamlParseException”类型的未处理异常
内部异常:
{“无法加载文件或程序集“PresentationFramework.Aero,Culture=neutral”或其依赖项之一。系统找不到指定的文件。”:“PresentationFramework.Aero,Culture=neutral”}
ResourceDictionary 的定义如下:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/ImageDefinition.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/Brushes.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesLabel.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesTextBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesButton.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesTabControl.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesTextBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesBorder.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesListView.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesWindow.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesIndicator.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesSearchBox.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ProjectResources;component/ProjectResources/Dictionaries/StylesTreeView.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我还添加了对PresentationFramework.Aero.dll 的引用。
所以我不明白为什么它找不到定义的主题。这个定义有什么问题呢?除了这个定义之外,我是否还错过了其他任何能够使用主题的东西?
已解决: 正如图米尔在评论中所说。您需要设置参数
复制本地 = true
的PresentationFramework.Aero DLL。通过这样做,dll 将被复制到您的程序的根目录中,并且它将 dll.dll 文件复制到您的程序的根目录中。
我需要动态更改ResourceDictionary文件App.xaml。我尝试过以下代码:
ResourceDictionary newRes = new ResourceDictionary();
newRes.Source = new Uri("/PsyboInventory;component/TitleBarResource.xaml", UriKind.RelativeOrAbsolute);
this.Resources.MergedDictionaries.Clear();
this.Resources.MergedDictionaries.Add(newRes);
Run Code Online (Sandbox Code Playgroud)
没有错误,但是主题没有改变
我正在寻找一种ResourceDictionary在项目之间共享的方法。
将新项目添加到共享项目不提供资源字典。它可以在其他(主)项目中创建并拖动。但是我无法将其构建选项更改为Page:
这个想法是像这样加载资源字典
var dictionary = new ResourceDictionary();
dictionary.Source = new Uri("/WpfApplication91;component/Dictionary2.xaml", UriKind.Relative);
Run Code Online (Sandbox Code Playgroud)
目前这显然失败了
“System.IO.IOException”类型的异常出现在PresentationFramework.dll中,但未在用户代码中处理
附加信息:无法找到资源“dictionary2.xaml”。
有任何想法吗?
我发现了这篇关于提高ResourceDictionaries网络性能的文章,但问题是它已经很旧了(2011 年)。我正在考虑实现这样的东西,但我想知道 Microsoft 是否没有在 .NET Framework 的最新版本中修复此问题。关于这个话题我有几个问题,希望这里有人能给出答案:
ResourceDictionaries通过创建它们的多个实例(每次将它们分配给控件时一个实例)来进行管理?"CustomButtonStyle"例如,当我在ResourceDictionary名为 的"ButtonStyles"程序集中调用样式,然后在应用程序中"StylesAssembly"引用该样式,并为其分配了 20 个时,这甚至是一个问题吗?App.xamlButtonsCustomButtonStyle"ButtonStyles" ResourceDictionary?运行 WPF 应用程序时,我在输出窗口中看到许多警告消息:
System.Windows.ResourceDictionary Warning: 9 : Resource not found
Run Code Online (Sandbox Code Playgroud)
然而,与我在 Stack Overflow 上看到的所有相关问题不同(比如这个),这些警告都没有告诉我哪个资源键(或键)是/导致了问题。
有没有办法让 Visual Studio 告诉我这些信息?我曾尝试更改调试选项中的 WPF 跟踪设置,但这些都没有效果。奇怪的是,即使相关的跟踪设置设置为“关闭”,我仍然收到资源字典警告。
背景资料
我正在使用一个名为Stylet的 MVVM 库,当一个视图切换到另一个视图时,这些警告似乎会出现。除了轻微(但明显)的延迟外,警告似乎不会影响应用程序的运行。
也就是说,我怀疑警告更有可能是由于另一个库Material Design 引起的,因为我在开发后期添加了这一点,直到在那之后才记得看到警告。
我的代码分为两个不同的 C# 项目:一个用于 UI 内容(控件、样式、转换器、画笔等),另一个用于主应用程序。UI 项目引用了 Stylet 和 Material Design NuGet 包;UI 项目然后由主应用程序项目引用。这两个项目都是 .Net Core 3.1。
我有一个ResourceDictionary只包含字符串键和字符串值.现在我想要一个具有相同内容的Dictionary <string,string>.
你会怎么做?什么是C#中最快的解决方案?
编辑:性能最快;)
我正在处理的应用程序有2个ResourceDictionary,DefaultStyles.xaml和CustomStyles.xaml.
CustomStyles字典中的样式是否可能使用其他字典中定义的基本样式?
DefaultStyles.xaml:
<Style x:Key="TextBlockDefaultStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="4" />
</Style>
Run Code Online (Sandbox Code Playgroud)
CustomStyles.xaml:
<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}">
<Setter Property="FontSize" Value="16" />
</Style>
Run Code Online (Sandbox Code Playgroud)
App.xaml中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles/DefaultStyles.xaml"/>
<ResourceDictionary Source="Assets/Styles/CustomStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
代码运行时,抛出以下异常:
找不到具有Name/Key TextBlockDefaultStyle的资源.
如果两个样式都在同一个文件中,它的效果很好.
我在外部有StoryBoard ResourceDictionary.我可以成功地回顾这个故事板,但我不能签署Completed活动
我得到了InvalidOperationException:
"System.Windows.Media.Animation.Storyboard"类型的指定值必须将IsFrozen设置为false才能进行修改.