2017-08-08 27 views
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文檔中找到任何可以實現此目標的內容。

如果我以錯誤的方式解決這個問題,並且有更簡單的方法來完成這個,那會是什麼?

感謝

+0

您是否嘗試過將頁面名稱作爲參數傳遞?例如,您可以創建包含頁面名稱的自定義對象以及要發送的數據,然後在主頁面中檢查頁面名稱並相應地處理數據。 – Pratyay

回答

2

我認爲,如果你想有一個頁面有不同的行爲,取決於它被調用時,正常的方法是通過不同的值作爲第二個參數的導航方法。

如果你絕對想知道哪個頁面調用,那麼您可以檢查在導航堆棧中的最後一項,是這樣的:

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

注意,你可能還需要檢查NavigationEventArgs.NavigationMode到看看用戶是否從頁面返回。

相關問題