2017-11-02 3 views
-1

어떻게이 작업을 수행 할 수 있습니까? xaml을 사용하여 이것을 구현할 수 있었고 그렇게 할 수있었습니다. 대신 ViewModel 패턴을 사용하면 어떻게됩니까?Xamarin Forms. 버튼을 클릭 한 후 ViewModel에서 뷰로드

View.xaml

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

View.cs XAML 명령 속성을 사용하여 바인딩에서

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

발췌 : https://blog.xamarin.com/simplifying-events-with-commanding/ by David Britch

+0

감사합니다. 만약 내가 ICommand 인터페이스를 구현했다. 마찬가지로 public void Execute (object parameter) {} 어떻게 명령이 실행되었는지 알 수 있습니까? – user2202098

+1

질문이 맞다면 각 명령마다 하나의 Property를가집니다. –

관련 문제