WPF基础笔记(1)App与控件

  1. WPF主要使用一下库:

  2. PresentationCore.dll 核心库

  3. PresentationFramework.dll 控件库

  4. System.Xaml.dll Xaml 解析库

  5. WindowBase 窗口基类库

  6. App类继承Application类几个重要的方法:
    //应用激活获取到焦点触发
    protected override void OnActivated(EventArgs e)
    { base.OnActivated(e); }
    //应用激活获取到焦点触发
    protected override void OnDeactivated(EventArgs e)
    { base.OnDeactivated(e); }
    //应用关闭时触发
    protected override void OnExit(ExitEventArgs e)
    { base.OnExit(e); }
    //应用程序启动时触发
    protected override void OnStartup(StartupEventArgs e)
    { base.OnStartup(e); }
    //系统关机时触发
    protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
    { base.OnSessionEnding(e); }

     常用属性:
     Application.MainWindow  应用的主窗体,不修改的话,默认为第一次show的窗体
    
     Application.Windows 应用包含的所有实例窗体的集合
    
     Application.ShouDownModel 应用关闭,生命周期结束的模式。三种枚举方式
    
     Application.Current 当前应用的属性
    
  7. Window窗口的常用属性
    Owner---->当前窗口属于那个窗口,会显示在该窗口前面
    Owners—>当前窗口是哪些窗口的Owner

  8. WPF全局异常的调用:
    this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);

    常用方法是将此放到OnStartup方法中调用。
    public partial class App : Application
    {
    protected override void OnStartup(StartupEventArgs e)
    {
    base.OnStartup(e);
    //注册Application_Error
    this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
    }
    //异常处理逻辑
    void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
    //处理完后,我们需要将Handler=true表示已此异常已处理过
    e.Handled = true;
    }
    }

    例子. private void button1_Click(object sender, RoutedEventArgs e)
    {
    throw new Exception(“我要抛异常”);
    }
    //异常处理逻辑
    void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
    MessageBox.Show(“谁tmd惹祸了:” + e.Exception.Message);
    //处理完后,我们需要将Handler=true表示已此异常已处理过
    e.Handled = true;
    }

9.控件生命周期时间触发顺序。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
  第一步:项目名称上右键->添加->新建项->窗口(WPF),该窗口为打印预览窗口。在其xaml文件中我们加入DocumentViewer容器,将固定文档放入该容器中则可实现打印预览 <DocumentViewer Name="docViewer"></DocumentViewer> <!--文档打印预览容器,当其绑定的文档发生改变时,打印预览的内容也相应发生改变-->     第二步:项目名称上右键->添加->新建项->流文档(WPF),该文档用以写入打印样式和内容,其实它长什么样后面打印出来的大概也就长什么样。     第三步:在主窗体中用按钮或者别的方式打开打印预览窗体(在初始化该窗体时将要打印的内容更改好) PrintPreviewWindow previewWnd = new PrintPreviewWindow("FlowDocument.xaml");//在这里我们将FlowDocument.xaml这个页面传进去,之后通过打印预览窗口的构造函数填充打印内容,如果有数据要插入应该在此传数据结构进去 previewWnd.Owner = this; previewWnd.ShowInTaskbar = false;//设置预览窗体在最小化时不要出现在任务栏中 previewWnd.ShowDialog();//显示打印预览窗体     第四步:从上面第三步我们知道打印内容的更新实际上是打印预览窗口的构造函数中完成的,所以我们在其构造函数中要实现流文档从xaml文件的加载,然后将其转为固定文档,然后将该文档放入到DocumentViewer容器中。这里总的打印预览窗口类代码如下: public partial class PrintPreviewWindow : Window { private delegate void LoadXpsMethod();//委托事件,相当于函数指针 private readonly FlowDocument m_doc;//流文档 public PrintPreviewWindow(string strTmplName)//从上面得到待打印的文档 { InitializeComponent(); m_doc = (FlowDocument)Application.LoadComponent(new Uri(strTmplName, UriKind.RelativeOrAbsolute));//从xaml文件中加载流文档对象 m_doc.PagePadding = new Thickness(50);//设置页面与页面之间的边距宽度 Dispatcher.BeginInvoke(new LoadXpsMethod(LoadXps), DispatcherPriority.ApplicationIdle);//“延后”调用,不然刚刚更改的数据不会马上更新,也就是说打印或者预览不到更新后的数据 } public void LoadXps() { //构造一个基于内存的xps document MemoryStream ms = new MemoryStream(); Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); Uri DocumentUri = new Uri("pack://InMemoryDocument.xps"); PackageStore.RemovePackage(DocumentUri); PackageStore.AddPackage(DocumentUri, package); XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, DocumentUri.AbsoluteUri); //将flow document写入基于内存的xps document中去 XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);//在这里需要添加对.NET 4.0 的一些应用,比较蛋疼 writer.Write(((IDocumentPaginatorSource)m_doc).DocumentPaginator); //获取这个基于内存的xps document的fixed documen docViewer.Document = xpsDocument.GetFixedDocumentSequence(); //关闭基于内存的xps document xpsDocument.Close(); } }     到此,编译完成程序在主窗口触发打开打印预览窗口,则可以看到第二步创建的流文档以预览的方式呈现在窗口上。 ———————————————— 版权声明:本文为CSDN博主「_寒潭雁影」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixinhum/article/details/49800841

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值