C#课后小试7

WPF(Windows Presentation Foundation)

WPF是用户界面框架,最大的特点是设计与编程分离,使各专业人才更加专注,不用分心。

我们可以通过XML的变形语言--XAML语言来操作。

XAML由一些规则(告诉解析器和编译器如何处理XML)和一些关键字组成,但它自己没有任何有意义的元素。

看下列代码

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

此代码将会输出HelloWPF.Window1

其中    "http://schemas.microsoft.com/winfx/2006/xaml/presentation为命名空间,用于验证自己和子元素。我们可以(在根元素或子元素上)声明额外的XML命名空间,但每一个命名空间下的标识符都必须有一个唯一的前缀。

在C#中 我们可以用 system.console.writeline("HelloWPF.Window1")来实现同样的效果,这时你一定会奇怪,既然C#看起来更简单,我们为什么要用XAML呢?

事实是,使用XAML语句,我们可以很快地在IE浏览器中查看XAML,还会看到一个活生生的按钮放在浏览器窗口中,而C#代码则必须要额外的代码编译方可使用。

生成和事件处理大顺序:在运行时(run-time)模式下,为任何一个XAML声明的对象设置属性之前,总要添加一些事件处理程序,这样就可以让某个事件在属性被设置时被触发,而不用担心XAML使用特性的顺序。

属性元素:

XAML语言:

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Button.Content>
    <Rectangle Height="40" Width="40" Fill="Black"/>
</Button.Content>
</Button>

C#中:

System.Windows.Controls.Button b = new System.Windows.Controls.Button();
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
r.Width = 40;
r.Height = 40;
r.Fill = System.Windows.Media.Brushes.Black;
b.Content = r;

我们可以使用属性元素来替代设置属性。

原文地址:https://www.cnblogs.com/wh-tju/p/4477686.html