I've been experimenting with the XAML parser a bit, trying to add XAML controls to a wpfBuilder window, with some success (code below).
What I would like to do, though - I realize that a) this is lazy and b) I'm asking a lot - is to be able to use VS 2022 to design a form and then use LA's XAML reading and writing capabilities to read the XAML Window into a script. So far I can't read in a whole XAML Window; and XamlReader.Parse doesn't recognize (for example) a Grid by itself. Code I've tried to at least created a Grid, which throws errors:
Regards,
burque505
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
var xaml = """
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Content="Click me!" Foreground='Coral' FontSize='20pt'></Button>
""";
Button myButton = XamlReader.Parse(xaml) as Button;
var xaml2 = """
<TextBox xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Margin="68,92,0,0" TextWrapping="Wrap" Background="Red" Foreground="Yellow"
FontFamily="Verdana" FontSize="48" FontStyle="Italic" FontWeight="ExtraBold"
VerticalAlignment="Top" Width="402" Height="158"/>
""";
TextBox myText = XamlReader.Parse(xaml2) as TextBox;
var b = new wpfBuilder("XAML Control Test");
b.StartGrid();
b.R.Add(myButton);
myButton.Click += (o, e) => {
print.it("Works");
if (myText.Text != "You clicked?") {
myText.Text = "You clicked?";
}
else {
myText.Text = "I'm toggling, I think ...";
}
};
b.R.Add(myText);
b.End();
b.ShowDialog();
What I would like to do, though - I realize that a) this is lazy and b) I'm asking a lot - is to be able to use VS 2022 to design a form and then use LA's XAML reading and writing capabilities to read the XAML Window into a script. So far I can't read in a whole XAML Window; and XamlReader.Parse doesn't recognize (for example) a Grid by itself. Code I've tried to at least created a Grid, which throws errors:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xaml;
using System.Xaml.Schema;
var xaml = """
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Content="Click me!" Foreground='Coral' FontSize='20pt'></Button>
""";
Button myButton = System.Windows.Markup.XamlReader.Parse(xaml) as Button;
var xaml3 = """
<TextBox xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
Margin="68,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="402" Height="238"/>
""";
TextBox myText = System.Windows.Markup.XamlReader.Parse(xaml3) as TextBox;
var xaml2 = """
<Grid>
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
</Grid>
""";
Grid myGrid = System.Windows.Markup.XamlReader.Parse(xaml2) as Grid;
// Error:
/*
System.Windows.Markup.XamlParseException: 'Cannot create unknown type 'Grid'.' Line number '1' and line position '6'.
---> System.Xaml.XamlObjectWriterException: 'Cannot create unknown type 'Grid'.' Line number '1' and line position '6'.
at line 29 in xamlr2.cs
*/
var b = new wpfBuilder("Window");
//b.Add<Grid>(myGrid); // Never reaches this
b.R.Add(myButton); // Works if grid is omitted
b.R.Add(myText); // Works if grid is omitted
b.End();
b.ShowDialog();
Regards,
burque505