WPF窗体(5)上

2016-08-20 10:35:32来源:http://studybao.blog.51cto.com/1772164/1135488作者:StudyBao人点击



一、窗体类


在VisualStudio和ExpressionBlend中,自定义的窗体均继承System.Windows.Window类(类型化窗体)。定义的窗体由两部分组成:


1、XAML文件


<Window


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


x:Class="WpfWindow.BasicWindow"x:Name="Window"


Title="BasicWindow"Width="300"Height="200">


<Canvas>


<Buttonx:Name="btnMessage"Width="79"Height="24"Content="OK"


Canvas.Left="172"Canvas.Top="93"Click="btnMessage_Click"/>


<TextBoxx:Name="txtValue"Width="215"Height="25"


Canvas.Left="36"Canvas.Top="48"Text=""TextWrapping="Wrap"/>


</Canvas>


</Window>


2、后台代码文件


namespaceWpfWindow


{publicpartialclassBasicWindow:Window


{publicBasicWindow()


{this.InitializeComponent();}


privatevoidbtnMessage_Click(objectsender,System.Windows.RoutedEventArgse)


{txtValue.Text="HelloWorld";}}}


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


<Window


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


x:Class="WpfWindow.BasicWindow"x:Name="Window"


Title="BasicWindow"Width="300"Height="200">


<Canvas>


<Buttonx:Name="btnMessage"Width="79"Height="24"Content="OK"


Canvas.Left="172"Canvas.Top="93"Click="btnMessage_Click"/>


<x:Code><![CDATA[


voidbtnMessage_Click(objectsender,System.Windows.RoutedEventArgse)


{txtValue.Text="HelloWorld";}]]>


</x:Code>


<TextBoxx:Name="txtValue"Width="215"Height="25"


Canvas.Left="36"Canvas.Top="48"Text=""TextWrapping="Wrap"/>


</Canvas>


</Window>


二、窗体的生存周期


1、显示窗体


·构造方法


·Show()、ShowDialog()方法:Show()方法显示非模态窗口,ShowDialog()方法显示模态窗口


·Loaded事件:窗体第一次Show()或ShowDialog()时引发的事件,通常在此事件中加载窗体的初始化数据


2、关闭窗体


·Close()方法:关闭窗体,并释放窗体的资源


·Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出。


3、窗体的激活


·Activate()方法:激活某窗体


·Activated、Deactivated事件:当窗体激动、失去焦点时引发的事件


4、窗体的生存周期



示例程序:


·在窗体载入时显示当前日期,并开始播放媒体


·当窗体失去焦点时暂停播放媒体


·当窗体重新获得焦点时继承播放窗体


·当点击窗体的关闭按钮时,询问用户是否退出应用程序


XAML文件:


<Windowx:Class="WpfWindow.WindowLifeCycle"


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


Title="WindowLifeCycle"Height="200"Width="300"


Loaded="Window_Loaded"


Activated="Window_Activated"


Deactivated="Window_Deactivated"


Closing="Window_Closing">


<Canvas>


<TextBlockCanvas.Right="15"Canvas.Bottom="15"Height="21"Name="txtDate"/>


<MediaElementCanvas.Left="89"Canvas.Top="12"Height="100"Width="100"


Name="myMedia"Source="numbers.wmv"


Stretch="Fill"LoadedBehavior="Manual"/>


</Canvas>


</Window>


代码文件:


namespaceWpfWindow


{publicpartialclassWindowLifeCycle:Window


{publicWindowLifeCycle()


{InitializeComponent();}


//开关变量,判断是否正在播放媒体


privateboolisPlaying;


privatevoidWindow_Loaded(objectsender,RoutedEventArgse)


{//窗体加载时,显示当前日期及开始播放媒体


txtDate.Text=DateTime.Now.ToString("yyyy-MM-dd");


myMedia.Play();isPlaying=true;


}


privatevoidWindow_Activated(objectsender,EventArgse)


{//如果窗体被激活,则继承播放媒体


if(!isPlaying)


{myMedia.Play();isPlaying=true;}


}


privatevoidWindow_Deactivated(objectsender,EventArgse)


{//如果窗体失去焦点,则暂停播放媒体


if(isPlaying)


{myMedia.Pause();isPlaying=false;}


}


privatevoidWindow_Closing(objectsender,System.ComponentModel.CancelEventArgse)


{//点击窗体的“关闭”按钮,询问用户是否退出程序


stringmessage="Quittheapplication?";


stringtitle="SystemInformation";


MessageBoxButtonbutton=MessageBoxButton.OKCancel;


MessageBoxImageimg=MessageBoxImage.Question;


MessageBoxResultresult=MessageBox.Show(


message,title,button,img);


if(result==MessageBoxResult.Cancel)


{e.Cancel=true;//取消退出}


}}}




最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台