Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Instanceproperty绑定到viewmodel,视图不更新_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何将Instanceproperty绑定到viewmodel,视图不更新

C# 如何将Instanceproperty绑定到viewmodel,视图不更新,c#,wpf,mvvm,C#,Wpf,Mvvm,问题: My Viewmodel创建控制器mycontroller的实例,属性Busy将传递给视图,并根据控制器状态在视图中更新,但我的视图不会更新。另一个属性Busy2根据当前状态进行更新。Bindablebase实现了 问题: 为什么ViewModel中的属性未更新?mycontroller.Busy属性正在更新,但视图未更新 框架和工具: 棱镜6.3.0 福迪,财产改变了 视图: 视图: 更新2 失败:缺少类控制器的INotifypropertychanged 视图仍然没有更新,原因是我的

问题:

My Viewmodel创建控制器mycontroller的实例,属性Busy将传递给视图,并根据控制器状态在视图中更新,但我的视图不会更新。另一个属性Busy2根据当前状态进行更新。Bindablebase实现了

问题:

为什么ViewModel中的属性未更新?mycontroller.Busy属性正在更新,但视图未更新

框架和工具:

棱镜6.3.0

福迪,财产改变了

视图:

视图:

更新2

失败:缺少类控制器的INotifypropertychanged 视图仍然没有更新,原因是我的Delegatecommandmethod执行mycontroller.DoWork1

问题:

为什么视图没有更新?如果我在DelegateCommandmethod中执行该方法

您应该实现由控制器类更改的InotifyProperty。属性在控制器类内更改,应通知此更改:

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;
    }
}
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();


        }
    }
   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>
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;
    }
}