Home > Back-end >  Convert code behind arguments into mvvm xamarin
Convert code behind arguments into mvvm xamarin

Time:09-17

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

CodePudding user response:

A combination of Jason's and Karas's answer .

  1. Bind with the corresponding properties between xaml and viewmodel .

    //xaml
    IsVisible = "{Binding IsVisible}"
    
  2. 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();
             }
         }
    
  3. 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;
                 }
             });
         }
    

CodePudding user response:

First you create a BaseViewModel, which you can now inherit on every other ViewModel. Then your ViewModel and bind it via BindingContext = new YourPageVM() to your page via YourPage.cs. You can now create properties in your ViewModel and Binding them in the XAML. e.g .: Here Is the BaseViewModel:

    public abstract class BaseViewModel : INotifyPropertyChanged
    {
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    protected void OnPropertyChanged([CallerMemberName] string 
    propertyName = 
    "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;

        changed.Invoke(this, new 
    PropertyChangedEventArgs(propertyName));
    }
     
    protected bool SetProperty<T>(ref T backingStore, T value,
       [CallerMemberName] string propertyName = "",
       Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }

    }

     //In Your VM:

     public class YourPageVM : BaseViewModel
     {
     bool isVisPreVideo;
     public bool IsVisPreVideo{ 
     get=> isVisPreVideo;
     set=> SetProperty(ref isVisPreVideo,value);}
    
     //set the Value in Constructor or in Your Method
     public YourPageVM()
     {
      IsVisPreVideo = false;
     }
     //........
     }
     //At Xaml:

     xmlns:viewmodel="clr-namespace:YourProject.ViewModel" 
     x:DataType="viewmodel:YourPageVM"

     IsVisible = "{Binding IsVisPreVideo}"

And this you can do also with the other values Rotation, Source and Text.

  • Related