2016-09-23 18 views
0

我是MVVM和MVVMLight Toolkit的新手。因此,我正在使用基於Uri進行導航的項目,該項目設置在以視圖形式公開的Frame上:x:Name。 這是我的代碼..如何獲取當前呈現UserControls在MainWindow作爲一個孩子?

public virtual void SetFrame(Uri userControll, string FrameElementName) 
{ 
    var mainWindow = Application.Current.MainWindow; 
    var frame = mainWindow.FindName(FrameElementName) as Frame; 

    if (frame != null) 
    { 
      frame.Source = userControll; 
    } 
} 

,如果我們想要導航功能上下工夫當前呈現UserControll哪個是主窗口的孩子呢?我們必須執行哪些步驟? 像

public virtual void SetFrame(Uri anotherUserControll, string FrameElementName) 
{ 
    var userControl= // what to do to get Currently rendered Usercontroll as child of MainWindow? 
    var frame = userControl.FindName(FrameElementName) as Frame; 

    if (frame != null) 
    { 
     frame.Source = anotherUserControll;  
    } 
} 

回答

0

我鑄造Frame.Context爲用戶控件來得到當前渲染用戶控件解決了這個問題.. 這裏

public virtual void SetFrame(Uri anotherUserControll, string FrameElementName) 
    { 
     var mainFrame =(Frame)Application.Current.MainWindow.FindName("MainFrame"); 
     var userControll = mainFrame.Content as UserControl; 
     var frame = userControll.FindName(contentControll) as Frame; 
     if (frame != null) 
     { 
      frame.Source = anotherUserControll; 
     }   
    } 
相關問題