title: WPF: Load XAML file, which contains the x:Code element
body: <p>I need load, and use XAML file.</p>
  
  <p>I have wrote simple XAML file:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;x:Code&gt;
          &lt;![CDATA[
              void btnMyButton_Click_1(object sender, RoutedEventArgs e) {
                  MessageBox.Show("Hello world", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
              }
          ]]&gt;
      &lt;/x:Code&gt;
      &lt;Grid&gt;
          &lt;Button x:Name="btnMyButton" x:FieldModifier="public" Margin="10" Padding="10" 
                  FontSize="20" Click="btnMyButton_Click_1"&gt;My button&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Then I try to load this XAML in my code:</p>
  
  <pre><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 &amp;&amp; depObj is Window) {
      Window win = (Window)depObj;
      acad.ShowModalWindow(win);
  }
  </code></pre>
  
  <p>But I get exception:</p>
  
  <blockquote>
    <p>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'.</p>
  </blockquote>
  
  <p>I found such info <a href="http://msdn.microsoft.com/en-us/library/ms750494.aspx" rel="nofollow">here</a>:</p>
  
  <blockquote>
    <p>Code declared within x:Code for WPF has several notable limitations:
      ...
      x:Class Directive must be provided on the parent root element.
      ...</p>
  </blockquote>
  
  <p>But if I add in my <code>Window</code> element the <code>x:Class</code> attribute:</p>
  
  <pre><code>x:Class="Lisp_Wpf.MainWindow"
  </code></pre>
  
  <p>then I get exception:</p>
  
  <blockquote>
    <p>'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'.</p>
  </blockquote>
  
  <p>In my MS Visual Studio project the XML file has properties:
  Build Action = None;
  Copy To Output Directory = copy always;
  Custom Tools = (empty string);</p>
  
  <p>The partial CS file for this XAML I deleted.</p>