为什么 XamlReader.Load 无法识别 Silverlight 4 中的附加属性?
我试图弄清楚如何在 Silverlight 4 应用程序中以编程方式在运行时应用主题。我认为这应该像从 XAML 加载资源字典并将其与应用程序的合并字典合并一样简单。到目前为止,这是我的代码:
var themeUri = new Uri(
"OurApp;component/Themes/Classic/Theme.xaml", UriKind.Relative);
var resourceInfo = GetResourceStream(themeUri);
using (var stream = resourceInfo.Stream)
{
using (var reader = new StreamReader(stream))
{
var xamlText = reader.ReadToEnd();
var dict = XamlReader.Load(xamlText) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
}
}
不幸的是,在调用 XamlReader.Load
期间引发了 XamlParseException
:
在类型“Bar”中找不到可附加属性“Foo”。
正确附加的已正确声明,并且 XAML 中的命名空间声明正确引用所需的命名空间。如果通过 App.xaml 标记以声明方式加载到合并字典中,则附加属性 XAML 可以正常工作。
这是我尝试在运行时加载的 XAML 的缩写副本:
<ResourceDictionary xmlns:u="clr-namespace:Company.Product.Utils"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ControlPanelStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Margin="0" u:Bar.Foo="True">
<!-- Stuff and things -->
<ContentPresenter Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
为什么在运行时加载 XAML 时对附加属性的引用不起作用,而在“静态”加载时它工作得很好?
I'm trying to figure out how to programmatically apply a theme at runtime in our Silverlight 4 application. I figured this should be as simple as loading a resource dictionary from XAML and merging it with the application's merged dictionaries. Here's my code so far:
var themeUri = new Uri(
"OurApp;component/Themes/Classic/Theme.xaml", UriKind.Relative);
var resourceInfo = GetResourceStream(themeUri);
using (var stream = resourceInfo.Stream)
{
using (var reader = new StreamReader(stream))
{
var xamlText = reader.ReadToEnd();
var dict = XamlReader.Load(xamlText) as ResourceDictionary;
Resources.MergedDictionaries.Add(dict);
}
}
Unfortunately, a XamlParseException
is raised during the call to XamlReader.Load
:
The attachable property 'Foo' was not found in type 'Bar'.
This attached properly is properly declared, and the namespace declaration in the XAML correctly references the required namespace. The attached property XAML works just fine if loaded into the merged dictionaries declaratively through the App.xaml markup.
Here's an abbreviated copy of the XAML which I'm trying to load at runtime:
<ResourceDictionary xmlns:u="clr-namespace:Company.Product.Utils"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ControlPanelStyle" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Margin="0" u:Bar.Foo="True">
<!-- Stuff and things -->
<ContentPresenter Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Why is the reference to the attached property not working when loading XAML at runtime when it is working just fine when "statically" loaded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚弄清楚问题的根源。在我们的 XAML 中,我们声明了命名空间,如下所示:
看起来,虽然这适用于静态编译的 XAML,但它不适用于动态加载的 XAML,因为动态加载时,命名空间的程序集不会得到解析。一旦我们将命名空间声明更改为这样,它就起作用了:
I just figured out the source of the problem. In our XAML, we had declared our namespace as follows:
It seems that although this works for statically-compiled XAML, it doesn't work for dynamically loaded XAML because when dynamically loaded, the assembly for the namespace doesn't get resolved. Once we changed the namespace declaration to this, it worked:
我今天刚刚遇到这个问题,我通过使用行为解决了它......它有点难看,但它确实有效。
希望有帮助!
干杯!
Fer Callejón.-
嗨,雅各布,这很奇怪,我按照你所说的引用了程序集
但是,无论如何,它不起作用。不同之处在于我加载的是画布而不是资源,但我想,xaml 验证应该是相同的。
我将尝试将此 ns 设置在我要使用它的同一标签上。
干杯!!
I just faced this problem today and i solved it by using a Behavior... it's kind of ugly but it does the trick.
Hope it help!
Cheers!
Fer Callejón.-
Hi Jacob, that's wierd, I have the assembly referenced as you said
But, anyway, it doesn't work. The difference is that I'm loading a Canvas not a Resource, but i guess, xaml validation's should be same.
I'll try by setting this ns on the same tag where I'm going to use it.
Cheers!!