成功引用 XamlReader.Load() 加载的文件中的 ResourceDictionary
我正在构建一个通用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我意识到 XamlReader 能够在指定为绝对路径时解析引用的 XAML 文件时,我寻找指定自己的上下文的可能性。
当我在调用 XamlReader.Load() 时指定 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()