简体   繁体   English

在Frame.Navigate中使用可变页面类型

[英]Using a variable page type in Frame.Navigate

Using WinRT, I am trying to implement navigation based on the users input. 使用WinRT,我试图基于用户输入来实现导航。 I tried to use a variable page type in the Navigate method. 我试图在Navigate方法中使用可变页面类型。 Unfortunately it doesn't look like it accepts variables as a page type. 不幸的是,它看起来好像不接受变量作为页面类型。 Does anybody know a way around this? 有人知道解决这个问题的方法吗?

object myPage = page2;

this.Frame.Navigate(typeof(myPage));

I could just create switch statements and put the whole instruction in each statement but that seems ungainly and hard to maintain, especially since I may end up having a great deal of page types. 我可以创建switch语句,然后将整个指令放在每个语句中,但这似乎很麻烦且难以维护,尤其是因为我最终可能拥有很多页面类型。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

Edit: A little clarification. 编辑:一点澄清。 I am writing a quiz program that stores the quizzes in a List, however the are multiple types of quizzes that require specific page formats so I need different page types. 我正在编写一个测验程序,将测验存储在列表中,但是有多种测验需要特定的页面格式,因此我需要不同的页面类型。 I am storing the the Page type as a string in the List, and the list is randomized so I don't know the next page type from the current test. 我将页面类型作为字符串存储在列表中,并且列表是随机的,所以我不知道当前测试的下一个页面类型。 The quiz engine will pre read the next quiz in the queue and extract the page type and create a variable to insert into the this.Frame.Navigate command. 测验引擎将预读队列中的下一个测验,并提取页面类型并创建一个变量以插入到this.Frame.Navigate命令中。 This is where I am having the problem. 这就是我遇到的问题。 Any ideas on how to get around this issue? 关于如何解决此问题的任何想法?

Thanks 谢谢

There is no any "variable" type. 没有任何“变量”类型。 var just allows you to write less code. var只允许您编写更少的代码。 Compiler substitutes return type of the expression as the variable type, there's no "magic". 编译器将表达式的返回类型替换为变量类型,没有“魔术”。

You can write it like this: 您可以这样写:

object myPage;

if (???)
    myPage = new Page1();
else
    myPage = new Page2();

this.Frame.Navigate(myPage.GetType());

If you want to implement navigation based on the user's input, you definely need to use if or switch statement. 如果要基于用户的输入实现导航,则必须使用if或switch语句。 You need to pass type of the page to Navigate() method. 您需要将页面的类型传递给Navigate()方法。 I don't really get what have you been trying to do with code above. 我真的不知道您在尝试使用上面的代码做什么。 I would do it by using simple if statement. 我会通过使用简单的if语句来做到这一点。

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

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