欢迎光临散文网 会员登陆 & 注册

WPF笔记

2023-08-30 22:38 作者:鹿共禾Gonghe  | 我要投稿

XML笔记

XML可扩展的标记语言

XML 存储数据

注意:XML是严格区分大小写的

XML标签成对存在的

XML文档有且只能有一个根节点

XML代码

 

                                             

通多代码来创建XML文档

//通多代码来创建XML文档

//引用命名空间

//创建xml文档对象

XmlDocument doc = new XmlDocument();

//创建一行描述信息,并添加到doc文档中

XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);

//创建根节点

XmlElement books = doc.CreateElement("Books");

//将更节电添加到文档中

doc.AppendChild(books);

 

//给根节点Books创建子节点

XmlElement book1= doc.CreateElement("Books");

//将book添加到根节点

books.AppendChild(book1);

//给Book1添加子节点

XmlElement name1= doc.CreateElement("name");

name1.InnerText = "西游记";

book1.AppendChild(name1);

XmlElement price1 = doc.CreateElement("price");

price1.InnerText = "10";

price1.AppendChild(price1);

XmlElement des1 = doc.CreateElement("Des");

des1.InnerText = "好看";

book1.AppendChild(des1);

 

doc.AppendChild(dec);

doc.Save("Books.xml");

Console.WriteLine("保存成功");

Console.ReadKey();

WPF

XAML中为对象赋值

Attribute=Value

<Rectangle Width="100"Height="80" Stroke="Black" Fill="Blue" RadiusX="10" RadiusY="100"/>

Stroke线框颜色

Fill填充颜色

RadiusX RadiusY圆角

这样可以画一个矩形

<Path Data="M 0,01L 200 ,100L100,200 Z" Stroke="Black" Fill="Red"/>

这样可以画一个线段

M移动将笔

L延伸到坐标

Z命令闭合图形

得到一个三角形

 

Windows资源标签

引用命名空间

xmlns:local="clr-namespace:WpfApp6"

作为一个Windows资源声明出来

<Window.Resources>

        <local:Human x:Key="human" Name="Gonghe"/>检索key命名空间

</Window.Resources>

一个类

public class Human

{

      public string Name { get; set; }

      public Human Child { get; set; }

}

XAML

<Window x:Class="WpfApp6.MainWindow"

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

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

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp6"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

<Window.Resources>

    赋值 准备一个检索存在在一个教key的命名空间中 Name赋值

        <local:Human x:Key="human" Name="Gonghe"/>

    </Window.Resources>

    <Grid>

        <Button Content="Show!" Width="120" Height="30" Click="Button_Click"/>

    </Grid>

</Window>

代码

namespace WpfApp6

{

    /// <summary>

    /// MainWindow.xaml 的交互逻辑

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            //检索windows资源

            Human h =this.FindResource("human") as Human;

            //as可能会返回一个空值会抛异常

            if(h != null)

            {

                MessageBox.Show(h.Name);

            }

        }

    }

    public class Human

    {

        public string Name { get; set; }

        public Human Child { get; set; }

    }

}

写一个自己的类

public class Human

{

public string Name { get; set; }

public Human Child { get; set; }

}

引用命名空间

当前程序集映射为一个lcoal的命名空间

xmlns:local="clr-namespace:WpfApp6"

作为一个Windows资源(Windows.Resources)声明出来(资源在WPF中:每个WPF程序以一个资源字典的形式维护这一系列的资源)通过索引检索

<Window.Resources>

    先准备一个检索x:key,检索在一个的命名空间中key的一个对象命名human,Name属性赋值

    <local:Human x:Key="human" Name="Gonghe"/>

</Window.Resources>

Button

<Button Content="Show!" Width="120" Height="30" Click="Button_Click"/>

事件处理器

private void Button_Click(object sender, RoutedEventArgs e)

{

//检索windows资源 把key的名字改成human返回一个object类型转换为一个Human类型

Human h =this.FindResource("human") as Human;

//as可能会返回一个空值会抛异常

if(h != null)

{

MessageBox.Show(h.Name);

}  

}

属性标签

<Button Width="120" Height="30" Content="停止"/>

<Rectangle Width="20" Height="20" Stroke="Black" Fill="Yellow"/>

我们做了一个按钮我们要将矩形放到按钮里

<Button Width="120" Height="30" />这个叫空标签,这个标签不具有内容

<Button Width="120" Height="30" >开始部分

    Button标签的内容

</Button>结尾部分

XAML

<Grid>

        <Button Width="120" Height="30" >

            <Button.Content>

                <Rectangle Width="20" Height="20" Stroke="Black" Fill="Yellow"/>

            </Button.Content>

        </Button>

    </Grid>

要用Button.Content

去掉Button XAML编译器会认为你要声明一个叫Content的类的对象(没有Content类 所以报错‘如果有那我们无法发达到我们的目的’)

属性标签是由类名.属性名可以书写属性的值,复杂的值

<Rectangle Width="200" Height="160" Stroke="Blue" Fill="LightBlue">我们画了一个矩形

我们想要一个渐变色来填充这个矩形

XAML

    <Grid>

        <Rectangle Width="200" Height="160" Stroke="Blue">

            <Rectangle.Fill>

                <LinearGradientBrush>

                    <LinearGradientBrush.StartPoint>

                        <Point X="0" Y="0"/>

                    </LinearGradientBrush.StartPoint>

                    <LinearGradientBrush.EndPoint>

                        <Point X="1" Y="1"/>

                    </LinearGradientBrush.EndPoint>

                    <LinearGradientBrush.GradientStops>

                        <GradientStopCollection>

                            <GradientStop Offset="0.2" Color="LightBlue"/>

                            <GradientStop Offset="0.7" Color="Blue"/>

                            <GradientStop Offset="0.9" Color="Red"/>

                        </GradientStopCollection>

                    </LinearGradientBrush.GradientStops>

                </LinearGradientBrush>

            </Rectangle.Fill>

        </Rectangle>

</Grid>

先画一个矩形

准备一个属性标签<Rectangle.Fill></Rectangle.Fill>

<LinearGradientBrush>渐变笔刷

设定一个点StartPoint从这个点开始绘画这是一个相对值(0,0)指的是图形的左上角(1,1)指的是图形的右下角

<LinearGradientBrush.StartPoint>

<Point X="0" Y="0"/>

</LinearGradientBrush.StartPoint>

这是结束点

<LinearGradientBrush.EndPoint>

<Point X="1" Y="1"/>

</LinearGradientBrush.EndPoint>

GradientStopCollection是一个集合可以装下很多GradientStops的对象

<LinearGradientBrush.GradientStops>

<GradientStopCollection>

    设置一个渐变点

<GradientStop Offset="0.2" Color="LightBlue"/>

<GradientStop Offset="0.7" Color="Blue"/>

       <GradientStop Offset="0.9" Color="Red"/>

</GradientStopCollection>

</LinearGradientBrush.GradientStops>

或者优化一下

<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">

<LinearGradientBrush.GradientStops>

<GradientStopCollection>

<GradientStop Offset="0.2" Color="LightBlue"/>

<GradientStop Offset="0.7" Color="Blue"/>

<GradientStop Offset="0.8" Color="Red"/>

</GradientStopCollection>

</LinearGradientBrush.GradientStops>

</LinearGradientBrush>

标签扩展

<TextBlock Height="24" Width="120" Background="LightBlue" Text="Hello"/>

我们用Attribute=Value这种方法为TEXT赋值Hello

事件处理器

事件就是.NET平台对象之间通讯的机制

<Grid>

    <Button x:Name="button1" Content="点击" Width="200" Height="60" ></Button>

</Grid>

我们写了一个按钮button,点击之后没有反应

没有事件响应者响应click事件

添加一个事件处理器

Click="button1_Click"

在后台代码事件处理器处

private void button1_Click(object sender, RoutedEventArgs e)

{

MessageBox.Show("Hello World");

}

点击之后就可以出现hello world

事件拥有者button拥有click事件

事件响应者是窗体

事件处理器是Button1_click

·C#代码来写

public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            this.button1.Click += new RoutedEventHandler(button1_Click);

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("Hello World");

        }

}


WPF笔记的评论 (共 条)

分享到微博请遵守国家法律