简体   繁体   English

在Windows Phone 8.1后面的代码中创建新页面

[英]Create new page in code behind Windows Phone 8.1

Currently in Windows Phone 8.1 if I want to navigate to a new Page, first I must create a new Page item - MyNewPage - that produces a XAML and CS file. 当前在Windows Phone 8.1中,如果我要导航到新页面,则首先必须创建一个新页面项MyNewPage该项目将生成XAML和CS文件。 The in order to navigate to it I do the following: 为了导航到它,我执行以下操作:

Frame.Navigate(typeof(MyNewPage));

Now, I want to know if it is possible to create a new Page in code behind and navigate to it, something like: 现在,我想知道是否有可能在后面的代码中创建一个新页面并导航到它,例如:

Page myNewPage = new Page();
Frame.Navigate(typeof(myNewPage));

Since the Navigate method only accepts a typeof() , how can I accomplish this? 由于Navigate方法仅接受typeof() ,我该如何做到这一点?

This link says that you can pass any object as a 2nd parameter for the another version of Frame.Navigate method. 该链接说,您可以将任何对象作为第二个参数传递给另一个版本的Frame.Navigate方法。 I think you can use it (i suggest that you want to fill some properties from code behind from your new page or smth...). 我认为您可以使用它(我建议您要从新页面或smst后面的代码中填充一些属性。)。 Also you can access your page from Frame after navigation. 您也可以在导航后从“框架”访问页面。 Think about it, it may helps. 考虑一下,可能会有所帮助。

var root = Window.Current.Content as Frame;
        var mainPage = root.Content as Page;

Thanks to @gunr2171's wild guess , I was able to make it work. 由于@ gunr2171的疯狂猜测 ,我得以使其工作。 So the final code looks like this: 因此,最终代码如下所示:

Page myNewPage = new Page();
Type pageType = myNewPage.GetType();
Frame.Navigate(pageType);

or to make it simpler: 或更简单点:

Page myNewPage = new Page();
Frame.Navigate(myNewPage.GetType());

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

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