簡體   English   中英

如何將Instanceproperty綁定到viewmodel,view沒有更新

[英]How to bind Instanceproperty to viewmodel, view is not updated

問題:

我的 Viewmodel 創建了一個控制器mycontroller的實例,並且屬性Busy應傳遞給 View 並根據控制器狀態在 View 中更新,但我的 View 不會更新。 另一個屬性Busy2根據當前狀態更新。 Bindablebase 實現了 I

題:

為什么 ViewModel 中的Busy屬性沒有更新? mycontroller.Busy 屬性正在更新,但不是視圖。

框架和工具:

棱鏡 6.3.0

Fody.Propertychanged

看法:

<TextBlock Text="{Binding Path=Busy}"></TextBlock>
<TextBlock Text="{Binding Path=Busy2}"></TextBlock>

視圖模型:

public class Controller
    {
        public bool Busy { get; private set; }

        public async void GetValue()
        {
            Busy = true;
            await Task.Delay(5000);
            Busy = false;
        }
    }

    public class MyViewModel : BindableBase
    {
        private readonly Controller _mycontroller;
        public DelegateCommand<string> RunCommand { get; private set; }

        // This property is not updated in the view
        public bool Busy
        {
            get { return _mycontroller.Busy; }
        }

        // Works as aspected
        public bool Busy2 { get; set; }

        public MyViewModel()
        {
            _mycontroller = new Controller();
            RunCommand = new DelegateCommand<string>(Run, Canrun).ObservesProperty((() => _mycontroller.Busy));
        }

        private bool Canrun(string arg)
        {
            return _mycontroller.Busy != true;
        }

        private void Run(string obj)
        {

            Busy2 = true;
            _mycontroller.GetValue();


        }
    }

更新:

我從 Prism 添加了 Bindablebase,因為它實現了 INotifyPropertyChanged,但視圖仍未更新。 我重構了代碼並設置了一個斷點來設置{ SetProperty(ref _busy, value); } { SetProperty(ref _busy, value); }並且斷點從未達到。

我也刪除了 Propertychanged.Fody nuget 包。

視圖模型:

   public class Controller : BindableBase
    {
        private bool _busy;
        public bool Busy
        {
            get { return _busy; }
            set { SetProperty(ref _busy, value); }
        }

        public Controller()
        {

        }

        public void DoWork1()
        {
            for (var i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);
                _busy = !_busy;
                Debug.WriteLine(_busy.ToString());
            }
        }

        public void DoWork2()
        {
            _busy = !_busy;

        }
    }

    public class MainWindowViewModel : BindableBase
    {
        private Controller mycontroller;
        private string _title = "Prism Unity Application";

        public DelegateCommand RunCommand { get; private set; }

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public bool Busy
        {
            get { return mycontroller.Busy; }
        }

        public MainWindowViewModel()
        {
            RunCommand = new DelegateCommand(Execute);
            mycontroller = new Controller();

        }

        private void Execute()
        {
            mycontroller.DoWork1();
        }
    }

看法:

<Window x:Class="PropertytestPrism.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <StackPanel>
        <Button Command="{Binding RunCommand}" Content="Run"></Button>
        <TextBlock Text="{Binding Busy}"></TextBlock>

    </StackPanel>
</Window>

更新 2

  1. 失敗:缺少類控制器的 INotifypropertychanged

視圖仍未更新,原因是我的 Delegatecommandmethod 執行 mycontroller.DoWork1();

題:

為什么視圖沒有更新? 如果我在 DelegateCommandmethod 中執行該方法?

您應該通過Controller類實現INotifyPropertyChanged Controller類中更改了屬性,應將此更改通知:

public class Controller : INotifyPropertyChanged
{
    private bool _busy;

    public bool Busy 
    {
        get
        {
            return  _busy;
        } 
        private set
        {
            SetField(ref _busy, value, "Busy"); }
        } 
    }

    public async void GetValue()
    {
        Busy = true;
        await Task.Delay(5000);
        Busy = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
           handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value))
        {
            return false;
        }

        field = value;
        OnPropertyChanged(propertyName);

        return true;
    }
}

試着讓你的綁定像這樣工作。

<TextBlock Text="{Binding Busy,Mode=OneWay}"></TextBlock>.

默認綁定是一次性的,因此即使在屬性更改通知中它們也不會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM