Xamarin表格。按钮单击

时间:2017-11-02 22:05:48

标签: c# data-binding xamarin.forms

如何做到这一点? 我已经能够使用xaml实现这一点,就像这样。 我将如何使用ViewModel模式执行此操作?

View.xaml

<Button Margin="50,0,20,20" FontSize="Large" Text="Test" Clicked="Button_Clicked">
                </Button>

View.cs

private void Button_Clicked(object sender, EventArgs e)
{
    Get_LessonViewAsync();            
}

private async void Get_LessonViewAsync()
{
    var view = new LessonView();
    await Navigation.PushModalAsync(view);
}

1 个答案:

答案 0 :(得分:5)

在xaml中,您使用Command属性进行绑定:

<Button Margin="50,0,20,20" FontSize="Large" Text="Test" Command="{Binding ButtonCommand}"></Button>

在ViewModel中:

public ICommand ButtonCommand { get; private set; }
public ICommand GoLeftCommand { get; private set; }     
public ICommand GoRightCommand { get; private set; }     

public DemoViewModel ()
{
    ...
    ButtonCommand = new Command (() => {
            var view = new LessonView();
            await Navigation.PushModalAsync(view);
        });
    GoLeftCommand = new Command (() => {
            var view = new LeftView();
            await Navigation.PushModalAsync(view);
        });
    GoRightCommand = new Command (() => {
            var view = new RightView();
            await Navigation.PushModalAsync(view);
        });
}

来自David Britch的https://blog.xamarin.com/simplifying-events-with-commanding/小部件