【发布时间】:2020-04-14 23:11:31
【问题描述】:
在创建需要使用来自另一个 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。所以我想做的是在App.xaml 中声明Brushes.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 状态它们应该以相反的顺序 - 这不起作用。
非常感谢任何帮助,谢谢
根据评论,这里是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>
【问题讨论】:
-
我觉得这不是合并字典的问题!您的
<x:String ...></x:String>之一必须更改为<x:Uri></x:Uri> -
我不明白 - 哪个字符串?谢谢
-
您能否将您的 Brushes.xaml 和 CustomStyles.xaml 文件上传到任何我可以下载并查看的位置?
-
CustomStyles.xaml在原帖中,我刚刚添加了Brushes.xaml