Issue
got a question on how to convert this code behind into a mvvm style, Here's a sample
void CameraView_MediaCaptured(object sender, MediaCapturedEventArgs e)
{
switch (cameraView.CaptureMode)
{
default:
case CameraCaptureMode.Default:
case CameraCaptureMode.Photo:
previewVideo.IsVisible = false;
previewPicture.IsVisible = true;
previewPicture.Rotation = e.Rotation;
previewPicture.Source = e.Image;
doCameraThings.Text = "Snap Picture";
break;
case CameraCaptureMode.Video:
previewPicture.IsVisible = false;
previewVideo.IsVisible = true;
previewVideo.Source = e.Video;
doCameraThings.Text = "Start Recording";
break;
}
}
Solution
A combination of Jason's and Karas's answer .
Bind with the corresponding properties between xaml and viewmodel .
//xaml IsVisible = "{Binding IsVisible}"
Implement
INotifyPropertyChanged
inside viewmodel .public class ViewModel: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private bool isVisible; public bool IsVisible { get { return isVisible; } set { isVisible = value; NotifyPropertyChanged(); } }
Replace the event with EventToCommandBehavior and change the properties value.
//xaml <xct:EventToCommandBehavior EventName="MediaCaptured" Command="{Binding MyCommand}" />
//viewmodel public ICommand MyCommand { get; set; } public ViewModel() { MyCommand = new Command((obj)=> { var cameraView = obj as CameraView; switch (cameraView.CaptureMode) { default: case CameraCaptureMode.Default: case CameraCaptureMode.Photo: IsVisible = false; break; case CameraCaptureMode.Video: IsVisible = false; break; } }); }
Answered By - ColeX - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.