1. 程式人生 > >UWP開發學習筆記1

UWP開發學習筆記1

導航到頁面:

this.Frame.Navigate(typeof(SecondPage));

 


導航進入當前頁面時會呼叫OnNavigatedTo方法;
導航從當前頁面離開時會呼叫OnNavigatingFrom方法
導航時傳遞引數採用:

this.Frame.Navigate(typeof(SecondPage), "這裡是引數");

 


接收頁面獲取引數:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.Parameter!=null && e.Parameter is
string) { tbkMessage.Text = e.Parameter as string; } base.OnNavigatedTo(e); }

 

管理導航記錄
兩個導航按鈕:

<Page.BottomAppBar>
    <AppBar>
        <StackPanel Orientation="Horizontal">
            <AppBarButton Click="OnBack" Label="上一頁" Icon="Back"/>
            <AppBarButton Click="
OnForward" Label="下一頁" Icon="Forward"/> </StackPanel> </AppBar> </Page.BottomAppBar>

 


程式碼:

private void OnBack(object sender, RoutedEventArgs e)
{
    if (this.Frame.CanGoBack)
    {
        this.Frame.GoBack();
    }
}

private void OnForward(object sender, RoutedEventArgs e)
{
    
if (this.Frame.CanGoForward) { this.Frame.GoForward(); } else { this.Frame.Navigate(typeof(PageA)); } }

 

檢視導航模式:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    tbkMessage.Text = "導航模式:" + e.NavigationMode.ToString();

    base.OnNavigatedTo(e);
}

 

從PageA跳轉到PageB,然後又從PageB跳轉到PageC,後面返回時不希望返回PageB,而是直接到PageA,可以在PageC的OnNavigatedTo方法加入以下程式碼:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var page = this.Frame.BackStack.FirstOrDefault(p => p.SourcePageType == typeof(PageB));
    if (page != null)
    {
        Frame.BackStack.Remove(page);
    }
    base.OnNavigatedTo(e);
}

 


處理手機上的“後退”鍵:
新增“Windows Mobile Extensions for the UWP”的引用
在App類的OnLaunched方法中加入以下程式碼:

if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
    Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

 


然後新增HardwareButtons_BackPressed方法:

private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame != null)
    {
        if (rootFrame.CanGoBack)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }
}

 


在App類的OnLaunched方法中新增以下程式碼:
rootFrame.CacheSize = 2;
表示快取兩個頁面例項
然後在要快取的頁面的建構函式新增程式碼:

public SecondPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Enabled;
}

 


NavigationCacheMode有三個值型別
Required:快取頁面,並且每次訪問時都重複利用快取的例項,而不考慮幀的快取大小。
Enabled:快取頁面,但當超過幀的快取大小時放棄快取的例項。
Disabled:從不快取頁面,每次訪問時建立頁面的新例項。

儲存和恢復導航狀態
GetNavigationState:將 Frame 導航歷史記錄序列化為字串。
SetNavigationState:從提供的序列化字串中讀取 Frame 的導航歷史記錄並還原。
在App類的OnSuspending方法中加入以下程式碼:

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();

    Frame rootFrame = Window.Current.Content as Frame;
    string navstate = rootFrame.GetNavigationState();
    var localSettings = ApplicationData.Current.LocalSettings;
    localSettings.Values["nav"] = navstate;

    deferral.Complete();
}

 

在OnLaunched方法中加入程式碼:

if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
    object value;
    var localSettings = ApplicationData.Current.LocalSettings;
    if (localSettings.Values.TryGetValue("nav", out value))
    {
        rootFrame.SetNavigationState(value as string);
    }
}