WPF之页的使用

1,Page介绍。

  •   Page直接从FrameworkElement中派生出来,WIndow从ContentControl中派生。
 [Localizability(LocalizationCategory.Ignore)]
    public class Window : ContentControl, IWindowService
    {....}
[ContentProperty("Content")]
    public class Page : FrameworkElement, IWindowService, IAddChild
    {....}
  • 尽管在设计时Page是顶级用户界面元素,但是在运行时Page不是顶级容器。
<Page x:Class="页面导航.HomePage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:页面导航"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="HomePage" WindowTitle="页面导航应用" Loaded="Page_Loaded">

    <Grid>
       
    </Grid>
</Page>
  •  运行时页面被驻留在另一个容器中,它可以使用几个不同的容器之一。
  1. NavigationWindow,他是Window类的派生类(Application.StartupUri=页面,作为启动页时,它将自动创建一个顶级容器NavigationWindow,并在其Content中添加当前Page。)。
  2. 位于另一个窗口中的框架(Frame)
  3. 位于另一个页面中的框架(Frame)
  4. 直接驻留与浏览器中的框架(Frame)
  • 页面不能指定大小,它的大小由宿主决定。
  • Page.WindowTitle:窗口标题,Page.Title:页面标题即在导航历史中显示的标题

2,Page使用的场景。

  • 在App.Xaml中设置启动uri为页面。
<Application x:Class="页面导航.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:页面导航"
             StartupUri="HomePage.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

        启动时,自动创建一个顶级容器NavigationWindow,并在其Content中添加当前Page(HomePage.xaml)。

  • 通过Frame控件可以将Page嵌入到窗口中,Frame是内容控件可以添加任何元素。
<Window x:Class="页面导航.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:页面导航"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Frame x:Name="frame01"   Grid.Column="1" Source="HomePage.xaml" BorderBrush="BlueViolet" BorderThickness="1" Margin="5"></Frame>
    </Grid>
</Window>

3,获取导航服务。

  • 属性:

        1,Frame.NavigationService.

        2,Page.NavigationService

  • 方法:

        1,NavigationService.GetNavigationService(this)(this 为Page);

  • 作用:通过导航服务可完成页面导航,跳转,事件监听等。
  public partial class MainWindow : Window
    {
        NavigationService service1;
       
        public MainWindow()
        {
            InitializeComponent();
            //获取服务
            service1 = frame01.NavigationService;          
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            service1 = frame01.NavigationService;          

        }
        private void btnHomePage_Click(object sender, RoutedEventArgs e)
        {
            service1.Navigate(new Uri("pack://application:,,,/HomePage.xaml"));
        }

        private void btnTestPage_Click(object sender, RoutedEventArgs e)
        {
            service1.Navigate(new Uri("TestPage.xaml",UriKind.Relative));
        }


    }
  • 注意事项:

        在页面(是Page不是Window)的构造函数中或者引发Page.Initialized事件时,是不能获取NavigationService.应当使用Page.Loaded事件。

 public partial class HomePage : Page
    {
        public HomePage()
        {
            InitializeComponent();
            var tt = this.NavigationService;
            
            
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var ss = this.NavigationService;
            var s2 = NavigationService.GetNavigationService(this);
        }
    }

4,相关属性

Page.KeepAlive:是否在导航历史中保留Page实例。

Page.NavigationService:获取页的导航服务。

Page.ShowsNavigationUI:导航UI是否显示。

Frame.JournalOwnership将记录添加到父容器导航历史中。

Frame.NavigationUIVisibility:导航UI是否在Frame中显示。

5,生命周期。

        通过链接和历史记录进行页面跳转有本质区别,通过链接跳转是建立了新的对象,跳转时不进行序列化操作。通过历史记录是WPF检查页面的整个元素树,并且查看所有元素的依赖属性,对少量额外的元数据进行保存,即日志标志,日志标志指示他们应当序列化后保存在名为journal的导航日志中(当注册依赖属性时,使用FrameworkPropertyMetadata对象设置日志标志)然后销毁该页,减少内存消耗。比如页面文本框中的数据,如果通过链接跳转,因为是建立新的对象该文本框中的数据将不再存在,而如果通过历史记录进行跳转则该文本框的数据表现为依旧存在。

        以下为TextBox.TextProperty的定义示例,在元数据中声明了类型标记为日志(FrameworkPropertyMetadataOptions.Journal)。

TextBox.TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextBox), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, new PropertyChangedCallback(TextBox.OnTextPropertyChanged), new CoerceValueCallback(TextBox.CoerceText), true, UpdateSourceTrigger.LostFocus));

        总之一句:若想在返回历史记录中保存页面某一数据(保存过程就是序列化具有日志标志的依赖属性的过程),该数据必须是具有日志标志的依赖属性。
        若想保存实例请将Page.KeepAlive=true,那时将在导航记录中保存实例(增加内存开销)而不是采用默认结合日志串行化后销毁实例。


 

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值