多窗口切换

使用Frame+Page的组合实现多窗口

MainPage是一个外框,包含顶部和抽屉菜单,主体部分为空,引用newslistView.xaml。

遇到第一个问题是如何点击newslistView里的内容,把MainPage换成contentView.xaml

首先借鉴网上内容,在MainPage里添加一个RoutedEventHandler,用来响应newslistView里的点击动作(还需要先在newslistView.xaml.cs里创建一个public的组件,并把需要响应的组件赋值给它)。
但是这个方法比较麻烦,要把MainPage整体包在一个Frame里,然后添加一堆代码内容。而且目前还不支持硬件的后退按钮。所以又看微软出的通用应用示例Windows-universal-samples-master/BackButton,直接使用Window.Current就可以解决页面切换的问题。

Frame rootFrame = Window.Current.Content as Frame;  
contentView articleFrame = new contentView();  
temp = e.AddedItems[0] as NewsListItem;  
(sender as ListView).SelectedItem = null;  
Uri webLink = new Uri(temp.origin);  
articleFrame.setAddress(webLink);  
rootFrame.Content = articleFrame;  

后退功能也使用这个示例里的方法

public App()
{
    //注册测试响应硬件返回事件
    if ("Windows.Mobile" == Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily)
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}

//按下硬件返回的处理
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    var rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

但是这种方法需要使用Page.Navigation,我现在是设置Frame.Content,不能直接导航。

posted on 2015-11-24 11:45  Woody桑  阅读(239)  评论(0编辑  收藏  举报

导航