WPF筆記
XML筆記
XML可擴(kuò)展的標(biāo)記語(yǔ)言
XML 存儲(chǔ)數(shù)據(jù)
注意:XML是嚴(yán)格區(qū)分大小寫的
XML標(biāo)簽成對(duì)存在的
XML文檔有且只能有一個(gè)根節(jié)點(diǎn)
XML代碼
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
通多代碼來創(chuàng)建XML文檔
//通多代碼來創(chuàng)建XML文檔
//引用命名空間
//創(chuàng)建xml文檔對(duì)象
XmlDocument doc = new XmlDocument();
//創(chuàng)建一行描述信息,并添加到doc文檔中
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//創(chuàng)建根節(jié)點(diǎn)
XmlElement books = doc.CreateElement("Books");
//將更節(jié)電添加到文檔中
doc.AppendChild(books);
?
//給根節(jié)點(diǎn)Books創(chuàng)建子節(jié)點(diǎn)
XmlElement book1= doc.CreateElement("Books");
//將book添加到根節(jié)點(diǎn)
books.AppendChild(book1);
//給Book1添加子節(jié)點(diǎn)
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中為對(duì)象賦值
Attribute=Value
<Rectangle Width="100"Height="80" Stroke="Black" Fill="Blue" RadiusX="10" RadiusY="100"/>
Stroke線框顏色
Fill填充顏色
RadiusX RadiusY圓角
這樣可以畫一個(gè)矩形
<Path Data="M 0,01L 200 ,100L100,200 Z" Stroke="Black" Fill="Red"/>
這樣可以畫一個(gè)線段
M移動(dòng)將筆
L延伸到坐標(biāo)
Z命令閉合圖形
得到一個(gè)三角形
?
Windows資源標(biāo)簽
引用命名空間
xmlns:local="clr-namespace:WpfApp6"
作為一個(gè)Windows資源聲明出來
<Window.Resources>
??????? <local:Human x:Key="human" Name="Gonghe"/>檢索key命名空間
</Window.Resources>
一個(gè)類
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>
??? 賦值 準(zhǔn)備一個(gè)檢索存在在一個(gè)教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可能會(huì)返回一個(gè)空值會(huì)拋異常
??????????? if(h != null)
??????????? {
??????????????? MessageBox.Show(h.Name);
??????????? }
??????? }
??? }
??? public class Human
??? {
??????? public string Name { get; set; }
??????? public Human Child { get; set; }
??? }
}
寫一個(gè)自己的類
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
}
引用命名空間
當(dāng)前程序集映射為一個(gè)lcoal的命名空間
xmlns:local="clr-namespace:WpfApp6"
作為一個(gè)Windows資源(Windows.Resources)聲明出來(資源在WPF中:每個(gè)WPF程序以一個(gè)資源字典的形式維護(hù)這一系列的資源)通過索引檢索
<Window.Resources>
??? 先準(zhǔn)備一個(gè)檢索x:key,檢索在一個(gè)的命名空間中key的一個(gè)對(duì)象命名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返回一個(gè)object類型轉(zhuǎn)換為一個(gè)Human類型
Human h =this.FindResource("human") as Human;
//as可能會(huì)返回一個(gè)空值會(huì)拋異常
if(h != null)
{
MessageBox.Show(h.Name);
}??
}
屬性標(biāo)簽
<Button Width="120" Height="30" Content="停止"/>
<Rectangle Width="20" Height="20" Stroke="Black" Fill="Yellow"/>
我們做了一個(gè)按鈕我們要將矩形放到按鈕里
<Button Width="120" Height="30" />這個(gè)叫空標(biāo)簽,這個(gè)標(biāo)簽不具有內(nèi)容
<Button Width="120" Height="30" >開始部分
??? Button標(biāo)簽的內(nèi)容
</Button>結(jié)尾部分
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編譯器會(huì)認(rèn)為你要聲明一個(gè)叫Content的類的對(duì)象(沒有Content類 所以報(bào)錯(cuò)‘如果有那我們無法發(fā)達(dá)到我們的目的’)
屬性標(biāo)簽是由類名.屬性名可以書寫屬性的值,復(fù)雜的值
<Rectangle Width="200" Height="160" Stroke="Blue" Fill="LightBlue">我們畫了一個(gè)矩形
我們想要一個(gè)漸變色來填充這個(gè)矩形
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>
先畫一個(gè)矩形
準(zhǔn)備一個(gè)屬性標(biāo)簽<Rectangle.Fill></Rectangle.Fill>
<LinearGradientBrush>漸變筆刷
設(shè)定一個(gè)點(diǎn)StartPoint從這個(gè)點(diǎn)開始繪畫這是一個(gè)相對(duì)值(0,0)指的是圖形的左上角(1,1)指的是圖形的右下角
<LinearGradientBrush.StartPoint>
<Point X="0" Y="0"/>
</LinearGradientBrush.StartPoint>
這是結(jié)束點(diǎn)
<LinearGradientBrush.EndPoint>
<Point X="1" Y="1"/>
</LinearGradientBrush.EndPoint>
GradientStopCollection是一個(gè)集合可以裝下很多GradientStops的對(duì)象
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
??? 設(shè)置一個(gè)漸變點(diǎn)
<GradientStop Offset="0.2" Color="LightBlue"/>
<GradientStop Offset="0.7" Color="Blue"/>
?????? <GradientStop Offset="0.9" Color="Red"/>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
或者優(yōu)化一下
<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>
標(biāo)簽擴(kuò)展
<TextBlock Height="24" Width="120" Background="LightBlue" Text="Hello"/>
我們用Attribute=Value這種方法為TEXT賦值Hello
事件處理器
事件就是.NET平臺(tái)對(duì)象之間通訊的機(jī)制
<Grid>
??? <Button x:Name="button1" Content="點(diǎn)擊" Width="200" Height="60" ></Button>
</Grid>
我們寫了一個(gè)按鈕button,點(diǎn)擊之后沒有反應(yīng)
沒有事件響應(yīng)者響應(yīng)click事件
添加一個(gè)事件處理器
Click="button1_Click"
在后臺(tái)代碼事件處理器處
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello World");
}
點(diǎn)擊之后就可以出現(xiàn)hello world
事件擁有者button擁有click事件
事件響應(yīng)者是窗體
事件處理器是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");
??????? }
}