7

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);
    }
}

残念ながら、XamlParseExceptionへの呼び出し中に a が発生しますXamlReader.Load:

添付可能なプロパティ 'Foo' がタイプ 'Bar' で見つかりませんでした。

これは適切にアタッチされ、適切に宣言されており、XAML の名前空間宣言は必要な名前空間を正しく参照しています。添付プロパティ XAML は、App.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 を読み込むときに添付プロパティへの参照が機能しないのはなぜですか?

4

2 に答える 2

15

問題の原因を突き止めました。XAMLでは、名前空間を次のように宣言しました。

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

これは静的にコンパイルされたXAMLでは機能するようですが、動的に読み込まれると名前空間のアセンブリが解決されないため、動的に読み込まれるXAMLでは機能しないようです。名前空間宣言をこれに変更すると、次のように機能しました。

xmlns:u="clr-namespace:Company.Product.Utils;assembly=OurAssembly"
于 2010-09-21T21:14:20.170 に答える
0

今日この問題に直面したばかりで、動作を使用して解決しました...ちょっと醜いですが、うまくいきます。

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"

しかし、とにかく、それはうまくいきません。違いは、リソースではなく Canvas をロードしていることですが、xaml 検証は同じである必要があると思います。

この ns を使用するタグと同じタグに設定してみます。

乾杯!!

于 2010-09-21T21:04:59.880 に答える