如何确定您刚才来自OnNavigatedTo函数的页面?

问题描述:

我在C#中创建一个UWP应用程序。我试图在OnNavigatedTo函数中运行不同的代码块,具体取决于哪些页面发送给我。现在我有,否则声明确定哪些代码块运行取决于哪些页面发送给我现在的页面。如何确定您刚才来自OnNavigatedTo函数的页面?

我现有的代码如下所示:

protected override void OnNavigatedTo(NavigationEventArgs e) 
    //runs every time MainPage is Navigated to from another page or when program is first started and MainPage loads 
    { 
     if ([SomeAttribute == Page2]) 
     { 
      //attempt to add string from page[2] to List 
      if (e.Parameter is string && !string.IsNullOrWhiteSpace((string)e.Parameter) && !InspectorInst.Names_list.Contains(e.Parameter)) 
      //if thing passed from previous page (e.Parameter) is a string and isnt null or whitespace and isnt already in the Names_list 
      { 
       string s = e.Parameter.ToString();//create string to hold e.Parameter 
       this.InspectorInst.Names_list.Add(s);//Add string to Names_list 
      } 
     } 
     else{ 
      //do something else 
     } 
     base.OnNavigatedTo(e); 
    } 

[SomeAttribute ==第2页]应该是如果指示我目前我的页面在页面上是第二页,将返回true。 SomeAttribute会返回发送给我当前页面的页面。我无法在UWP文档中找到任何可以实现此目标的内容。

如果我以错误的方式解决这个问题,并且有更简单的方法来完成这个,那会是什么?

感谢

+0

您是否尝试过将页面名称作为参数传递?例如,您可以创建包含页面名称的自定义对象以及要发送的数据,然后在主页面中检查页面名称并相应地处理数据。 – Pratyay

我认为,如果你想有一个页面有不同的行为,取决于它被调用时,正常的方法是通过不同的值作为第二个参数的导航方法。

如果你绝对想知道哪个页面调用,那么您可以检查在导航堆栈中的最后一项,是这样的:

var entry = this.Frame.BackStack.LastOrDefault(); 
if (entry != null) 
// Check entry.SourcePageType - it contains the type of the previous page 

注意,你可能还需要检查NavigationEventArgs.NavigationMode到看看用户是否从页面返回。