打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF 基础到企业应用系列2——窗体
userphoto

2013.03.05

关注

一、窗体类基本概念

对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类.大家都可能听说过或者看过Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation这本书,它里面就是用XAML和后台代码两种形式来实现同一个功能,那么我们这里定义的窗体也由两部分组成:

1、 XAML文件,在这里面通常全部写UI的东西(希望大家还记得这两幅图)

   

 
2、后台代码文件
namespace WPFApplications
{
/// <summary>
///
Interaction logic for Window5.xaml
/// </summary>
public partial class Window5 : Window
{
public Window5()
{
InitializeComponent();
}

private void btnOK_Click(object sender, RoutedEventArgs e)
{
lblHello.Content = "Hello World Changed";
}
}
}

也可以将后台代码放在XAML文件中,上面的例子可以改写为:

<Window x:Class="WPFApplications.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5" Height="300" Width="300">
<
StackPanel>
<
Label x:Name="lblHello">Hello,World!</Label>
<
Button x:Name="btnOK" Width="88" Height="22" Content="Click"
Click="btnOK_Click"/>
<
x:Code>
<![CDATA[
void btnOK_Click(object sender, System.Windows.RoutedEventArgs e)
{
lblHello.Content = "Hello World Changed";
}
]]>
</x:Code>
</
StackPanel>
</
Window>

二、窗体的生命周期

1、显示窗体

  • 构造函数
  • Show()、ShowDialog()方法:Show()方法显示非模态窗口,ShowDialog()方法显示模态窗口,这个基本和WinForm类似
  • Loaded事件:窗体第一次Show()或ShowDialog()时引发的事件,通常在此事件中加载窗体的初始化数据,但如果用了MVVM模式,基本就不在这里面写。

2、关闭窗体

  • Close()方法:关闭窗体,并释放窗体的资源
  • Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出等信息。

3、窗体的激活

  • Activate()方法:激活窗体
  • Activated、Deactivated事件:当窗体激动、失去焦点时引发的事件

4、窗体的生命周期

为了证实上面的结论,我们用下面的代码进行测试:

public partial class Window3 : Window
{
public Window3()
{
this.Activated += new EventHandler(Window1_Activated);
this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
this.ContentRendered += new EventHandler(Window1_ContentRendered);
this.Deactivated += new EventHandler(Window1_Deactivated);
this.Loaded += new RoutedEventHandler(Window1_Loaded);
this.Closed += new EventHandler(Window1_Closed);
this.Unloaded += new RoutedEventHandler(Window1_Unloaded);
this.SourceInitialized += new EventHandler(Window1_SourceInitialized);

InitializeComponent();
}


void Window1_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Unloaded");
}

void Window1_SourceInitialized(object sender, EventArgs e)
{
Debug.WriteLine("SourceInitialized");
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Loaded");
}

void Window1_Deactivated(object sender, EventArgs e)
{
Debug.WriteLine("Deactivated");
}

void Window1_ContentRendered(object sender, EventArgs e)
{
Debug.WriteLine("ContentRendered");
}

void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Debug.WriteLine("Closing");
MessageBoxResult dr = MessageBox.Show("Cancel the window?",
 "Answer", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (dr == MessageBoxResult.No)
{
e.Cancel = true;
}
}

void Window1_Closed(object sender, EventArgs e)
{
Debug.WriteLine("Closed");
}

void Window1_Activated(object sender, EventArgs e)
{
Debug.WriteLine("Activated");
}

}

执行结果为:
WPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与Windows应用程序中 System.Windows.Forms.Form类颇为相似,其中常用的一些属性、方法、事件有:
  1. 窗体边框模式(WindowStyle属性)和是否允许更改窗体大小(ResizeMode属性) 。
  2. 窗体启动位置(WindowStartupLocation属性)和启动状态(WindowState属性) 等。
  3. 窗体标题(Title属性)及图标 。
  4. 是否显示在任务栏(ShowInTaskbar)
  5. 始终在最前(TopMost属性)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF 如何控制右键菜单ContextMenu的弹出
Wpf 关闭当前窗体 打开新窗体
WPF 模拟UI 键盘录入
线程间操作问题
C#委托(delegate、Action、Func、predicate)和事件
winform获得键盘输入的按键
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服