大家好,我和我的团队最近开始开发一个 win10 uwp 应用程序。应用程序将有很多 View 和组件,因此预计会大量使用样式,因此我们需要通过文件/文件夹结构来组织我们的样式,我们使用以下结构来完成此操作(不幸的是我无法嵌入图像但看到链接):
无论如何,我的 Resource.xaml 合并所有其他词典如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Colors.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
<ResourceDictionary Source="/Resources/Fonts.xaml" />
<ResourceDictionary Source="/Resources/Converters.xaml" />
<ResourceDictionary Source="/Resources/Buttons.xaml" />
<ResourceDictionary Source="/Resources/RadioButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
在我的 App.xaml 中,我引用了这本字典:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
现在我设法在我的 RadioButton.xaml 中找到问题的根源,我使用 StaticResource 查找引用了 Colors.xaml 中定义的画笔:
<Setter Property="Foreground" Value="{StaticResource TopMenuTextBrush}" />
如果我删除这一行,一切都会开始,但有了它我会得到以下异常:
- Exception {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
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: 28 Position: 37]} System.Exception {Windows.UI.Xaml.Markup.XamlParseException}
有趣的是,当我启动应用程序并注释掉此行并取消注释时,visual studio 将识别画笔并正确应用它,它只会在应用程序启动时中断。 我们之前在开发 WPF 时使用了相同的方法,所以我认为它可能与应用程序部署有关。
非常感谢所有帮助。
最佳答案
Exception = {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
问题是您使用了错误的 ResourceDictionary
来源。我发现 Resources.xaml
和其他 xaml 文件存储在您的屏幕截图中的同一级别目录中。所以你不能在 source 中声明这些 xaml 文件的父目录。请像下面这样修改 ResourceDictionary
<ResourceDictionary Source="Colors.xaml"/>
关于c# - UWP - 来自不同样式资源字典的引用 StaticResource : Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284724/