Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XAML parsing help requested
#1
I've been experimenting with the XAML parser a bit, trying to add XAML controls to a wpfBuilder window, with some success (code below).
 
Code:
Copy      Help
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:
 
Code:
Copy      Help
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
#2
The grid XAML is invalid.

------------------

Copy XAML from VS and remove the x:Class attribute.

If loaded XAML of entire Window or UserControl, you will not have control variables. And cannot set events in XAML.

Examples.

Code:
Copy      Help
// script ""
using System.Windows;
using System.Windows.Controls;

var xaml = """
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUserControlLibrary1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Height="24" Margin="23,24,0,0" VerticalAlignment="Top" Width="64"/>
        <TextBox HorizontalAlignment="Left" Height="25" Margin="23,67,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="87"/>

    </Grid>
</Window>
"""
;
var w = System.Windows.Markup.XamlReader.Parse(xaml) as Window;
w.ShowDialog();

Code:
Copy      Help
// script ""
using System.Windows;
using System.Windows.Controls;

var xaml = """
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfUserControlLibrary1"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Height="24" Margin="28,33,0,0" VerticalAlignment="Top" Width="57"/>
        <TextBox HorizontalAlignment="Left" Height="21" Margin="28,75,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="102"/>

    </Grid>
</UserControl>
"""
;
var uc = System.Windows.Markup.XamlReader.Parse(xaml) as UserControl;

var b = new wpfBuilder("Window");
b.R.Add(uc);
b.R.AddOkCancel();
b.End();
b.ShowDialog();
#3
Thanks very much, Gintaras, that's a huge help.
Regards,
burque505


Forum Jump:


Users browsing this thread: 1 Guest(s)