引用其他样式的 UWP MergedDictionary 样式引发错误 [英] UWP MergedDictionary Style that references other style throws error
问题描述
在创建需要使用来自另一个 MergedDictionary
(Brushes.xaml
) 的值的样式合并字典时遇到实际问题.
当我尝试从另一个文件引用它时,出现以下错误:
"未能分配给属性 'Windows.UI.Xaml.ResourceDictionary.Source',因为类型 'Windows.Foundation.String' 无法分配给类型 'Windows.Foundation.Uri'.[行:x 位置: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 中执行)...
<应用程序.资源><资源字典><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></应用程序>
CustomStyles.xaml
包含
例如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 - 不起作用.尝试将顺序更改为 它将注入所选控件引用的资源的所有定义,但未在全局通用 UWP 资源中定义.
最后,当您在合并字典中引用 CustomStyles.xaml
时,包括对 Brushes.xaml
AFTER 的引用,这将使Brusjes.xaml
覆盖您在 CustomStyles.xaml
中包含的 存根 定义.
<资源字典><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="CustomStyles.xaml"/><ResourceDictionary Source="Brushes.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
Having real problems creating a Merged Dictionary of styles that need to use values from another MergedDictionary
(Brushes.xaml
).
When I try and reference it from another file I get the following error:
"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]"
E.g. in a Resource Dictionary I have the following that works..
<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}" />
However, this isn't ideal as I'd need to reference Brushes.xaml
in every style resource dictionary. So what I want to do is declare the Brushes.xaml
in App.xaml
but everything I try results in the error above. It basically does not recognise the resources defined in Brushes.xaml
unless I add them to each individual style.
E.g. What I want (to be able to do in 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
contains
<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>
And e.g. Styles\Button\Default.xaml
contains..
<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}" />
Where CustomBrush1 is defined in Brushes.xaml
.
I've tried so many things e.g. Tried adding Brushes.xaml to the CustomStyles.xaml - doesn't work. Tried changing the order as this states they should be in inverse order - this doesn't work.
Any help is much appreciated, thanks
As per comment, here is the 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>
Whilst this sort of behaviour is by design, you should not be trying to reference external resource dictionaries (styles or themes) directly as this creates a tightly coupled relationship between the two files, so much so that you might as well just make them the one file.
To avoid this, in the header of CustomStyles.xaml
create stub definitions for the resources that you need to reference, this serves a number of purposes:
- You have documented the resources that your resource dictionary is dependent upon
- You can declare the default definition of the required resource, further documenting your custom styles but also making sure that your resources will function, even if the consumer has not referenced the external resources (
Brushes.xaml
)- Which by the way it the reason for the error in the first place.
- You do not have to include the real definition, but as best practise you really should. If the
Brushes.xaml
will be included all the time anyway, then your stubs should be overridden each time, but because we cannot control how or if Brushes.xaml is defined, it is A good idea to include the real and complete definition of the needed resources.
So yes, you could be lazy and insert the entire Brushes.xaml
directly into your CustomStyles.xaml
however you should only insert the resources that CustomStyles.xaml
actually needs
This is the standard method for overriding standard UWP control templates, after a few times and it becomes second nature, Visual studio even does this for you if you right click a control in the designer and choose Edit Template then Edit a Copy...
It will inject all the definitions for resources that are referenced by the selected control, but are not defined in the global generic UWP resources.
Finally, when you reference CustomStyles.xaml
in your merged dictionary, include the reference to Brushes.xaml
AFTER it, this will make Brusjes.xaml
overwrite the stub definitions that you included in CustomStyles.xaml
.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CustomStyles.xaml"/>
<ResourceDictionary Source="Brushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这篇关于引用其他样式的 UWP MergedDictionary 样式引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!