7gegenTheben
7gegenTheben

Reputation: 81

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, e.g. a TextBox?

Upvotes: 0

Views: 268

Answers (1)

Jerry Nixon
Jerry Nixon

Reputation: 31831

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://learn.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!

Upvotes: 1

Related Questions