我正在使用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>();
}
}
是否有一個簡單的方法來實現這個給定我的設置。我是否應該使用其他一些註冊/導航方法?
棱鏡已徹底改變,所有樣品似乎有點過時了,所以我感到茫然,不知道該做什麼這裏
從這個人自己的答案,謝謝! 當我說它已經改變我正在說4.0到5.0到6.x.我意識到這是幾年,但我是新來的,Pluralsight的介紹視頻比6.x早很多。總之,我正在做很多追趕。 我的計劃是通過搜索本地目錄來演變此代碼以發現我的模塊(從而發現我的視圖),在這種情況下,我不知道我提前做了什麼。我猜想區域經理是那種「發現」服務,但我一定會接受你的建議並創建自己的。 – Joe