![](/img/trans.png)
[英]Using ResourceDictionary.MergedDictionaries in App.xaml
[英]ResourceDictionary.MergedDictionaries causes weird errors
在我的WPF應用程序內部,我包括另一個項目的ResourceDictionary。
<Application x:Class="namespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This Line causes an Error -->
<ResourceDictionary Source="pack://application:,,,/Commons;Component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
元數據覆蓋和基本元數據必須是相同類型或派生類型。
該解決方案可以成功構建並運行。
重新啟動Visual Studio無法修復它。
剪切和粘貼<ResourceDictionary Source="..." />
行會導致另一個錯誤,如注釋中的此處所述: Value Cannot be Null. Parameter Name: item
Value Cannot be Null. Parameter Name: item
。 重新啟動Visual Studio將帶回舊錯誤。
可悲的是,我還沒有找到如何重現此錯誤的方法,我只能告訴您一些有關環境的信息:
盡管我對此表示懷疑,但這些都是與我的問題有關的,這里是我安裝的插件:
Sinatr的評論提示我閱讀有關主題的更多信息。
ThemeInfo
在自定義控件庫中,在AssemblyInfo.cs
自動創建了ThemeInfoAttribute
[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
正如它在自動生成的注釋中指出的那樣,第一個參數是確定是否存在主題或特定於主題的資源字典。 第二個參數定義在哪里存在或在何處找到通用ResourceDictionary
( Generic.xaml
)。
ResourceDictionaryLocation
枚舉 ResourceDictionaryLocation
-Enumeration本身用於指定這些詞典的位置。
ResourceDictionaryLocation.None
不存在主題詞典。
ResourceDictionaryLocation.SourceAssembly
程序集中存在主題字典,用於定義主題的類型。 這期望
ResourceDictionary
位於/Themes
-Folder中。 稍后解釋。
ResourceDictionaryLocation.ExternalAssembly
主題詞典存在於定義主題類型的程序集之外的程序集中。
我不會解釋它是如何工作的。
/Themes
夾 可悲的是我找不到太多的東西。 如果有人有更多信息,請分享。
您是否想過,如何應用無外觀控件的樣式?
如果創建了一個外觀難看的控件,他將執行以下操作:
public class MyControl : ControlTemplate
{
static MyControl()
{
// This tells WPF to search for a Style for this type
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl)),
new FrameworkPropertyMetadata(typeof(MyControl)));
}
}
簡而言之,通過搜索Logical-Tree
,然后在Application
的Resources中,最后在sth中,可以找到WPF中的資源。 他們稱為System-Area
(如果您知道更好的話,這是我的德語翻譯)。
因此,取決於ThemeInfo
, MyControl
可能在/Themes
-Folder內部的ResourceDictionary
具有其樣式,例如。 /Themes/Generic.xaml
。 並告訴 WPF的Ressources添加到System-Area
這最終導致自動解決相應的樣式。
在/Themes/Generic.xaml
內部的某個地方:
<Style TargetType="{x:Type MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControl}">
..
</ControlTemplate/>
</Setter.Value>
</Setter>
</Style>
這就是為什么上述
ThemeInfoAttribute
要求Generic.xaml
將位於/Themes
-文件夾。 -而且,即使在我的情況下,即使該通用文件都沒有使用System-Area
Functionality,也會導致這些錯誤。 但是我無法找出原因。
資料來源:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.