c# - UWP - 来自不同样式资源字典的引用 StaticResource : Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'

标签 c# xaml uwp windows-10-universal uwp-xaml

大家好,我和我的团队最近开始开发一个 win10 uwp 应用程序。应用程序将有很多 View 和组件,因此预计会大量使用样式,因此我们需要通过文件/文件夹结构来组织我们的样式,我们使用以下结构来完成此操作(不幸的是我无法嵌入图像但看到链接):

/image/XvW0u.png

无论如何,我的 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"/>

更多可以引用ResourceDictionary and XAML resource references .

关于c# - UWP - 来自不同样式资源字典的引用 StaticResource : Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284724/

相关文章:

c# - 在构造函数中手动添加 Application Start 事件处理程序

c# - 如何在重建 C# 应用程序时始终生成逐字节相同的 .exe?

wpf - 如何将命令/属性绑定(bind)到 FSharp.ViewModule 中的 PropertyChanged

wpf - 约什史密斯的传奇文章 : I need a bit more on the DataBinding that takes place

c# - 测量标签尺寸

c# - UWP TextBox 在键入时不遵守 TwoWay 绑定(bind)

c# - 根据字段具有不同列数的交叉表/数据透视表查询

c# - UWP MediaCapture-录制WAV音频,采样率16000,16位,单声道

mvvm - UWP 绑定(bind)到 MVVM 中的 AutoSuggestBox

c# - 将 FlowDocument 添加到主窗口