2016-10-28 7 views
0

私はXamarin MVVMプロジェクトで異なるページを切り替える機能を実装しようとしています。私は3つのフォルダを持っています - "モデル"、 "ビュー"と "ViewModels"。 「Views」には、他のViewを表示する役割を果たす「MainView」が含まれています。MVVMの異なるページを切り替える

MainView.xaml:

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="KeepFit.Views.MainView"> 

    <ContentPresenter x:Name="ContentArea" Content="{Binding CurrentView}"/> 

</ContentPage> 

MainView.xaml.cs:

public partial class MainView : ContentPage 
{ 
    public MainView() 
    { 
     InitializeComponent(); 

     BindingContext = new MainViewModel(); 
    } 
} 

あなたが見ることができるように、 "MAINVIEW" は "MainViewModel" クラスにバインドされます。

MainViewModel.cs:CurrentViewは私のHomeViewに設定されている

public class MainViewModel : INotifyPropertyChanged 
    { 
     public Command SwitchViewsCommand { get; private set; } 

     public MainViewModel() 
     { 
      SwitchViewsCommand = new Command((parameter) => 
      CurrentView = (ContentPage)Activator.CreateInstance(parameter as Type)); 
      CurrentView = new HomeView(); 
     } 

     private ContentPage _currentView; 
     public ContentPage CurrentView 
     { 
      get 
      { 
       return _currentView; 
      } 
      set 
      { 
       if (value != _currentView) 
       { 
        _currentView = value; 
        OnPropertyChanged("CurrentView"); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged([CallerMemberName] 
     string propertyName = null) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

コンストラクタで(私たちはそれがデフォルトContentPageあるとしましょう)。これはContentPresenterにバインドされているため、アプリケーションのランタイムの始めに表示する必要があります。そうではありません。

私は、ContentPresenterがXamarin.Forms.Viewオブジェクトを「コンテンツ」として期待していて、私のビューがXamarin.Forms.ContentPageから継承していることに気付きました。しかし、例えばContentViewsに変更すると、BindingContextを設定することができなくなります。誰が何を私が間違っているのか説明できますか?

答えて

0

あなたはこれを試していますか?

async void butonClick(object sender, EventArgs e) 
    { 
     await Navigation.PushAsync(new EntryPageCode()); 
    } 

"EntryPageCode"の位置にビューインスタンスがあります。 これを試して、仕事がうまくいくかどうかについて教えてください。

関連する問題