简体   繁体   中英

Caliburn.Micro not updating UI

I'm trying to build simple utility with caliburn micro and wpf. This simple code:

    public bool CanTransfer
    {
        get { return this.canTransfer; }
        set
        {
            this.NotifyOfPropertyChange(() => this.CanTransfer);
            this.canTransfer = value;
        }
    }

    public void Transfer()
    {
        Task.Factory.StartNew(this.Process);
    }

    private void Process()
    {
        this.CanTransfer = false;
        Thread.Sleep(5000);
        this.CanTransfer = true;
    }

Does the following - Transfer button remains disabled, but I'd expect it to be enabled 5 seconds after, what am I doing wrong?

Swap those lines in CanTransfer setter method - now you first say something has changed while it actually hasn't and then you change it:

public bool CanTransfer
{
    get { return this.canTransfer; }
    set
    {
        this.canTransfer = value;
        this.NotifyOfPropertyChange(() => this.CanTransfer);
    }
}

I usually use method like ChangeAndNotify<T> (the only annoyance is declaration of the field), the code can be seen here :

private bool _canTransfer;
public string CanTransfer
{
  get { return _canTransfer; }
  set 
  { 
    PropertyChanged.ChangeAndNotify(ref _canTransfer, value, () => CanTransfer); 
  }
}

You are missing a notifiation:

this.NotifyOfPropertyChange(() => this.CanTransfer);

I hope I got the syntax right, I am typing from memory. This basically tells WPF "Hey something changed, go and look at the new value".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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