简体   繁体   English

如何在主窗口中以子级的形式获取当前呈现的UserControl?

[英]How to get Current Rendered UserControls In MainWindow as a Child?

I am new to MVVM and using MVVMLight Toolkit. 我是MVVM的新手,并使用MVVMLight Toolkit。 Thus i am working on project where Navigation Take place using based on Uri which is set on Frame exposed on View as x:Name. 因此,我正在基于Uri进行导航的项目,Uri是在View上以x:Name公开的Frame上设置的。 Here is my Code.. 这是我的代码。

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;
     }
 }

If we Want navigation function to Work on Currently Rendered UserControll Which is child of MainWindow? 如果我们想让导航功能在当前渲染的UserControll上运行,那是MainWindow的子级? What steps we have to perform? 我们必须执行哪些步骤? Like 喜欢

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;      
     }
 }

I solved this problem by casting Frame.Context as UserControl to get the Current Rendered UserControl.. Here 我通过将Frame.Context强制转换为UserControl来获取当前呈现的UserControl,从而解决了此问题。

    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;
         }           
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UserControls如何在MainWindow中访问文本框的值? - how do UserControls get access to a textbox's value in MainWindow? 在MainWindow中重用WPF UserControls - Reusing WPF UserControls in MainWindow 如何在 MainWindow 的 ContentControl 中获取当前用户控件? (MVVM) - How to get the current usercontrol inside a ContentControl in MainWindow? (MVVM) 如何单击设计器中的usercontrol子级? - How do I click a usercontrols child in designer? 如果在MainWindow视图上有2个UserControl,并且想在它们之间传递属性,该怎么做? - If I have 2 UserControls on a MainWindow view, and I want to pass a property between them, how do I do it? 如何获取我自己创建的 USERCONTROL 的列表 - How to get a list of my own created USERCONTROLs MVVM Light:如何从用户控件获取数据? - MVVM Light: How to get data from usercontrols? 如何从 MainWindow 上 NavigationView 控件的框架中的页面检索当前 WinUI 3 MainWindow 的 window 句柄 - How to retrieve the window handle of the current WinUI 3 MainWindow from a page in a frame in a NavigationView control on the MainWindow 如何从Page访问MainWindow视图模型? - How to get access to the MainWindow viewmodel from Page? 如何在 WPF 中给子窗口边界不超出 MainWindow - How to give a child window bounds to not go outside the MainWindow in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM