0
我在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文檔中找到任何可以實現此目標的內容。
如果我以錯誤的方式解決這個問題,並且有更簡單的方法來完成這個,那會是什麼?
感謝
您是否嘗試過將頁面名稱作爲參數傳遞?例如,您可以創建包含頁面名稱的自定義對象以及要發送的數據,然後在主頁面中檢查頁面名稱並相應地處理數據。 – Pratyay