创建需要使用其他值的样式合并字典时遇到实际问题MergedDictionary
(Brushes.xaml
).
当我尝试从另一个文件引用它时,出现以下错误:
"Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: x Position: y]"
例如。在资源词典中我有以下有效的内容..
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource customBrush1}" />
<Setter Property="Foreground" Value="{StaticResource customBrush2}" />
但是,这并不理想,因为我需要参考Brushes.xaml
在每个样式资源字典中。所以我想做的是声明Brushes.xaml
in App.xaml
但我尝试的一切都会导致上述错误。它基本上不识别中定义的资源Brushes.xaml
除非我将它们添加到每个单独的样式中。
例如。我想要什么(能够在 App.xaml 中执行操作)...
<Application x:Class="Test.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Test.Xamarin.Forms.Themes.UWP/Brushes.xaml" />
<ResourceDictionary Source="ms-appx:///Test.Xamarin.Forms.Themes.UWP/CustomStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
CustomStyles.xaml
包含
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\Button\BackButton.xaml" />
<ResourceDictionary Source="Styles\Button\Default.xaml" />
<ResourceDictionary Source="Styles\Button\HyperlinkBasicButton.xaml" />
<ResourceDictionary Source="Styles\Button\HyperlinkButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
以及例如Styles\Button\Default.xaml
包含..
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource CustomBrush1}" />
<Setter Property="Foreground" Value="{StaticResource CustomBrush2}" />
其中 CustomBrush1 定义于Brushes.xaml
.
我尝试过很多事情,例如尝试将 Brushes.xaml 添加到 CustomStyles.xaml - 不起作用。尝试将顺序更改为this https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/resourcedictionary-and-xaml-resource-references声明它们应该按相反的顺序 - 这是行不通的。
非常感谢任何帮助,谢谢
根据评论,这是Brushes.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="CustomBrush1"
Color="Black}" />
<SolidColorBrush x:Key="CustomBrush2"
Color="White}" />
</ResourceDictionary>