2016-08-15 67 views
0

我有問題將對象傳遞給MVVM light WPF中的輔助視圖。我有主視圖模型。 follow of operation。我能夠使用MVVM light和Modren UI導航服務進行連接。問題是我無法將主要客戶視圖模型的對象發送到輔助視圖模型。我想從源視圖模型設置目標視圖的數據上下文。我已經嘗試過,但似乎沒有工作。我更喜歡沒有代碼,我花了很多時間沒有成功。WPF MVVM Light和modren UI-將對象傳遞到輔助視圖

public virtual void NavigateTo(string pageKey, object parameter) 
    { 
     lock (_pagesByKey) 
     { 
      if (!_pagesByKey.ContainsKey(pageKey)) 
      { 
       throw new ArgumentException(string.Format("No such page: {0}. Did you forget to call NavigationService.Configure?", pageKey), "pageKey"); 
      } 

      var frame = GetDescendantFromName(Application.Current.MainWindow, "ContentFrame") as ModernFrame; 

      // Set the frame source, which initiates navigation 
      if (frame != null) 
      { 
       frame.Source = _pagesByKey[pageKey]; 
       //i Dont know if this should work or not 
       frame.DataContext = parameter; 

      } 
      Parameter = parameter; 
      _historic.Add(pageKey); 
      CurrentPageKey = pageKey; 
     } 
    } 

任何幫助將不勝感激。我只需要如何設置目標視圖的datacontext而不使用代碼。謝謝

回答

0

存在多種可能性,但不會在視圖模型之間創建依賴關係的方法是使用pub/sub system in MVVMLight。基本上它是這樣的:

當你從你的視圖中選擇一些實體並轉換到另一個實體時,viewmodel發送一個消息,說明給定實體的載體。在另一個視圖模型中,您收到消息並相應地設置一些屬性(用於編輯,添加新實體等)

// mainviewmodel 
Messenger.Default.Send(new MyMessage(myObj)); 

// otherviewmodel 
Messenger.Default.Register<MyMessage>(this, message => 
{ 
    /* do something with message.MyObj */ 
}); 

// mymessage 
public class MyMessage : MessageBase 
{ 
    ... 
    public MyObj MyObj { get; set; } 
}