Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Load XAML file, which contains the x:Code element

Tags:

c#

.net

wpf

xaml

I need load, and use XAML file.

I have wrote simple XAML file:

<Window      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" 
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="300" Title="My Window">
    <x:Code>
        <![CDATA[
            void btnMyButton_Click_1(object sender, RoutedEventArgs e) {
                MessageBox.Show("Hello world", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        ]]>
    </x:Code>
    <Grid>
        <Button x:Name="btnMyButton" x:FieldModifier="public" Margin="10" Padding="10" 
                FontSize="20" Click="btnMyButton_Click_1">My button</Button>
    </Grid>
</Window>

Then I try to load this XAML in my code:

String curDir = io.Path.GetDirectoryName(typeof(Lisp_Wpf.Commands).Assembly.Location);
String xamlFileName = "MyWindow.xaml";
String fileFullName = io.Path.Combine(curDir, xamlFileName);
DependencyObject depObj = default(DependencyObject);
using (io.FileStream fs = new io.FileStream(fileFullName, io.FileMode.Open)) {
    depObj = XamlReader.Load(fs) as DependencyObject;
    fs.Close();
}
if (depObj != null && depObj is Window) {
    Window win = (Window)depObj;
    acad.ShowModalWindow(win);
}

But I get exception:

System.Windows.Markup.XamlParseException occurred: 'Failed to create a 'Failed to create a 'Click' from the text 'btnMyButton_Click_1'.' Line number '16' and line position '31'.

I found such info here:

Code declared within x:Code for WPF has several notable limitations: ... x:Class Directive must be provided on the parent root element. ...

But if I add in my Window element the x:Class attribute:

x:Class="Lisp_Wpf.MainWindow"

then I get exception:

'Specified class name 'Lisp_Wpf.MainWindow' doesn't match actual root instance type 'System.Windows.Window'. Remove the Class directive or provide an instance via XamlObjectWriterSettings.RootObjectInstance.' Line number '1' and line position '14'.

In my MS Visual Studio project the XML file has properties: Build Action = None; Copy To Output Directory = copy always; Custom Tools = (empty string);

The partial CS file for this XAML I deleted.

like image 454
Andrey Bushman Avatar asked Oct 24 '25 16:10

Andrey Bushman


2 Answers

if you want to load xaml at runtime you cannot give any code behind your XAML file.Probably you can just attach general event for the Button click and identity the controls at runtime - Just have look at the below link and try it out.

Loading XAML XML through runtime?

like image 161
Kishore Kumar Avatar answered Oct 26 '25 06:10

Kishore Kumar


General hint (I did not reproduce your specific problem):

In WPF this error comes also if the Build Action in Visual Studio to was set to "Resource" (you find the setting under properties of the xaml file). Set the Build Action to "Page" and it will compile!

like image 38
Knasterbax Avatar answered Oct 26 '25 06:10

Knasterbax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!