2017-12-16 4 views
0

하나의 창을 포함하는 WPF 응용 프로그램이 있습니다. 이 창만 던져서 사용자가 앱에서 내비게이션을 수행 할 수 있습니다.WPF MVVM : MainWindow 탐색

어플리케이션 구조이다

  • MainWindow.xaml
  • MainWindowViewModel.cs
  • StartPage.xaml
  • StartPageViewMode.cs
  • Systems.xaml
  • Systems.cs
  • 다른보기 및 관련보기 모델.

MainWindow.xaml

<Grid> 
    <ContentControl Content="{Binding CurrentWorkspace}" x:Name="ContentControlMainWindow" VerticalAlignment="Stretch"/> 
</Grid> 

MainWindowViewModel.cs

private ContentControl _currentWorkspace; 
public ContentControl CurrentWorkspace 
{ 
    get => _currentWorkspace; 
    set => SetProperty(ref _currentWorkspace, value); 
} 

//c'tor 
public MainWindowViewModel() 
{ 
    CurrentWorkspace.Content = new ContentControl { Content = new StartPage() 
} 

당신이 볼 수 있듯이, 응용 프로그램 초기화에서 내가 CurrentWorkspace에 시작 페이지보기를로드하고 있습니다. StartPageViewModel에서 CurrentWorkspace 컨텐트를 다른 뷰로 변경해야합니다. 기본적으로 나는이 CurrentWorkspace를 응용 프로그램의 각 부분에서 제어 (및 변경)하는 데 어려움을 겪고 있습니다.

MainWindowViewModel.cs에서 : MainWindow.xaml에서

// You would more likely type this as something like ViewModelBase/ObservableObject/etc. 
private object _currentWorkspace; 
public object CurrentWorkspace 
{ 
    get => _currentWorkspace; 
    set => SetProperty(ref _currentWorkspace, value); 
} 

private StartPageViewModel _startPageViewModel; 
public StartPageViewModel StartPageViewModel 
{ 
    get => _startPageViewModel; 
    set => SetProperty(ref _startPageViewModel, value); 
} 

private AnotherPageViewModel _anotherPageViewModel; 
public AnotherPageViewModel AnotherPageViewModel 
{ 
    get => _anotherPageViewModel; 
    set => SetProperty(ref _anotherPageViewModel, value); 
} 

public MainWindowViewModel() 
{ 
    StartPageViewModel = new StartPageViewModel(); 
    AnotherPageViewModel = new AnotherPageViewModel(); 

    CurrentWorkspace = StartPageViewModel; 
} 

// Navigation Method 
private void NavigateToStartPage() 
{ 
    if (CurrentWorkspace != StartPageViewModel) 
     CurrentWorkspace = StartPageViewModel; 
} 

// Navigation Method 
private void NavigateToAnotherPage() 
{ 
    if (CurrentWorkspace != AnotherPageViewModel) 
     CurrentWorkspace = AnotherPageViewModel; 
} 

:

<Window ... 
    xmlns:vm="clr-namespace:App.ViewModels" 
    xmlns:vw="clr-namespace:App.Views" 
    ... > 
    <Window.DataContext> 
     <vm:MainWindowViewModel /> 
    </Window.DataContext> 
    <Grid> 
     <ContentControl x:Name="ContentControlMainWindow" 
         Content="{Binding CurrentWorkspace}" 
         VerticalAlignment="Stretch"> 
      <ContentControl.Resources> 
       <DataTemplate x:Key="start_page_view" 
           DataType="{x:Type vm:StartPageViewModel}"> 
        <vw:StartPage /> 
       </DataTemplate> 
       <DataTemplate x:Key="another_page_view" 
           DataType="{x:Type vm:AnotherPageViewModel}"> 
        <vw:AnotherPage /> 
       </DataTemplate> 
      </ContentControl.Resources> 
     </ContentControl> 
    </Grid> 
</Window/> 

당신은 당신이 원하는대로에 CurrentWorkspace을 설정할 수 있습니다

+0

WPF의 탐색 개념을 참조하십시오. – Aybe

답변

0

나는 이런 종류의 접근법을 좋아한다. 예를 들어 StartPageViewModel의 인스턴스를 처리하려면 StartPageViewModel = null;을 설정하면됩니다.

일반적으로 ViewVMod에 UI 요소 (예 : ContentControl)가있는 것은 MVVM을 위반하는 것으로 간주됩니다.

+0

Chris 님, 다른보기 모델 (기본 창보기 모델이 아닌 사용자)에서 현재 작업 영역을 어떻게 바꿀 수 있습니까? – gr1d3r

+0

필자는 두 가지 탐색 방법뿐만 아니라 이것을 설명 할 수있는 두 가지 탐색 방법을 추가했습니다. 다음은 'MainWindowViewModel'에서 이러한 메소드를 호출하는 문제입니다. 일반적으로 일종의 ICommand를 사용합니다. 이것이 설명하는 경우 알려 주시기 바랍니다. –