2016-03-06 93 views
0

我必須在WPF C#中編寫應用程序。我的問題是我不知道如何處理多個視圖。直到知道我知道如何使用Prism通過綁定將ViewModel連接到View,在基本級別。通過重寫OnStartup方法和UnityContainer的使用,我學會了一點Unity以在App.xml.cs中註冊ViewModel到View。WPF多視圖加棱鏡和統一

我想知道如何從視圖1導航到視圖2,反之亦然。 我想瀏覽一個按鈕,視圖不同。

你能幫助我嗎?一些建議?

像這樣,退相干! enter image description here

+1

創建一個區域(例如,''),註冊您的導航視圖('IUnityContainer.RegisterTypeForNavigation'),然後執行導航命令綁定到你的按鈕('RegionManager.RequestNavigate') – Haukinger

回答

0

這是非常容易與棱鏡導航做到這一點,它不需要你在ViewModel上創建依賴關係,或者使用動態DataTemplates引入性能滯後。我建議閱讀有關Prism導航的文檔。

https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/60-Navigation.md#view-based-navigation

基本上你使用ReqestNavigate,GoBack的,和GoForward的組合。

酒店還設有一個樣品,你在這裏學習:

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/View-Switching%20Navigation_Desktop

你也應該看這個過程可以引導您瞭解什麼,你都在問:

https://app.pluralsight.com/library/courses/prism-introduction/table-of-contents

1

我有很久以前寫的一個例子,它不需要任何框架廢話,根據我的經驗,WPF MVVM框架是沒用的大部分,而且往往簡單的事情複雜化所有你需要的是一個ICommand實現和ViewModelBase它實現INotiftyPropertyChanged,這裏是一個簡單的例子:

XML:

<Window.Resources> 
     <DataTemplate DataType="{x:Type local:ViewModel1}"> 
      <local:View1/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:ViewModel2}"> 
      <local:View2/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:ViewModel3}"> 
      <local:View3/> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"></RowDefinition> 
      <RowDefinition></RowDefinition> 
     </Grid.RowDefinitions> 
     <ContentControl Content="{Binding CurrentViewModel}"> 
     </ContentControl> 

     <StackPanel Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Bottom"> 
      <Button Command="{Binding PrevViewModel}">Previouws View</Button> 
      <Button Command="{Binding NextViewModel}">Next View</Button> 
      <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel1}">Switch To View1</Button> 
      <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel2}">Switch To View2</Button> 
      <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel3}">Switch To View3</Button> 
     </StackPanel> 
    </Grid> 

鑑於上述情況時,有史以來CurrentViewModel屬性更改,視圖將得到一個基於選擇資源,並且窗口的DataContext設置爲MainViewModel

主視圖模型如下所示:

public class MainViewModel : ViewModelBase 
    { 
     //add instances to all the ViewModels you want to switch between here, and add templates for them in your resources specifying the x:type and the view or data template to be used 
     public ObservableCollection<ViewModelBase> ViewModels { get; set; } = new ObservableCollection<ViewModelBase>(); 

     private ViewModelBase _currentViewModel; 

     public ViewModelBase CurrentViewModel { 
      get { return _currentViewModel; } 
      set { SetField(ref _currentViewModel, value); } 
     } 

     private ICommand _nextViewModel; 
     public ICommand NextViewModel 
     { 
      get 
      { 
       return _nextViewModel = _nextViewModel ?? new RelayCommand(p => 
        { 
         CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) +1]; 
        }, p => 
        { 
         return ViewModels.IndexOf(CurrentViewModel) + 1 != ViewModels.Count && CurrentViewModel != null; 
        }); 
      } 
     } 

     public ICommand _prevViewModel; 
     public ICommand PrevViewModel 
     { 
      get 
      { 
       return _prevViewModel = _prevViewModel ?? new RelayCommand(p => 
       { 
        CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) - 1]; 
       }, p => 
       { 
        return ViewModels.IndexOf(CurrentViewModel) != 0 && CurrentViewModel!=null; 
       }); 
      } 
     } 

     private ICommand _switchToViewModel; 
     public ICommand SwitchToViewModel 
     { 
      get 
      { 
       return _switchToViewModel = _switchToViewModel ?? new RelayCommand(p => 
       { 
        CurrentViewModel = this.ViewModels.FirstOrDefault(vm=>vm.GetType()==p as Type); 
       }, p => 
       { 
        return this.ViewModels.FirstOrDefault(vm => vm.GetType() != p as Type) != null; 
       }); 
      } 
     } 
    } 

結果看起來像

enter image description here

+0

@Adrian對不起,以爲你問的是'wpf',因爲它在問題的標籤中,我不知道任何與IOS相關的東西,請編輯你的問題並詢問你的問題需要準確地等待相關的答案,問題說wpf,而不是iphone或手機或任何跡象表明你沒有使用'wpf'。 –

+0

謝謝。沒關係。你可能有一部智能手機。我問這個問題,因爲我想知道我是否可以在WPF中實現相同的行爲。爲了以防萬一,我會將此頁面加入書籤。 – Adrian

+0

@Adrian是的,你確定你可以!,這是上述想法,你需要上述的唯一變化是隱藏按鈕,而不是禁用它們,如果這就是你需要讓我知道,讓我知道你是如何準確地 –