网站优化的方式,做网站定金是多少,设计外包网站,延安网站优化1、XAML 主要用于绘制UI界面#xff0c;最大的优点是能使UI与运行逻辑分离开来#xff0c;使得整个程序回到逻辑处理上来。 每一个标签对应.NET Framework类库的一个控件类。通过设置标签的Attribute#xff0c;不仅可以对标签所对应的控件 对象Property进行赋值#xf…1、XAML 主要用于绘制UI界面最大的优点是能使UI与运行逻辑分离开来使得整个程序回到逻辑处理上来。 每一个标签对应.NET Framework类库的一个控件类。通过设置标签的Attribute不仅可以对标签所对应的控件 对象Property进行赋值还可以声明名称空间指定类名等 2、使用attribute给对象的属性赋值 XAML是一种声明性语言XAML编译器会为每个标签创建一个与之对应的对象之后要对他的属性进行初始化 才会有意义。所以每个标签除了声明对象就是初始化对象的属性--即给其属性赋值。赋值方法有两种一种 是字符串简单赋值在XAML中赋值另外一种是使用属性元素进行复杂赋值在.cs里面赋值。下面的 例子用xaml赋值 1前台代码 Window x:ClassfirstApplicaton.Window1 xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml TitleWindow1 Height300 Width300 Grid Grid.ColumnDefinitions ColumnDefinition Width*/ ColumnDefinition Width120/ /Grid.ColumnDefinitions Grid.RowDefinitions RowDefinition Height100/ RowDefinition Height*/ /Grid.RowDefinitions Button x:Namebutton1 Content按钮1 Width80 Height20 Grid.Column0 Grid.Row0 BackgroundBlue / Button x:Namebutton2 Content按钮2 Width80 Height20 Grid.Column1 Grid.Row1 BackgroundFuchsia/ Rectangle x:Name正方形1 Width80 Height 80 Grid.Column0 Grid.Row1 FillRed/ /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进行映射 1TypeConvert: 提供一种将值类型转换为其他类型的统一方式。 TypeConverter 通常支持字符串到对象的 转换目的是供设计环境中的属性编辑器使用或者是为了能够使用 XAML 在xaml 语法中元素英文就attribute value值都是sring类型的 但是 在相印的property却不都是 string,为了能达到attribute与property的一一对应及相互操作 那就需要用上面的这个 typeconvert类进行数据类型的转换。 下面这个例子 自定义一个类 class1 在里面有两个属性一个是name 另一个是 subclass 在一下代码中可以看到subclass是class1类型的属性但是在 xaml中属性却是string 现在问题来了 我们怎么能在。cs文件中把sting转换成 class1类型呢 这个时候就用到了 typeConvert 用他去重写 convertFrom 方法 来实现。 2xaml源码 Window x:ClassfirstApplicaton.Window1 xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:aclr-namespace:firstApplicaton TitleWindow1 Height300 Width300 Window.Resources a:class1 x:Keyclass1 Name1 subclassclass2 /a:class1 /Window.Resources Grid Grid.ColumnDefinitions ColumnDefinition Width*/ ColumnDefinition Width120/ /Grid.ColumnDefinitions Grid.RowDefinitions RowDefinition Height100/ RowDefinition Height*/ /Grid.RowDefinitions Button x:Namebutton1 Content按钮1 Width80 Height20 Grid.Column0 Grid.Row0 BackgroundBlue Clickbutton1_Click / Button x:Namebutton2 Content按钮2 Width80 Height20 Grid.Column1 Grid.Row1 BackgroundFuchsia/ Rectangle x:Namerec Width80 Height 80 Grid.Column0 Grid.Row1 FillRed/ /Grid /Window 3cs源码 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