简体   繁体   English

在C#UWP中根据类型创建页面

[英]create Page from Type in C# UWP

I saw this UWP check the current page for name or instance question on stackoverflow. 我看到此UWP检查当前页面上是否有关于stackoverflow的名称或实例问题。 It mentions I can get the page's Type by currentFrame.SourcePageType . 它提到我可以通过currentFrame.SourcePageType获取页面的Type。 my question is: once I have the type of the page active in the current Frame , how can I access elements of that page, eg a TextBox? 我的问题是:一旦我在当前Frame激活了页面的类型,如何访问该页面的元素,例如TextBox?

Not sure what you are trying to do, but that's cool. 不确定您要做什么,但这很酷。 Do it like this: 像这样做:

Activator.CreateInstance(null, PageType.ToString());

Once instantiated, a page cannot be added to the Frame , so that's a thing you should remember if it's in the back of our mind to nav to an instance. 一旦实例化后,就无法将页面添加到Frame ,因此,如果导航到实例是在我们的脑海中的,那您应该记住这一点。

To access controls, I think you mean, you will need to make them public by doing this: <Control x:Name="MyControl" x:FieldModifier="Public" /> The x:ModifyField directive is the key. 要访问控件,我想您的意思是,您需要通过以下操作将它们公开: <Control x:Name="MyControl" x:FieldModifier="Public" /> x:ModifyField指令是关键。

More: https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-fieldmodifier-attribute 更多: https : //docs.microsoft.com/zh-cn/windows/uwp/xaml-platform/x-fieldmodifier-attribute

Opps, there's this, too 哦,也是这个

You aren't wanting to instantiate the type, which is what I first thought you wanted to do, now I see you just want to access the Page currently visible in the Frame , that's so much easier. 你是不是想实例的类型,这是我第一个想到你想做的事,现在我看你只是要访问的Page当前可见的Frame ,这是容易得多。

var p = _frame.Content as Page;
p.MyControl.Text = "Hello world";

Best of luck! 祝你好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM