WPF 体验 WPF起始页

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

 

首先配置开发环境:

S2008 SP1  或者  VS2010

 

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

打开App.xaml:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< 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

 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< 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

 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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文件了。

 

 

转载于:https://www.cnblogs.com/jimson/articles/WPF1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值