A group of friends in a certain WPF exchange group asked about the tools for converting XAML to C #. At present, there seems to be no such tool.Program. I suddenly realized that C # is required to dynamically generate the XAML in some cases. For example, do I need to write a large number of elements one by one? C #CodeWriting is more efficient. However, in general, C # code is not required for writing, and the efficiency of using XAML is higher.
For example,
Xmal:
<WindowX: 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>
As you can see, window1 does not have any containers and controls.
Now we add a Grid container with three rows and one column:
In the constructor of window1:
Initializecomponent (); grid Grid = New Grid (); // instantiate a grid rowdefinition row1 = New Rowdefinition (); // instantiate a grid row rowdefinition row2 = New Rowdefinition (); rowdefinition row3 = New Rowdefinition (); columndefinition col = New Columndefinition (); // instantiate a grid column row1.height = New Gridlength ( 3 , Gridunittype. Star); // sets the Row Height (in proportion) row2.height = New Gridlength ( 4 , Gridunittype. Star); row3.height = New Gridlength ( 3 , Gridunittype. Star );
Col. width =NewGridlength (10, gridunittype. Star); // set the column width (fully filled) // row3.height =NewGridlength (51, gridunittype. pixel); // set the Row Height (by pixel)// Row3.height = gridlength. Auto; // you can specify the row height based on the element height)
Grid. rowdefinitions. add (row1); // Add to grid in the row set. rowdefinitions. add (row2); grid. rowdefinitions. add (row3); grid. columndefinitions. add (COL); // Add to the column set of the gridThis. Content = grid; // Set grid as the form's main container
Now, the Grid container is constructed and added to window1.
Add the following controls:
Textblock text = New Textblock (); text. Text = " This is an example !! (I make up 3 copies) " ; Button button1 = New Button (); button1.width = Button1.height = 50 ; Button button2 = New Button (); button1.content = " Button 1 (4 copies) " ; Button2.content = " Button 2 (3 copies) " ; Grid. Children. Add (button1); grid. Children. Add (button2); grid. Children. Add (text );
Now, if you run the command, you will find that all the controls are in the first line, because we just added the control in the previous step and haven't set it yet. Now let's set it:
Grid. setrow (text,0);//Set text to the first lineGrid. setrow (button1,1); Grid. setrow (button2,2);//Grid. setcolumn (text, 0 );//Set text to the first column
So far, I have already written an interface. Is it very tedious?
Well, I think so too, but if you want to write like this:
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 = New Color (); color. = 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;
Set the border background to 256*255 colors. Do you need to manually write 256*255 border, 255 columns, and 255 rows? Naturally, code efficiency is high.
BenArticleIt is just a simple introduction of basic things, so it is not very detailed.
As for the Program for converting Shenma Xmal to C #, we are expected to develop it.