【问题标题】:Convert code behind arguments into mvvm xamarin将参数后面的代码转换为 mvvm xamarin
【发布时间】:2021-10-31 22:12:42
【问题描述】:

有一个关于如何将后面的代码转换为 mvvm 样式的问题,这是一个示例

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

【问题讨论】:

  • 我不确定您所说的“MVVM 样式”究竟是什么意思,但您可以使用 Xamarin 社区工具包中的 EventToCommand 行为,或者您可以在 VM 上创建公共方法并从事件中调用这些方法处理程序。

标签: xamarin xamarin.forms mvvm


【解决方案1】:

结合了杰森和卡拉斯的回答。

  1. 用xaml和viewmodel对应的属性绑定。

    //xaml
    IsVisible = "{Binding IsVisible}"
    
  2. 在视图模型中实现INotifyPropertyChanged

    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. 将事件替换为EventToCommandBehavior 并更改属性值。

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

【讨论】:

    【解决方案2】:

    首先,您创建一个 BaseViewModel,您现在可以在所有其他 ViewModel 上继承它。然后你的 ViewModel 并通过 YourPage.cs 通过 BindingContext = new YourPageVM() 将它绑定到你的页面。您现在可以在 ViewModel 中创建属性并在 XAML 中绑定它们。例如: 这是 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}"
    

    您也可以使用其他值 Rotation、Source 和 Text。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2022-08-19
      相关资源
      最近更新 更多