2017-11-02 9 views
-1

どうすればこのことができますか? 私はこれをxamlを使って実装することができました。 代わりにViewModelパターンを使用してこれを行う方法はありますか? XAMLでXamarin Forms。ボタンをクリックした後に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); 
} 

答えて

3

あなたは、コマンド属性を使用してバインド:あなたのViewModelで

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

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

スニペット:https://blog.xamarin.com/simplifying-events-with-commanding/ by David Britch

+0

ありがとう、もう1つ質問。私はICommandインターフェイスを実装する場合。同様に、public void Execute(object parameter){}どのコマンドが実行されたかを知るには – user2202098

+1

あなたの質問が正しいと分かったら、各コマンドに1つのプロパティがあります。 –

関連する問題