利用C#动态生成Xaml
之前在某一WPF交流群中一群友提问关于xaml转c#的工具,目前好像没有这方面的程序。忽而发觉在某些时候需要用C#动态生成xaml。例如在需要生成大量元素的时候,难道需要一个个的去写吗?这时用C#代码去写便比较高效率了。但是在一般情况下,是不需要用C#代码去写,用xaml效率反而更高。
举一个代码例子吧,
xmal:
<Window x:Class="WpfApplication9.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"> </Window>
可以看到,Window1中没有任何容器和控件。
我们现在往里面加上一个三行一列的Grid容器:
Window1的构造函数中:
InitializeComponent(); Grid grid = new Grid(); //实例化一个Grid RowDefinition row1= new RowDefinition(); //实例化一个Grid行 RowDefinition row2 = new RowDefinition(); RowDefinition row3 = new RowDefinition(); ColumnDefinition col = new ColumnDefinition(); //实例化一个Grid列 row1.Height = new GridLength(3, GridUnitType.Star); //设置行的高度(按比例) row2.Height = new GridLength(4, GridUnitType.Star); row3.Height = new GridLength(3, GridUnitType.Star);
col.Width = new GridLength(10,GridUnitType.Star); //设置列的宽度(完全填充) //row3.Height = new GridLength(51, GridUnitType.Pixel); //设置行的高度(按像素) //row3.Height = GridLength.Auto; //设置行的高度(根据元素的高度来确定)
grid.RowDefinitions.Add(row1); //添加到grid的行集合中 grid.RowDefinitions.Add(row2); grid.RowDefinitions.Add(row3); grid.ColumnDefinitions.Add(col); //添加到grid 的列集合中 this.Content = grid; //设置grid为窗体的主容器
现在就将Grid容器构造完毕,并添加到了Window1中。
接下来添加几个控件进去:
TextBlock text = new TextBlock(); text.Text = "这是一个示例!!(我占3份)"; Button button1 = new Button(); button1.Width = button1.Height = 50; Button button2 = new Button(); button1.Content = "按钮1(我占4份)"; button2.Content = "按钮2(我占3份)"; grid.Children.Add(button1); grid.Children.Add(button2); grid.Children.Add(text);
现在若运行下,会发现所有控件都挤在了第一行中,因为我们上一步只是添加,还没有设置,现在我们设置一下:
Grid.SetRow(text, 0); //设置text为第一行 Grid.SetRow(button1, 1); Grid.SetRow(button2, 2); //Grid.SetColumn(text,0); //设置text为第一列
到目前为止就已经写好一个界面了,是不是很繁琐?
好吧,我也是这么认为的,但是如果你要这样写:
Grid grid = new Grid(); for (int g = 0; g <= 255; g ) { for (int b = 0; b <= 255; b ) { if (grid.RowDefinitions.Count <= 255) { RowDefinition row = new RowDefinition(); row.Height = GridLength.Auto; grid.RowDefinitions.Add(row); } if(grid.RowDefinitions.Count <= 255) { ColumnDefinition column = new ColumnDefinition(); column.Width = GridLength.Auto; grid.ColumnDefinitions.Add(column); } Color color = new Color(); color.A = Convert.ToByte(g); color.R = 255; color.G =0; color.B = Convert.ToByte(b); // Debug.WriteLine(color.ToString()); Border border = new Border(); border.BorderBrush = new SolidColorBrush(Colors.Black); border.Background = new SolidColorBrush(color); border.Margin = new Thickness(2); TextBlock texttool = new TextBlock(); texttool.Text = color.ToString(); border.ToolTip = texttool; border.Width = border.Height = 10; Grid.SetRow(border, b); Grid.SetColumn(border, g); grid.Children.Add(border); } } this.Content = grid;
设置Border的背景分别为256*255种颜色,难道256*255个Border、255个列和255个行要手动写吗?自然用代码效率是高的。
本文章只是简单的提一下基础的东西,因此不是很详细。
至于那个神马Xmal转C#的程序,就有望大神们开发了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库