Andrey Bushman
Andrey Bushman

Reputation: 12476

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

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.

Upvotes: 4

Views: 5064

Answers (2)

Knasterbax
Knasterbax

Reputation: 8207

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!

Upvotes: 2

Kishore Kumar
Kishore Kumar

Reputation: 21863

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?

Upvotes: 3

Related Questions