【发布时间】:2017-08-09 04:44:00
【问题描述】:
我正在用 C# 创建一个 UWP 应用。我正在尝试在 OnNavigatedTo 函数中运行不同的代码块,具体取决于将我发送到那里的页面。现在我有 if else 语句来确定运行哪些代码块,具体取决于哪个页面将我发送到我现在所在的页面。
我现有的代码如下所示:
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 == Page2] 如果将我引导到我当前所在页面的页面是 Page2,则应该返回 true。 SomeAttribute 会返回将我发送到我当前所在页面的页面。我无法在 UWP 文档中找到任何可以实现此目的的内容。
如果我以错误的方式解决这个问题,并且有更简单的方法来解决这个问题,那会是什么?
谢谢
【问题讨论】:
-
您是否尝试过将页面名称作为参数传递?例如,您可以创建一个自定义对象,其中包含您的页面名称以及要发送的数据,然后在您的主页中,您可以检查页面名称并相应地处理数据..