从模型MVVM UWP更新ViewModel属性

时间:2016-12-13 10:13:16

标签: c# events mvvm uwp

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

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

每次执行活动后,我都需要更新View Model中的属性。

ViewModel.cs

public class MainActivity extends Activity {

MediaRecorder recorder;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();

}

public void init(){

recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/storage/sdcard0/android_730_new.amr");

recorder.setMaxDuration(10000);

recorder.setOnInfoListener(new OnInfoListener() {

    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {

        if(what==recorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){

            Toast.makeText(getApplicationContext(), "10 seconds recording completed....", 2000).show();

            recorder.stop();

        }
    }
});


try{
    recorder.prepare();
}catch (Exception e) {
    // TODO: handle exception
}



}





public void start(View v){

    recorder.start();
}


public void stop(View v){

    recorder.stop();
}

Service.cs

 public Brush CurrentGetExecutionColor
        {
            get { return _currentGetExecutionColor; }
            set { Set(ref _currentGetExecutionColor, value); }
        }

public DelegateCommand DelegateCommandProcess
            => _delegateCommandProcess ?? (_delegateCommandProcess = new DelegateCommand(async () =>
            {
                await _service.ProcessMethod();
            }));

我如何实现此功能,以便我可以从服务更新View Model属性。

先谢谢。

1 个答案:

答案 0 :(得分:0)

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

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));
}
相关问题