日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

wpf学习笔记二 深入学习 xaml

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 wpf学习笔记二 深入学习 xaml 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、XAML 主要用于繪制UI界面,最大的優點是能使UI與運行邏輯分離開來,使得整個程序回到邏輯處理上來。

?? 每一個標簽對應.NET Framework類庫的一個控件類。通過設置標簽的Attribute,不僅可以對標簽所對應的控件 ?? 對象Property進行賦值,還可以聲明名稱空間,指定類名等

2、使用attribute給對象的屬性賦值

?? XAML是一種聲明性語言,XAML編譯器會為每個標簽創建一個與之對應的對象,之后要對他的屬性進行初始化 ?? 才會有意義。所以,每個標簽除了聲明對象就是初始化對象的屬性--即給其屬性賦值。賦值方法有兩種:一種 ?? 是字符串簡單賦值(在XAML中賦值),另外一種是使用屬性元素進行復雜賦值(在.cs里面賦值)。下面的 ? 例子用xaml賦值,

??? 1)前臺代碼:

<Window x:Class="firstApplicaton.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.ColumnDefinitions> ???????????

?? <ColumnDefinition Width="*"/> ???????????

? <ColumnDefinition Width="120"/> ???????

</Grid.ColumnDefinitions> ???????

<Grid.RowDefinitions> ???????????

?? <RowDefinition Height="100"/> ???????????

?? <RowDefinition Height="*"/> ???????

</Grid.RowDefinitions> ??????? ????????

?? <Button x:Name="button1" Content="按鈕1" Width="80"? Height="20" Grid.Column="0"?

???? Grid.Row="0" Background="Blue" /> ????????

?? <Button x:Name="button2" Content="按鈕2" Width="80" Height="20" Grid.Column="1" ??????????

??? ?Grid.Row="1"? Background="Fuchsia"/> ???????

? <Rectangle x:Name="正方形1"? Width="80" Height=" 80" Grid.Column="0" Grid.Row="1" ?????????

???? Fill="Red"/> ???

</Grid> </Window>

??????????????????????????

????? 2)后臺代碼修改button 跟rectangle 的 填充色跟 背景顏色

???????

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 firstApplicaton

{ ??

? /// <summary> ??? /// Window1.xaml 的交互邏輯 ??? /// </summary> ???

public partial class Window1 : Window ??

? { ???????

???? public Window1() ???????

????? { ???????????

???????? InitializeComponent(); ???????????

????????? ? //修改rectangle的填充色 ??????????

?????????? ??? SolidColorBrush scb = new SolidColorBrush(); ??

????? ????????? scb.Color = Colors.Green;

??? ???????????? this.rec.Fill = scb;?

??????????? //修改button的背景色

? ???????????? this.button1.Background = scb;

????????????? this.button2.Background = scb;

??????? } ?

??? }

} ??????????

?????????????????????????????
3、TypeConvert類將XAML標簽的Attribute與對象的Property進行映射

? 1)TypeConvert: 提供一種將值類型轉換為其他類型的統一方式。 TypeConverter 通常支持字符串到對象的

????????????????? 轉換,目的是供設計環境中的屬性編輯器使用或者是為了能夠使用 XAML

?????????????????????????????????? 在xaml 語法中,元素英文就attribute value值都是sring類型的 但是 在相印的property卻不都是

????????????????????????????? ???? string,為了能達到attribute與property的一一對應及相互操作 那就需要用上面的這個

????????????????????????????????? ? typeconvert類進行數據類型的轉換。

????? 下面這個例子 自定義一個類 class1 在里面有兩個屬性,一個是name 另一個是 subclass ,在一下代碼中可以看到subclass是class1類型的屬性,但是在 xaml中屬性卻是string? 現在問題來了? 我們怎么能在。cs文件中把sting轉換成 class1類型呢? 這個時候就用到了 typeConvert? 用他去重寫 convertFrom 方法 來實現。

?? 2)xaml源碼

<Window x:Class="firstApplicaton.Window1"

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

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

??? xmlns:a="clr-namespace:firstApplicaton"

??? Title="Window1" Height="300" Width="300">

?? ??? <Window.Resources>

??????? <a:class1 x:Key="class1" Name="1" subclass="class2">

??????? </a:class1>

??? </Window.Resources>

??? <Grid>

??????? <Grid.ColumnDefinitions>

??????????? <ColumnDefinition Width="*"/>

??????????? <ColumnDefinition Width="120"/>

??????? </Grid.ColumnDefinitions>

??????? <Grid.RowDefinitions>

??????????? <RowDefinition Height="100"/>

??????????? <RowDefinition Height="*"/>

??????? </Grid.RowDefinitions>

??????? ???????? <Button x:Name="button1" Content="按鈕1" Width="80"? Height="20" Grid.Column="0" Grid.Row="0" Background="Blue" Click="button1_Click" />

???????????? ??? <Button x:Name="button2" Content="按鈕2" Width="80" Height="20" Grid.Column="1" Grid.Row="1"? Background="Fuchsia"/>

????????????? ??<Rectangle x:Name="rec"? Width="80" Height=" 80" Grid.Column="0" Grid.Row="1" Fill="Red"/>

??? </Grid> </Window>

3)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;

using System.ComponentModel;

?

namespace firstApplicaton

{ ???

/// <summary> ??? /// Window1.xaml 的交互邏輯 ??? /// </summary>

??? public partial class Window1 : Window

??? {

??????? public Window1()

??????? {

??????????? InitializeComponent();

??????????? //修改rectangle的填充色

??????????? SolidColorBrush scb = new SolidColorBrush();

??????????? scb.Color = Colors.Green;

??????????? this.rec.Fill = scb;

??????????? //修改button的背景色

??????????? this.button1.Background = scb;

??????????? this.button2.Background = scb;

?

??????? }

?

??????? private void button1_Click(object sender, RoutedEventArgs e)

??????? {

???????????? class1? c =( class1) this.FindResource("class1");

?

??????????? MessageBox.Show(c.subclass.Name);

?

?????????? ??????? }

??? }

??? // 先托管 數據轉換的類

??? [TypeConverter(typeof(StringToClass1TypeConverter))]

??? //目標類

??? public class? class1

??? {

??????? public string Name { get;set;}

??????? public class1 subclass{get;set;}

??? } ???

//重寫數據類型轉換 在默認的數據類型轉換中會自動轉換

??? public class StringToCass1TypeConverter : TypeConverter ???

{

?

??????? public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

??????? {

?

??????????? if (value is string)

??????????? {

?

?????????????? class1 c = new class1();

?

??????????????? h.Name = value as string;

?

??????????????? return c;

?

??????????? }

?

??????????? return base.ConvertFrom(context, culture, value);

?

??????? }

?

??? }

}

轉載于:https://www.cnblogs.com/happygod/archive/2013/01/29/wpf.html

總結

以上是生活随笔為你收集整理的wpf学习笔记二 深入学习 xaml的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。