为什么 XamlReader.Load 无法识别 Silverlight 4 中的附加属性?

发布于 2024-09-24 08:49:10 字数 1651 浏览 10 评论 0原文

我试图弄清楚如何在 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 技术交流群。

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

发布评论

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

评论(2

温馨耳语 2024-10-01 08:49:10

我刚刚弄清楚问题的根源。在我们的 XAML 中,我们声明了命名空间,如下所示:

xmlns:u="clr-namespace:Company.Product.Utils"

看起来,虽然这适用于静态编译的 XAML,但它不适用于动态加载的 XAML,因为动态加载时,命名空间的程序集不会得到解析。一旦我们将命名空间声明更改为这样,它就起作用了:

xmlns:u="clr-namespace:Company.Product.Utils;assembly=OurAssembly"

I just figured out the source of the problem. In our XAML, we had declared our namespace as follows:

xmlns:u="clr-namespace:Company.Product.Utils"

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:

xmlns:u="clr-namespace:Company.Product.Utils;assembly=OurAssembly"
戏舞 2024-10-01 08:49:10

我今天刚刚遇到这个问题,我通过使用行为解决了它......它有点难看,但它确实有效。

public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(AddressableObjectBehavior), new PropertyMetadata(null, OnTitleChanged));

    protected override void OnAttached()
    {
        AddressableObject.SetTitle(this.AssociatedObject, this.Title);
        base.OnAttached();
    }

希望有帮助!
干杯!
Fer Callejón.-


嗨,雅各布,这很奇怪,我按照你所说的引用了程序集

xmlns:bsic="clr-namespace:Bsi.Ipp.Eurocodes.UI.Controls;assembly=Bsi.Ipp.Eurocodes.UI.Controls"

但是,无论如何,它不起作用。不同之处在于我加载的是画布而不是资源,但我想,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.

public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(AddressableObjectBehavior), new PropertyMetadata(null, OnTitleChanged));

    protected override void OnAttached()
    {
        AddressableObject.SetTitle(this.AssociatedObject, this.Title);
        base.OnAttached();
    }

Hope it help!
Cheers!
Fer Callejón.-


Hi Jacob, that's wierd, I have the assembly referenced as you said

xmlns:bsic="clr-namespace:Bsi.Ipp.Eurocodes.UI.Controls;assembly=Bsi.Ipp.Eurocodes.UI.Controls"

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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文