我发现了一些INotifyPropertyChanged包装器的solution,但是它没有任何作用。我做错了什么?名称更新异步,但Windows中的值不变。为什么?* *

namespace WpfApplication1.ViewModel
{
class CustomerViewModel : INotifyPropertyChanged, IWeakEventListener
{
    private readonly Customer _customer;

    internal CustomerViewModel(Customer customer)
    {
        if (customer == null)
        {
            throw new ArgumentNullException("personModel");
        }

        _customer = customer;
        NotifyPropertyChangedEventManager.AddListener(_customer, this);

        Action Start = new Action(UpdateAsync);
        IAsyncResult result = Start.BeginInvoke(null, null);
    }
    private void UpdateAsync()
    {
        int i = 0;
        while (true)
        {
            System.Threading.Thread.Sleep(1000);
            _customer.Name = (++i).ToString();
        }
    }

    public string Name
    {
        get { return _customer.Name; }
        set { _customer.Name = value; }
    }

    public string JobTitle
    {
        get { return _customer.Work; }
        set { _customer.Work = value; }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

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

    #region IWeakEventListener Members
    public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
    {
        PropertyChangedEventArgs pcArgs = e as PropertyChangedEventArgs;
        if (pcArgs != null)
        {
            OnPropertyChanged(pcArgs.PropertyName);
            return true;
        }
        return false;
    }
    #endregion
}
public class NotifyPropertyChangedEventManager : WeakEventManager
{
    public static NotifyPropertyChangedEventManager CurrentManager
    {
        get
        {
            var manager_type = typeof(NotifyPropertyChangedEventManager);
            var manager = WeakEventManager.GetCurrentManager(manager_type) as NotifyPropertyChangedEventManager;
            if (manager == null)
            {
                manager = new NotifyPropertyChangedEventManager();
                WeakEventManager.SetCurrentManager(manager_type, manager);
            }
            return manager;
        }
    }

    public static void AddListener(INotifyPropertyChanged source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedAddListener(source, listener);
        return;
    }

    public static void RemoveListener(INotifyPropertyChanged source, IWeakEventListener listener)
    {
        CurrentManager.ProtectedRemoveListener(source, listener);
        return;
    }
    protected override void StartListening(object source)
    {
        ((INotifyPropertyChanged)source).PropertyChanged += Source_PropertyChanged; return;
    }
    protected override void StopListening(object source)
    {
        ((INotifyPropertyChanged)source).PropertyChanged -= Source_PropertyChanged;
        return;
    }

    void Source_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        { CurrentManager.DeliverEvent(sender, e); };
    }
}


}

顾客

    public class Customer:INotifyPropertyChanged
{
    public string Name { get; set; }
    public string Number { get; set; }
    public string Work { get; set; }

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


和Xaml代码

<Grid>
    <Label Content="{Binding Name}"></Label>
</Grid>

最佳答案

除了自己做PropertyChanged,还可以使用Fody.PropertyChanged。 (https://github.com/Fody/PropertyChanged

您可以通过Visual Studio中的Nuget安装它。

它的作用是在编译时自动添加PropertyChanged实现。

您的代码:

using PropertyChanged;

[ImplementPropertyChanged]
public class Person
{
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}


编译内容:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string givenNames;
    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            if (value != givenNames)
            {
                givenNames = value;
                OnPropertyChanged("GivenNames");
                OnPropertyChanged("FullName");
            }
        }
    }

    string familyName;
    public string FamilyName
    {
        get { return familyName; }
        set
        {
            if (value != familyName)
            {
                familyName = value;
                OnPropertyChanged("FamilyName");
                OnPropertyChanged("FullName");
            }
        }
    }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

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


还有其他功能,请在Wiki中阅读它们:https://github.com/Fody/PropertyChanged/wiki

关于c# - INotifyPropertyChanged包装器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32518808/

10-10 13:22