0

次の問題があります。私は ResourceDictionaries を別のアセンブリに配置しています。UserControls を作成し、この ResourceDictionaries から Styles と Resourcen を使用すると、ランタイムではすべて正常に動作しますが、設計時に vs2010 のようなエラーが発生しました - 「InvertConverter」という名前のリソースが見つかりませんでした。

  • CoreResource.dll
  • その他のResource.dll
  • UserControl.dll (上記の両方を参照)
  • OtherWpf.dll (上記のすべてを参照し、ユーザー コントロールを使用します)

最近、この問題に関連する多くの投稿やブログをチェックしました。1 つの解決策は、ResourceDictionaries をすべての UserControl に追加することでした。これは機能しますが、実行時に多くのオーバーヘッドが発生します。私が見つけた他のすべての解決策は私にとってはうまくいきませんでした。

それは私にとってはうまくいくので、少なくとも答えとして私がやったことを投稿します。しかし、他の/より良い解決策を見たいです。

4

1 に答える 1

0

これが私が今やったことです。

私は単に静的メソッドを使用して、設計時にリソースディクショナリを追加します。

public class DesignTimeResourceLoader
{
    public static void LoadResources4DesignTime(UserControl ctrl)
    {
        //do this just in DesignMode
        if (Convert.ToBoolean(DesignerProperties.IsInDesignModeProperty.GetMetadata(ctrl).DefaultValue))
        {
            var uricore = new Uri("/CoreResource;component/ResourceDictionary.xaml", UriKind.Relative);
            var core = (ResourceDictionary)Application.LoadComponent(uricore);
            ctrl.Resources.MergedDictionaries.Add(core);

            var uriother = new Uri("/OtherResource;component/OtherResourceDictionary.xaml", UriKind.Relative);
            var other = (ResourceDictionary)Application.LoadComponent(uriother);
            ctrl.Resources.MergedDictionaries.Add(other);

            //if you have(need more just add here
        }
    }
}

UserControl.dll でこのクラスを作成して使用し、すべての Usercontrol に対してコンストラクターでメソッドを呼び出します。

public partial class MyControl : UserControl
{
    public MyControl ()
    {
        DesignTimeResourceLoader.LoadResources4DesignTime(this);
        InitializeComponent();
    }
}

これは私のatmで機能します。bu たぶん、私が今見たことのないいくつかの欠点があります。

于 2012-07-26T12:03:55.707 に答える