NavigationService簡介

對於NavigationService類來講在WP7上是十分重要的概念從.Net 3開始出現,主要用於打開新的對話框和相互之間的跳轉,NavigationService類的相關成員和方法和瀏覽器有些類似,同時 NavigationService的實現和Android平臺管理Activity很像,基於棧的實現。 瀏覽器

System.Windows.Navigation.NavigationService類 異步

1、主要屬性 測試

CanGoBack 判斷是否能後退
CanGoForward 判斷是否能前進
Content 獲取或設置一個引用對象包含當前內容
CurrentSource獲取當前內容最後一個URI
Source 獲取或設置URI從當前內容 this

2、主要方法 spa

GoBack 後退從歷史棧從,通常執行該項必須有至少一個歷史,能夠先經過上面介紹的 CanGoBack 屬性來測試是否容許後退。
GoForward 前進這個用法同上,前進的概念在瀏覽器中很常見,這裏再也不贅述
Refresh 從新載入當前頁面
StopLoading 中止加載當前頁
Navigate() 有關Navigate的重載版本比較多,Navigate是基於NavigationService類異步方式加載指定的URI或對象。 orm

3、主要事件 (下面例子中progressStatusBarItem是一個簡單的TextBlock控件) 對象

1. LoadCompleted使用示例 事件

void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
string msg = string.Format("{0} 加載完成.", e.Uri.OriginalString);
this.progressStatusBarItem.Content = msg;
}

2. Navigated使用示例 string

void NavigationService_Navigated(object sender, NavigationEventArgs e)
{
string msg = string.Format("正在下載 {0}.", e.Uri.OriginalString);
this.progressStatusBarItem.Content = msg;
} it

3. Navigating使用示例

void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
// Zune123提示,這裏不容許刷新靜態頁

if ((e.NavigationMode == NavigationMode.Refresh) &&
(e.Uri.OriginalString == "StaticPage.xaml"))
{
e.Cancel = true;
}
}

4. NavigationFailed使用示例

void NavigationService_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
string msg = string.Format("導航到 {0} 失敗 {1}.", e.Uri.OriginalString, e.Exception.Message);
this.progressStatusBarItem.Content = msg;
}

5. NavigationProgress使用示例

void NavigationService_NavigationProgress(object sender, NavigationProgressEventArgs e)
{
string msg = string.Format("{0} of {1} 字節已經接收", e.BytesRead, e.MaxBytes);
this.progressStatusBarItem.Content = msg;
}

6. NavigationStopped使用示例

void NavigationService_NavigationStopped(object sender, NavigationEventArgs e)
{
this.progressStatusBarItem.Content = "導航中止";
}

最後有關NavigationService類經典的對話框跳轉代碼爲 NavigationService.Navigate(new Uri("/MyPage.xaml", UriKind.Relative));

相關文章
相關標籤/搜索