2017-02-16 67 views
-1

我'誰建設幾個觀點,我想通過點擊一個按鈕,在當前視圖從一個視圖切換到另一個軟件: enter image description hereprobleme幾個視圖MVVM處理目前

當然

我有超過3個觀點,但它是爲了說明這個概念。這裏是我的代碼從第1頁到第2頁,但我有麻煩從第2頁到第3頁,我不知道什麼是錯的。感謝您的幫助。

MainWindow.xaml

<Controls:MetroWindow x:Class="maquette.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"  
     xmlns:local="clr-namespace:maquette" 
     xmlns:viewModel ="clr-namespace:maquette.ViewModel" 
     xmlns:view ="clr-namespace:maquette.View" 
     BorderBrush="{DynamicResource AccentColorBrush}" 
     BorderThickness="2" 
     mc:Ignorable="d" 
     Title="MainWindow"> 

    <Window.Resources> 
     <DataTemplate DataType="{x:Type viewModel:page1ViewModel}"> 
      <local:Page1/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type viewModel:page2ViewModel}"> 
      <view:Page2/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type viewModel:page3ViewModel}"> 
      <view:Page3/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type viewModel:page4ViewModel}"> 
      <view:Page4/> 
     </DataTemplate> 
    </Window.Resources> 

    <Grid> 
     <Controls:TransitioningContentControl x:Name="pagesControl" 
               Content="{Binding SelectedViewModel}" 
               Transition="Left" 
               /> 
    </Grid> 

</Controls:MetroWindow> 

MainWindow.cs

public MainWindow() 
{ 
    InitializeComponent(); 

    var viewModel = new NavigationViewModel(); 

    viewModel.SelectedViewModel = new page1ViewModel(viewModel); 

    this.DataContext = viewModel; 
} 

NavigationViewModel.cs

public class NavigationViewModel : INotifyPropertyChanged 
{ 

    private object selectedViewModel; 

    public object SelectedViewModel 
    { 
     get 
     { 
      return selectedViewModel; 
     } 
     set 
     { 
      selectedViewModel = value; 
      OnPropertyChanged("SelectedViewModel"); 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
} 

的Page1.xaml

<UserControl x:Class="maquette.Page1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:maquette.View" 
      xmlns:viewModel="clr-namespace:maquette.ViewModel" 
      d:DesignHeight="300" d:DesignWidth="300" 
      mc:Ignorable="d"  
      > 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 


     <TextBlock x:Name="Title" 
        HorizontalAlignment="Center" 
        TextWrapping="Wrap" 
        VerticalAlignment="Center" 
        Width="320" 
        FontSize="30" 
        Grid.ColumnSpan="2" 
        FontFamily="Segoe UI" 
        FontWeight="Bold">File Control Program</TextBlock> 


     <Button x:Name="button" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Grid.RowSpan="2" 
       Width="180" 
       Height="90" 
       Style="{StaticResource AccentedSquareButtonStyle}" 
       Command="{Binding Path=goSettings}" 
       > 
      <TextBlock Text="go page 2" 
         TextWrapping="Wrap" 
         TextAlignment="Center"/> 
     </Button> 
    </Grid> 
</UserControl> 

page1ViewModel

class page1ViewModel 
{ 
    public ICommand goSettings { get; set; } 

    private readonly NavigationViewModel _navigationViewModel; 

    public page1ViewModel(NavigationViewModel navigationViewModel) 
    { 
     _navigationViewModel = navigationViewModel; 
     goSettings = new BaseCommand(OpenSettings); 
    } 
    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="obj"></param> 
    private void OpenSettings(object obj) 
    { 
     _navigationViewModel.SelectedViewModel = new page2ViewModel(); 
    } 
} 

page2.xaml

<UserControl x:Class="maquette.View.Page2" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      xmlns:local="clr-namespace:maquette.View" 
      xmlns:view ="clr-namespace:maquette.View" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      Background="White"> 

    <Grid> 
     <Button x:Name="button" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Grid.ColumnSpan="2" 
       Grid.Row="4" 
       Width="180" 
       Height="80" 
       Style="{StaticResource AccentedSquareButtonStyle}" 
       Command="{Binding Path=goPage3}"> 
      <TextBlock Text="Go page 3" 
         TextWrapping="Wrap" 
         TextAlignment="Center" 
         FontSize="20" 
         /> 
     </Button> 
    </Grid> 
</UserControl> 

page2.cs

public Page2() 
{ 
    InitializeComponent(); 
    var viewModel = new NavigationViewModel(); 
    DataContext = new page2ViewModel(viewModel); 
} 

page2ViewModel.cs

public class page2ViewModel 
    { 

     public ICommand goPage3 { get; set; } 

     private readonly NavigationViewModel _navigationViewModel; 

     public page2ViewModel() 
     { 

     } 
     public page2ViewModel(NavigationViewModel navigationViewModel) 
     { 
      _navigationViewModel = navigationViewModel; 
      goPage3 = new BaseCommand(OpenPage3); 
     } 
     /// <summary> 
     /// 
     /// </summary> 
     /// <param name="obj"></param> 
     private void OpenPage3(object obj) 
     { 
      _navigationViewModel.SelectedViewModel = new page3ViewModel(); 
     } 
} 

但正如我所說2頁之間的過渡和3不起作用。任何幫助?謝謝 !

回答

1

page2ViewModel刪除此構造函數重載:

public page2ViewModel() 
{ 

} 

始終與一個注入它只有在page1ViewModel NavigationViewModel

private void OpenSettings(object obj) 
{ 
    _navigationViewModel.SelectedViewModel = new page2ViewModel(_navigationViewModel); 
} 

而且不設置Page2DataContext明確:

public Page2() 
{ 
    InitializeComponent(); 
} 

數據模板將確保它獲得正確的DataContext

+0

好的,但是然後從第2頁到第3頁我應該把:private void OpenPage3(object obj) { _navigationViewModel.SelectedViewModel = new page3ViewModel(_navigationViewModel); } – Json

+0

and in page3ViewModel:private readonly NavigationViewModel _navigationViewModel; 公共page3ViewModel(NavigationViewModel navigationViewModel) { _navigationViewModel = navigationViewModel; } ?? – Json

+0

是的,應該能夠導航的所有視圖模型都應該注入一個對NavigationViewModel類的引用。 – mm8

0

這是不使用任何MVVM框架

定義數據模板在主窗口視圖模型孩子的ViewModels。

對於整個應用程序,您必須爲您的主視圖模型創建靜態對象。

然後只有意見將被改變。

它應該是這樣的......

public partial class App : Application 
{ 
    public static MainWindowViewModel mainWindowViewModel; 

    public App() 
    { 
     mainWindowViewModel = new MainWindowViewModel(); 
    } 
} 

在主窗口視圖模型

public class MainWindowViewmodel 
    { 

     private object selectedViewModel; 

     public object SelectedViewModel 
     { 
      get 
      { 
       return selectedViewModel; 
      } 
      set 
      { 
       selectedViewModel = value; 
       OnPropertyChanged("SelectedViewModel"); 
      } 
     } 
     public MainWindowViewmodel() 
     { 
      SelectedViewModel = new page1viewmodel(); 
     } 
    } 

在Page1Viewmodel在

class page1ViewModel 
{ 
public ICommand goSettings { get; set; } 

private readonly NavigationViewModel _navigationViewModel; 

public page1ViewModel(NavigationViewModel navigationViewModel) 
{ 
    _navigationViewModel = navigationViewModel; 
    goSettings = new BaseCommand(OpenSettings); 
} 
/// <summary> 
/// 
/// </summary> 
/// <param name="obj"></param> 
private void OpenSettings(object obj) 
{ 
    mainWindowViewModel.SelectedViewModel = new page2ViewModel(); 
} 

}

這樣,你必須執行。

這會幫助你。