簡體   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