今天天气不错,园子大丰收~~:

WPF 体验 WPF起始页

最近研究WPF,以此记录下自己的研究历程:

 

首先配置开发环境:

S2008 SP1  或者  VS2010

 

新建一个WPF Application Project,看到App.xaml,和Window1.xaml两个文件。顾名思义App.xaml主要包括Application级的设置,而Window1则就是一个体现UI 的Window实体。

打开App.xaml:

 

代码
<Application x:Class="WpfProject.App"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri
="Window1.xaml">

<Application.Resources>

</Application.Resources>
</Application>

 StartupUri="Window1.xaml" 则表明程序的起始页面为Window1, 当然也可以通过代码控制。打开App.xaml.cs

 

 

代码
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

Window1 view
= new Window1();
view.Show();
}
}

这点和Winform 差不多。但是在代码设置的时候不要忘了删除xaml里面的StartupUri啊,不然会多出现界面的哦。

 

再看看Window1.xaml:

 

代码
<Window x:Class="WpfProject.Window1"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="Window1" Height="300" Width="300">
<Grid>

</Grid>
</Window>

解析Window 的属性:Title,Width,height就不用说了。xmlns定义一个或多个可供选择的命名空间,其实是xml的namespace,Namespaces翻译为名字空间,这个大家知道。xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 说明当前默认的NameSpace就是http://schemas.microsoft.com/winfx/2006/xaml/presentation,位于此名字控件下的类就可以在页面类直接使用了。那么不再此名字空间下的类呢,也就是怎么定义其他名字空间呢? 看xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",就是定义了http://schemas.microsoft.com/winfx/2006/xaml名字空间,但是不能使默认的了,不然就重复了,那么就再给他一个名字x,就组成了xmlns:x,也就是说x就是http://schemas.microsoft.com/winfx/2006/xaml。那么应用它下面的类就用x:ClassName。因此,x:Class="WpfProject.Window1"就是Class是x下面的类了,并指定此xaml的Class为WpfProject.Window1。那么继续引用其他的命名空间:E.g:

 

 

xmlns:cs="clr-namespace:System.Windows.Controls;assembly=PresentationFramework" 

在看看Window1.xaml.cs

 

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfProject
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}

 

 public partial class Window1 : Window 这个表明此CS是Windows1的一部分,所以在编辑的时候编译器才会把它们整合在一起编译成Windows1这个类,不让它就成一个独立的XML文件了。

 

 

posted @ 2010-05-22 23:43  Jimson.Ma  阅读(1019)  评论(0编辑  收藏  举报

Email: 20045912@163.com MSN: 20045912@163.com