[英]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.