繁体   English   中英

Xamarin形式。 单击按钮后从ViewModel加载View

[英]Xamarin Forms. Load View from ViewModel after button click

如何才能做到这一点? 我已经能够使用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);
}

在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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM