简体   繁体   中英

create Page from Type in C# UWP

I saw this UWP check the current page for name or instance question on stackoverflow. It mentions I can get the page's Type by currentFrame.SourcePageType . 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?

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.

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.

More: https://docs.microsoft.com/en-us/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.

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

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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