成功引用 XamlReader.Load() 加载的文件中的 ResourceDictionary

发布于 2024-12-19 19:30:22 字数 1070 浏览 2 评论 0原文

我正在构建一个通用的 WP7 程序集,它将显示我的应用程序的通用帮助/有关信息,每个应用程序程序集将指定一对 StackPanel,其中包含一些应用程序特定的信息(我们称为 Legal.xaml 和 WhatsNew.xaml)。

理想情况下,这些应用程序特定的 XAML 文件应采用纯文本形式(而不是在代码中实例化的内容),以便可通过 HTTP 加载或作为嵌入式资源字符串加载。

加载 XAML 工作正常,直到我尝试将某些样式定义分解到另一个文件中,然后 XamlReader.Load() 失败并显示以下注释:“属性 AboutPageDocs/CommonStyles.xaml 值超出范围。 [行:43 位置:45]”

加载 Legal.xaml 时会发生该错误,当我们环顾四周时,如 43 所示,我们发现我试图加载现在包含自定义样式的 ResourceDictionary:

<StackPanel.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="AboutPageDocs/CommonStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</StackPanel.Resources>

这是错误...如果只是复制 &粘贴 StackPanel 代码(在运行时动态加载)并将其放入 UserControl 中...一切正常。

无需在 Legal.xaml 和 Legal.xaml 中内联定义我的样式。 WhatsNew.xaml ...有什么方法可以让 XamlReader.Load() 属性查找 CommonStyles.xaml 吗?

考虑到源路径不正确,我尝试通过两个程序集将 CommonStyles.xaml 的副本放置在不同位置...并尝试使用 pack:// uri 语法...到目前为止都无济于事。

我缺少什么?

I am building a common WP7 assembly which will display common help/about information for my apps, each app assembly will specify a pair of StackPanels which have some of the app specific information (well call em Legal.xaml and WhatsNew.xaml).

Ideally these app specific XAML files should be in a plaintext form (vs something that is instantiated in code) so loadable via HTTP or as an embedded resource string.

Loading the XAML works fine, until I try to break out some of the style definitions into another file, then XamlReader.Load() fails with a note that: “Attribute AboutPageDocs/CommonStyles.xaml value is out of range. [Line: 43 Position: 45]”

That error would happen when loading Legal.xaml, which when we look around like 43 we find where I am attempting to load the ResourceDictionary that now contains the custom styles:

<StackPanel.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="AboutPageDocs/CommonStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</StackPanel.Resources>

Here is the bugger... if simply copy & paste the StackPanel code (which is being loaded dynamically at runtime) and drop it into a UserControl... things work fine.

Short of having to define my styles inline in both Legal.xaml & WhatsNew.xaml... is there any way to have XamlReader.Load() property lookup CommonStyles.xaml?

On the thought that the Source path was not correct, I have tried placing copies of CommonStyles.xaml in various locations through both assemblies... as well as experimented with the pack:// uri syntax... all to no avail thus far.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2024-12-26 19:30:22

当我意识到 XamlReader 能够在指定为绝对路径时解析引用的 XAML 文件时,我寻找指定自己的上下文的可能性。

当我在调用 XamlReader.Load() 时指定 ParserContext 时,我发现这对我有用

public static FlowDocument ReadFlowDocument( FileInfo xamlFile )
{
    // Specify a ParserContext.
    // It's important to set BaseUri to the file itself
    // not to its parent direcory!
    ParserContext parserContext = new ParserContext();
    parserContext.BaseUri = new Uri( xamlFile.ToString() );

    // Create a stream from this file
    FileStream stream = new FileStream( xamlFile.ToString(), FileMode.Open );

    // Let the XamlReader load and parse the XAML. It will resolve any referenced ResourceDirectories
    // specified with a relative path
    return (FlowDocument) XamlReader.Load( stream, parserContext );
}

As I realized that XamlReader is able to resolve referenced XAML files when they are specified as absolute paths, I looked for a possibility to specify an own context.

I found this working for me, when I specify a ParserContext when calling XamlReader.Load()

public static FlowDocument ReadFlowDocument( FileInfo xamlFile )
{
    // Specify a ParserContext.
    // It's important to set BaseUri to the file itself
    // not to its parent direcory!
    ParserContext parserContext = new ParserContext();
    parserContext.BaseUri = new Uri( xamlFile.ToString() );

    // Create a stream from this file
    FileStream stream = new FileStream( xamlFile.ToString(), FileMode.Open );

    // Let the XamlReader load and parse the XAML. It will resolve any referenced ResourceDirectories
    // specified with a relative path
    return (FlowDocument) XamlReader.Load( stream, parserContext );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文