2010-05-09 67 views
4

Windows Phone 7中的導航框架是Silverlight中的內容的簡化版本。你只能導航到Uri,而不能通過視圖。由於NavigationService綁定到View,因此人們如何將它融入到MVVM中。例如:使用頁面導航的MVVM在Windows Phone 7上

public class ViewModel : IViewModel 
{ 
    private IUnityContainer container; 
    private IView view; 

    public ViewModel(IUnityContainer container, IView view) 
    { 
     this.container = container; 
     this.view = view; 
    } 

    public ICommand GoToNextPageCommand { get { ... } } 

    public IView { get { return this.view; } } 

    public void GoToNextPage() 
    { 
     // What do I put here. 
    } 
} 

public class View : PhoneApplicationPage, IView 
{ 
    ... 

    public void SetModel(IViewModel model) { ... } 
} 

我正在使用Unity IOC容器。我必須首先解析我的視圖模型,然後使用View屬性來獲取視圖並顯示它。但是使用NavigationService,我必須通過Uri視圖。我無法首先創建視圖模型。有沒有辦法解決這個問題。

回答

4

而不是通過構造函數傳遞視圖。您可以先通過NavigationService構建視圖並將其傳遞給視圖模型。像這樣:

public class ViewModel : IViewModel 
{ 
    private IUnityContainer container; 
    private IView view; 

    public ViewModel(IUnityContainer container) 
    { 
     this.container = container; 
    } 

    public ICommand GoToNextPageCommand { get { ... } } 

    public IView 
    { 
     get { return this.view; } 
     set { this.view = value; this.view.SetModel(this); } 
    } 

    public void GoToNextPage() 
    { 
     // What do I put here. 
    } 
} 

PhoneApplicationFrame frame = Application.Current.RootVisual; 
bool success = frame.Navigate(new Uri("View Uri")); 

if (success) 
{ 
    // I'm not sure if the frame's Content property will give you the current view. 
    IView view = (IView)frame.Content; 
    IViewModel viewModel = this.unityContainer.Resolve<IViewModel>(); 
    viewModel.View = view; 
} 
0

我的看法是,視圖模型應該是在應用程序啓動時創建並註冊。通過將其放置在根DataContext中,所有頁面都將自動獲得對其的引用,而無需任何代碼隱藏或IoC技巧。

// Code to execute when the application is launching (eg, from Start) 
    // This code will not execute when the application is reactivated 
    private void Application_Launching(object sender, LaunchingEventArgs e) 
    { 
     m_ViewModel = new PrimaryViewModel(RootFrame) ; 
     RootFrame.DataContext = m_ViewModel; 
    } 

    // Code to execute when the application is activated (brought to foreground) 
    // This code will not execute when the application is first launched 
    private void Application_Activated(object sender, ActivatedEventArgs e) 
    { 
     m_ViewModel = new PrimaryViewModel(RootFrame) ; 
     m_ViewModel.Activated(PhoneApplicationService.Current.State); 
     RootFrame.DataContext = m_ViewModel; 
    } 
0

如果您使用MVVM架構,那麼您可以在使用Messenger註冊後傳遞navigationPage。用一個字符串(比如說PageName)變量創建一個模型類(比如NavigateToPageMes​​sage)。你想通過從homepage.xaml字符串newpage.xaml,然後在首頁視圖模型只是發送郵件這樣的指揮下你綁定(比如說HomeNavigationCommand)

private void HomeNavigationCommandHandler() 
     { 
      Messenger.Default.Send(new NavigateToPageMessage {PageName = "newpage"}); 
     } 

在NEWPAGE視圖模型,你應該註冊的使者這樣,

Messenger.Default.Register<NavigateToPageMessage>(this, (action) => ReceiveMessage(action)); 

private object ReceiveMessage(NavigateToPageMessage action) 
     { 
      var page = string.Format("/Views/{0}.xaml", action.PageName);   
      NavigationService.Navigate(new System.Uri(page,System.UriKind.Relative)); 
      return null; 
     } 

//假設你的意見是在查看文件夾