好吧,我的理解可能有點緊張,因爲我沒有使用WinRT或INavigationService
,但我認爲Frame
是RT的一部分,INavigationService
提供了視圖模型解析和導航框架。
我的另一個假設是,你的框架是一個有點像你的指揮已經,並且當你調用「導航()」在框架上,它只是替換新指定的內容框架的內容。如果是這種情況,那麼CM正在查看視圖模型的第一個分辨率。
既然你想要去的導體路徑,這聽起來像你想溝INavigationService
的CM執行,只是推出自己來處理INavigationService
導航方法(例如跳過Frame
小號Navigate()
法)。
快速查看CM源代碼,發現所有NavigationService
正在處理框架上的Navigate
事件,然後執行VM解析並設置視圖(導線可能已經這樣做)。所有你需要做的是確保你的INavigationService
實現只是加載指定的視圖到外殼,而不是導航框架
你可能只需要盜取構造函數代碼NavigationService
和改變Navigate()
執行,然後就打電話ActivateItem(x)
其中x是VM的實例。 CM會照顧其餘的(我認爲CM boostrapper已經設置了你的根'Frame',所以你不需要擔心這個)。
例如
的實現可能看起來更像這個(要記住,這只是我的東西放在一起,並且可以無恥的謊言!):
public class NewFrameAdapter : INavigationService
{
private readonly Frame frame;
private readonly IConductActiveItem shell;
private event NavigatingCancelEventHandler ExternalNavigatingHandler = delegate { };
public NewFrameAdapter(Frame frame)
{
this.frame = frame;
// Might want to tighten this up as it makes assumptions :)
this.shell = (frame as FrameworkElement).DataContext as IConductActiveItem;
}
public bool Navigate(Type pageType)
{
// Do guardclose and deactivate stuff here by looking at shell.ActiveItem
// e.g.
var guard = shell.ActiveItem as IGuardClose;
if (guard != null)
{
var shouldCancel = false;
guard.CanClose(result => { shouldCancel = !result; });
if (shouldCancel)
{
e.Cancel = true;
return;
}
}
// etc
// Obviously since the guard is probably async (assume it is, if not you are ok to continue!) you'd have to not call this code right
// here but I've just stuck it in here as an example
// edit: looking at the code above (the guard code) it looks like this is all sync so the below code should be fine
// You might get away with calling shell.ActivateItem(pageType) as I'm not sure
// if the viewmodel binder in RT would resolve this all for you, but if it doesnt...
// Init the view and then resolve the VM type
ViewLocator.InitializeComponent(pageType);
var viewModel = ViewModelLocator.LocateForView(pageType);
// Activate the VM in the shell)
shell.ActivateItem(viewModel);
}
按照自己的方式推出這個應該不會太困難。這對你有幫助嗎?
那麼你的XAML將是非常簡單的:
<Frame blah blah>
<SomeStaticContent />
<ContentControl x:Name="ActiveItem" /> <!-- The dynamic bit... -->
<SomeMoreStaticContent />
</Frame>
我在想,這大概是鑑於一和視圖模型,第一,因爲你的根Frame
將使用視圖第一的混合,以及你的售票員將使用ActivateItem()
這需要一個視圖模型,然後解決視圖時,活頁夾踢,但如果我的假設是好的,它應該工作
你有沒有想過這個傢伙?我看了一些在WPF中的東西,但它只是太多的工作來嘲笑它的快速回答 – Charleh
我從來沒有想過如何讓它工作,但我改變了我的設計,使我有現在決定不實施主頁面方案。如果你最終增加一個解決方案,我會驗證它的工作原理並接受答案。感謝您的幫助! –