简体   繁体   English

WPF绑定在Windows XP中不起作用

[英]WPF binding not working in Windows XP

I have the following class: 我有以下课程:

public abstract class TestBase<T> : INotifyPropertyChanged where T : class
{
    private static T _instance;
    public event PropertyChangedEventHandler PropertyChanged;

    public static T Instance
    {
        get
        {
            lock (Lock)
            {
                var instance = _instance ?? (Activator.CreateInstance<T>());

                _instance = instance;
                return instance;
            }
        }
    }

    // ReSharper disable once StaticMemberInGenericType
    public static object Lock = new object();

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}


public class Test : TestBase<Test>
{
    private bool _isDone = true;

    public bool IsDone
    {
        get
        {
            return _isDone;
        }
        set
        {
            _isDone = value;
        }
    }
}

Then, I have this declared in the xaml: 然后,我在xaml中声明了这一点:

<lib:Test x:Key="Test" />

And used like this: 像这样使用:

IsChecked="{Binding Source={StaticResource Test}, Path=Instance.IsDone}"

This is working fine on Vista and higher, but not on XP, it just doesn't bind (falls back to the default value). 在Vista和更高版本上可以正常工作,但在XP上则不能,它只是不绑定(恢复为默认值)。 We wish we could ditch XP but this isn't the case. 我们希望我们可以放弃XP,但事实并非如此。 I tried to find a little more information with Snoop but it crashes on all our XP machines. 我试图找到更多有关Snoop的信息,但它在我们所有的XP计算机上都崩溃了。 I'm shooting blanks here and run out of ideas so I'm curious if anyone here has any ideas about this problem. 我在这里拍摄空白并且用尽了所有想法,所以我很好奇这里是否有人对此问题有任何想法。

As a workaround I can add something like this to the Test class: 作为一种解决方法,我可以在Test类中添加以下内容:

public static Test InstanceXP 
{
   get 
   {
       return Instance;
   }
}

But this is a terrible workaround. 但这是一个可怕的解决方法。

I magically stumbled upon another workaround that does work on Windows XP. 我神奇地偶然发现了另一种可在Windows XP上运行的解决方法。

Instead of declaring it in a <...Resources>, use the following notation instead: 与其在<... Resources>中声明它,不如使用以下符号:

 {Binding Source={x:Static lib:Test.Instance}, Path=IsDone}

This is working correctly on all Windows versions. 这在所有Windows版本上均正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM