从模型MVVM UWP更新ViewModel属性



我正在开发一个UWP应用程序,其中我正在遵循MVVM模式。

我在视图模型中具有绑定到视图的属性。我在服务中有一个处理多个任务的功能。

每次执行活动后,我需要更新视图模型中的属性。

ViewModel.cs

 public Brush CurrentGetExecutionColor
        {
            get { return _currentGetExecutionColor; }
            set { Set(ref _currentGetExecutionColor, value); }
        }
public DelegateCommand DelegateCommandProcess
            => _delegateCommandProcess ?? (_delegateCommandProcess = new DelegateCommand(async () =>
            {
                await _service.ProcessMethod();
            }));

service.cs

    private async Task<bool> ProcessMethod()
    {
       While(condition)
       {
          Process();
          //UpdateViewModel property
         CurrentGetExecutionColor = Color.Red;
       }
    }

如何实现此功能,以便可以从服务更新视图模型属性。

预先感谢。

尝试在您的属性中实现,如下:

private Type _yourProperty;
public Type YourProperty
{
   get { return _yourProperty; }
   set
   {
      _yourProperty = value;
      OnPropertyChanged();
   }
}

public event PropertyChangedEventHandler PropertyChanged;    
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

最新更新