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);
    }
}

猜你喜欢

转载自www.cnblogs.com/wzwyc/p/10227425.html
UWP
今日推荐