2012-03-01 35 views
-2

現在,我擁有的是,當用戶點擊頁面時,頁面會自動將用戶發送到打開pdf文件的web任務。導航循環和清除後備棧

現在發生的事情是,當用戶按下後退按鈕時,它會在返回到原始頁面之前重新導向到pdf,因爲我已將它分配給(由於onnavigateto功能)

我該如何做到這一點,以便當用戶點擊pdf文檔中的後退按鈕時,應用程序會將用戶帶回主頁面?

此外,在主頁上,我如何確保清除後臺? (由於應用程序已經退出MainPage執行,所以不能回去的PDF文件。)

到目前爲止我的代碼,我已經試過......

{ 
public partial class Page2 : PhoneApplicationPage 
{ 
public Page2() 
{ 
    InitializeComponent(); 
} 

//as soon as this page is opened, navigate/redirect it to the URL below 

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    WebBrowserTask task = new WebBrowserTask() { URL ="http://test.com/test.pdf"}; 
    task.Show(); 

} 

//when the user clicks the hardware back button, instead of taking them to the daily notices, which will send them back to brower 
// send the user to the main page 

protected override void OnBackKeyPress 

       (System.ComponentModel.CancelEventArgs e) 
{ 

    base.OnBackKeyPress(e); 

    new Uri("/MainPage.xaml", UriKind.Relative); 


} 

回答

1

首先,你爲什麼需要僅打開WebBrowserTask的第二頁?你可以從主頁面做到這一點。

如果您仍然希望從第二頁打開,您可以將WebBrowserTask移動到構造函數中並使用Dispatcher包圍它。這種方法保證WebBrowserTask在導航到這個頁面後只會被調用一次(也許會有一些墓碑問題)。或者,您可以將狀態保存到PhoneApplicationPage.State以處理用戶的位置以及接下來應該打開的位置。

對於清算回堆棧,你可以使用下面的代碼:

while (NavigationService.BackStack.Any()) 
{ 
    NavigationService.RemoveBackEntry(); 
} 
0

您必須檢測這種應用水平,而不是頁面級別。當您將用戶重定向到PDF時,您的應用程序將被暫停。當他們然後導航回來時,它被恢復。

Visual Studio的模板提供,它調用當應用程序恢復的方法:

// Code to execute when the application is activated (brought to foreground) 
// This code will not execute when the application is first launched 
private void Application_Activated(object sender, ActivatedEventArgs e) 
{ 
} 

在上面的方法,你可以設置一個標誌,你再檢查您的網頁時瀏覽到該,,表示已發生簡歷。