1

次の例を検討してください

public class Test
{
    private static string _property = "Success";
    public static string Property
    {
        get { return _property; }
        set { _property = value; }
    }


    public void Check()
    {
        var prop = new PropertyPath(this.GetType().GetProperty("Property"));
        var binding = new Binding();
        binding.Source = typeof(Test);
        binding.Path = prop;
    }

    public static void DoTest()
    {
        new Test().Check();
    }
}

呼び出すと、自分のマシンでは正常に動作しますが、他のマシンでは「Binding.Source が使用されている場合は Binding.StaticSource を割り当てることができません」(これは正確な翻訳テキストではありません) などのメッセージTest.DoTest()がスローされます。InvalidOperationExceptionプロパティが静的でない場合、すべてが機能します。このような動作の原因は何ですか?

4

2 に答える 2

1

私は4年前にWPFで働いていました...私はそれをすべて覚えていませんが、これを使用するとうまくいくかもしれません。

public class Test : DependencyObject
{

    public static readonly DependencyProperty FilterStringProperty =
        DependencyProperty.Register("Property", typeof(string),
        typeof(Test), new UIPropertyMetadata("Success"));
    public string Property
    {
        get { return (string)GetValue(FilterStringProperty); }
        set { SetValue(FilterStringProperty, value); }
    }

    public static Test Instance { get; private set; }

    static Test()
    {

    }

    public void Check()
    {
        var prop = new PropertyPath(this.GetType().GetProperty("Property"));

        var binding = new Binding();
        binding.Source = this;
        //binding.Source = typeof(Test); //-- same thing
        binding.Path = prop;

    }

    public static void DoTest()
    {

        Instance = new Test();
        new Test().Check();
    }
}
于 2013-05-15T21:45:55.437 に答える
0

テストしているマシンには異なるフレームワーク バージョンがあり、.NET 4.5 のこの新機能のために非互換性が発生していると推測しています: http://msdn.microsoft.com/en-us/library/bb613588 %28v=VS.110%29.aspx#static_properties

于 2013-05-15T19:09:33.290 に答える