将参数后面的代码转换为mvvm xamarin



有一个关于如何将这段代码转换成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;
}
}

Jason和Karas的答案的组合。

  1. 在xaml和viewmodel之间绑定相应的属性

    //xaml
    IsVisible = "{Binding IsVisible}"
    
  2. 在viewmodel内实现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;
    }
    });
    }
    

首先创建一个BaseViewModel,现在您可以在每个其他ViewModel上继承它。然后你的ViewModel和绑定它通过BindingContext = new YourPageVM()到你的页面通过YourPage.cs。现在可以在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}"

你也可以设置其他的值旋转,源和文本。

最新更新