title: XAML WPF form wont load when runing my script
body: <p>Hey guys I am getting an error when I try to run my script with my XAML file. Error below. I have also attached the code from my WPF form that I made in visual studio that is causing the error. When I remove this code from my form the script loads the XAML file just fine. What am I doing wrong?</p>
  <p>Error:
  Exception calling &quot;Load&quot; with &quot;1&quot; argument(s): &quot;Failed to create a 'Click' from the text 'CheckBox1_Click'.&quot;</p>
  <p>At PATH OF XAML\Script.ps1:30 char:5</p>
  <p>$window = [Windows.Markup.XamlReader]::Load($reader)</p>
  <p>CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException</p>
  <p>FullyQualifiedErrorId : XamlParseException</p>
  <pre><code>#Assembly and Sharepoint Connection Code
  
  Add-Type -AssemblyName PresentationFramework
  
  #Gets user name of the user running the script (Only first and last initial)
  
  $UserName = $env:username.ToCharArray() | Select -First 2
  
  $UserInitials = $UserName -Join &quot;&quot;
  
  #XAML GUI Code
  
  $xamlFile = &quot;Path of XAML file&quot;
  
  #GUI Creation Code
  
  $inputXML = Get-Content $xamlFile -Raw
  
  $inputXML = $inputXML -replace 'mc:Ignorable=&quot;d&quot;', '' -replace &quot;x:N&quot;, 'N' -replace '^&lt;Win.*', '&lt;Window'
  
  [XML]$XAML = $inputXML
  
  #Read XAML
  
  $reader = (New-Object System.Xml.XmlNodeReader $xaml)
  
  
  try 
  {
      $window = [Windows.Markup.XamlReader]::Load($reader)
  } 
  catch 
  {
      Write-Warning $_.Exception
      throw
  }
  
  #Create variables based on form control names
  
  $xaml.SelectNodes(&quot;//*[@Name]&quot;) | ForEach-Object {
  
      #&quot;trying item $($_.Name)&quot;;
  
      try 
      {
          Set-Variable -Name &quot;var_$($_.Name)&quot; -Value $window.FindName($_.Name) -ErrorAction Stop
      } 
      catch 
      {
          throw
      }
  
  }
  
  Get-Variable var_*
  
  #Code from the click event in my WPF form
  
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void CheckBox1_Click(object sender, RoutedEventArgs e)
      {
          if (CheckBox1.IsChecked == true)
          {
              CheckBox2.IsEnabled = false;
              CheckBox2.IsChecked = false;
              CheckBox3.IsEnabled = false;
              CheckBox3.IsChecked = false;
              CheckBox4.IsEnabled = false;
              CheckBox4.IsChecked = false;
              CheckBox5.IsEnabled = false;
              CheckBox5.IsChecked = false;
          }
          else
          {
              CheckBox2.IsEnabled = true;
              CheckBox3.IsEnabled = true;
              CheckBox4.IsEnabled = true;
              CheckBox5.IsEnabled = true;
          }
      }
  }
  </code></pre>