当前位置: 首页 > news >正文

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,那时将在导航记录中保存实例(增加内存开销)而不是采用默认结合日志串行化后销毁实例。


 

http://www.lryc.cn/news/347166.html

相关文章:

  • 【FFmpeg】Filter 过滤器 ② ( 裁剪过滤器 Crop Filter | 裁剪过滤器语法 | 裁剪过滤器内置变量 | 裁剪过滤器常用用法 )
  • thinkphp5 中控制器的创建和使用方法
  • [Linux] 常用服务器命令(持续更新)
  • 编译官方原版的openwrt并加入第三方软件包
  • PC适配移动端
  • springboot+vue+mybatis灵活就业服务平台+PPT+论文+讲解+售后
  • Android 13 系统自定义安全水印
  • C# WCF服务(由于内部错误,服务器无法处理该请求。)
  • 利用github pages建立Serverless个人博客
  • Spring Boot 集成 sa-token 实践教程
  • CSS:盒子模型
  • django中的cookie与session
  • 环形链表(判断链表中是否有环)的讲解
  • NLP(14)--文本匹配任务
  • MySQL——系统变量
  • 「 网络安全常用术语解读 」漏洞利用预测评分系统EPSS详解
  • 理解python中的Iterator 和 Iterable 迭代器和可迭代对象
  • C语言实现动态加载.so动态库,使用,错误捕获以及卸载
  • 《动手学深度学习》V2(11-18)
  • web前端之excel转pdf、小黄人发送请求、base64、jspdf、xlsx
  • 【面试题】音视频流媒体高级开发(2)
  • 数据与结构--堆
  • Github的使用教程(下载项目、寻找开源项目和上传项目)
  • Linux-线程概念
  • js的桶排序
  • 解决ubuntu无法上网问题
  • 使用nvm管理多版本node.js
  • 推导 模型矩阵的逆转置矩阵求运动物体的法向量
  • 定时任务的几种实现方式
  • docker部署springboot+Vue项目