2017-10-20 143 views
0

我正在使用Prism 6來構建具有多個不同模塊的應用程序。這是一個簡單的按鈕菜單,每個按鈕都將主要內容區域導航到不同的動態發現視圖。不幸的是,我發現自己對主菜單按鈕進行了硬編碼,這顯然不是我想要的。棱鏡6導航和動態發現視圖

我想要的是從所有已註冊我的類別的視圖列表中動態構建單選按鈕的ItemsControl。但我不知道如何編寫ViewModel屬性,要求Prism提供這樣的列表

此外,我希望保證我的整體方法是合理的。

爲了說明,現在我已經有了幾個功能顯示,每個功能都在自己的模塊中實現。

  • 一種用於顯示來自攝像頭的實時取景和捕獲圖像
  • 模塊A用於瀏覽所俘獲圖像的一個庫模塊
  • 其他忽略爲了簡潔

以我主視圖XAML,我在左邊的DockPanel中有按鈕,右邊是一個區域(ContentControl「Main」)中的視圖內容。這是XAML修剪了一下。

<DockPanel > 
    <Border MinWidth="50" 
      DockPanel.Dock="Left" 
      > 
     <StackPanel Orientation="Vertical"> 
      <StackPanel.Resources> 
       <Style TargetType="{x:Type RadioButton}" 
         BasedOn="{StaticResource CategoryButton}"> 
        <Setter Property="GroupName" Value="MainGroup"/> 
        <Setter Property="IsChecked"> 
         <Setter.Value> 
         <MultiBinding Converter="{StaticResource ObjectEqualsConverter}" Mode="OneWay"> 
          <Binding Path="DataContext.ActivePageName" 
            RelativeSource="{RelativeSource AncestorType={x:Type Window}}" /> 
          <Binding Path="CommandParameter" RelativeSource="{RelativeSource Self}" /> 
         </MultiBinding> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </StackPanel.Resources> 
      <RadioButton Command="{Binding NavigateCmd}" 
         CommandParameter="CaptureView" 
         Margin="5" Content="Capture" 
      /> 

      <RadioButton Command="{Binding NavigateCmd}" 
         CommandParameter="LibraryView" 
         Margin="5" Content="Explore" 
      /> 

     </StackPanel> 
    </Border> 

    <!-- 
    Content is in the region named "Main". Modules register their own page 
    views and we navigate this region to show them as the user clicks the 
    page buttons. 
    --> 
    <ContentControl x:Name="Page" 
        prism:RegionManager.RegionName="{x:Static ga:Regions.Main}" 
        /> 
</DockPanel> 

</Window> 

我的主視圖模型

public class MainWindowVm : BaseVm 
{ 
    private readonly IRegionManager _regionManager; 
    private readonly IUnityContainer _container; 
    private readonly IModuleCatalog _catalog; 
    private readonly IEventAggregator _aggregator; 
    public MainWindowVm(
     IRegionManager rm, 
     IUnityContainer container, 
     IModuleCatalog catalog) 
    { 
     NavigateCmd = new Prism.Commands.DelegateCommand<string>(Navigate); 

     _regionManager = rm; 
     _container = container; 
     _catalog = catalog; 
    } 

    public string CurrentView => (string)_regionManager?.Regions[Regions.Main]?.ActiveViews?.FirstOrDefault()?.ToString() ?? ""; 
    public Prism.Commands.DelegateCommand<string> NavigateCmd { get; private set; } 
    public void Navigate(string path) 
    { 
     _regionManager.RequestNavigate(Regions.Main, path, NavigationCompleted); 
    } 



    private void NavigationCompleted(NavigationResult nr) 
    { 
     RaisePropertyChanged(""); 
    } 

    public string ActivePageName 
    { 
     get 
     { 
      // HACK: Currently I'm taking the fully qualified name of the 
      // current view (e.g. "MyCompany.LibraryModule.LibraryView") and just 
      // chopping off all namespaces to get the basic, unqualified 
      // view name (e.g. "LibraryView") which I am blithely assuming 
      // will perfectly match one of the Page buttons on the main 
      // window. This really needs to be improved and generalized... 

      var name = CurrentView; 
      var i = name.LastIndexOf('.'); 
      if (-1 != i) 
       name = name.Substring(i+1); 

      return name; 
     } 
    } 
}; 

我模塊使用RegisterTypeForNavigation註冊了自己的看法。例如:

public class LibraryModule : IModule 
{ 
    private readonly IRegionManager _regionManager; 
    private readonly IUnityContainer _container; 

    public LibraryModule(IRegionManager regionManager, IUnityContainer c) 
    { 
     _regionManager = regionManager; 
     _container = c; 
    } 

    public void Initialize() 
    { 
     _container.RegisterTypeForNavigation<LibraryView>(); 
    } 
} 

是否有一個簡單的方法來實現這個給定我的設置。我是否應該使用其他一些註冊/導航方法?

棱鏡已徹底改變,所有樣品似乎有點過時了,所以我感到茫然,不知道該做什麼這裏

回答

1

棱鏡已經改變得這麼厲害,所有樣品似乎有點過時如此我不知道該怎麼做

實際上,Prism for WPF在幾年中並沒有改變,所有的當前樣本都是最新的。所以,不知道你是基於這個聲明。

你在考慮這一切都是錯誤的。您正在註冊您的觀點,然後試圖向Prism詢問視圖名稱是什麼。相反,實際發生的事情是,您正在向Prism講述您的視圖名稱並進行相應的註冊。你已經有了這些數據,你只需要把它存儲在你可以要求的地方。您可能最好使用某種類型的ViewRegistrationService來管理哪些視圖名稱已註冊,並將它們添加到您的ItemsControl可綁定到的集合中。該服務應該是一個單身人士。

+0

從這個人自己的答案,謝謝! 當我說它已經改變我正在說4.0到5.0到6.x.我意識到這是幾年,但我是新來的,Pluralsight的介紹視頻比6.x早很多。總之,我正在做很多追趕。 我的計劃是通過搜索本地目錄來演變此代碼以發現我的模塊(從而發現我的視圖),在這種情況下,我不知道我提前做了什麼。我猜想區域經理是那種「發現」服務,但我一定會接受你的建議並創建自己的。 – Joe