0

I am new to XAML and Visual Studio and am working on my first project. When try to load my code into Powershell, I get the following error,

"Exception calling "Load" with "1" argument(s): "Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Null'." To show the form, run the following You cannot call a method on a null-valued expression. At C:\FileLocation.ps1:320 char:1

$Form1.ShowDialog() | out-null

  • CategoryInfo : InvalidOperation: (:) [], RuntimeException
  • FullyQualifiedErrorId : InvokeMethodOnNull If I remove "xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" from my code I get the error,

"Exception calling "Load" with "1" argument(s): "Cannot create unknown type 'Window'."

Below is my entire code:

$inputXML = @"
    <Window x:Class="JICXAML.MainWindow"  
            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:JICXAML"
            xmlns:fa="http://schemas.awesome.incremented/wpf/xaml/fontawesome.sharp"        
            mc:Ignorable="d" Height="740" Width="900" WindowStartupLocation="CenterScreen" 
            WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip">
    
            <Window.Resources>
            <Style x:Key="menuButton" TargetType="Button">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="#707db2" />
                <Setter Property="Margin" Value="0 0 0 5"/>
                <Setter Property="Height" Value="45"/>            
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Tag" Value="#6673b7"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderThickness=" 4 0 0 0" BorderBrush="Transparent">
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                        </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
    
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="#bccaf1"/>
                        <Setter Property="Tag" Value="#E9A422"/>
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1,0.5">
                                    <GradientStop Color="#31407b" Offset="0"/>
                                    <GradientStop Color="#495385" Offset="3"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                            
                            
                        </Setter>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="Button">
                                    <Border Background="{TemplateBinding Background}" BorderThickness="4 0 0 0" BorderBrush="#a5a1f5">
                                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>                                   
                                    </Border>                                        
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers> 
    
                
            </Style>
    
            <Style x:Key="menuButtonIcon" TargetType="fa:IconImage">
                <Setter Property="Width" Value="23"/>
                <Setter Property="Height" Value="23"/>
                <Setter Property="Margin" Value="5 0 20 0"/>
                <Setter Property="Foreground" Value="{Binding Path=Tag, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
            </Style>
    
            <Style x:Key="menuButtonText" TargetType="TextBlock">
                <Setter Property="FontSize" Value="17"/>
                <Setter Property="Margin" Value="-25 0 0 0"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Foreground" Value="{Binding Path=Foreground,RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
            </Style>
            
            <Style x:Key="menuButtonActive" TargetType="Button">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1,0.5">
                            <GradientStop Color="#31407b" Offset="0"/>
                            <GradientStop Color="#495385" Offset="3"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Foreground" Value="#707db2"/>
                <Setter Property="Margin" Value="0 0 0 5"/>
                <Setter Property="Height" Value="45"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Tag" Value="#7071f0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderThickness=" 4 0 0 0" BorderBrush="#a5a1f5">
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>           
            </Style>
    
            <Style x:Key="topMenuButton" TargetType="Button">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="#707db2" />
                <Setter Property="Margin" Value="-40 10 10 -60"/>
                <Setter Property="Height" Value="45"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Setter Property="Tag" Value="#6673b7"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderThickness=" 4 0 0 0" BorderBrush="Transparent">
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
    
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Tag" Value="#E9A422"/>
                        <Setter Property="Foreground" Value="{Binding Path=Tag, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect/>
                            </Setter.Value>
                        </Setter>
    
                    </Trigger>
                </Style.Triggers>
    
    
            </Style>
        </Window.Resources>
    
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
    
    
    
            <!--Left Menu-->
            <Border CornerRadius="10 0 0 10" MouseDown="Border_MouseDown">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,10.5">
                        <GradientStop Color="#223266" Offset="0"/>
                        <GradientStop Color="#27396b" Offset=".5"/>
                    </LinearGradientBrush>
                </Border.Background>
    
                <StackPanel>
                    <!--Logo-->
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0 35 0 0">
                        <Border  Width="40" Height="40">
                            <Image Source="C:\Users\Angel\source\repos\JICXAML\jic_logo_final.png" Margin="-75,-34,-57,-141">
    
                            </Image>
                        </Border>
    
                    </StackPanel>
    
                    <Separator Height="1" Margin="10 111" Background="#46558a"/>
    
                    <!--Menu Buttons-->
                    <Button Style="{StaticResource menuButton}">
                        <StackPanel Orientation="Horizontal">
                            <fa:IconImage Icon="Users" Style="{StaticResource menuButtonIcon}" Width="70" Height="30"/>
                            <TextBlock Text="Find User" Style="{StaticResource menuButtonText}"/>
                        </StackPanel>
                    </Button>
    
                    <Button Style="{StaticResource menuButton}">
                        <StackPanel Orientation="Horizontal">
                            <fa:IconImage Icon="Computer" Style="{StaticResource menuButtonIcon}" Width="70" Height="30"/>
                            <TextBlock Text="Find Computer" Style="{StaticResource menuButtonText}"/>
                        </StackPanel>
                    </Button>
    
                    <Button Style="{StaticResource menuButton}">
                        <StackPanel Orientation="Horizontal">
                            <fa:IconImage Icon="Print" Style="{StaticResource menuButtonIcon}" Width="70" Height="30"/>
                            <TextBlock Text="Find Printer" Style="{StaticResource menuButtonText}"/>
                        </StackPanel>
                    </Button>
    
                    <Button Style="{StaticResource menuButton}">
                        <StackPanel Orientation="Horizontal">
                            <fa:IconImage Icon="CompactDisc" Style="{StaticResource menuButtonIcon}" Width="70" Height="30"/>
                            <TextBlock Text="Install Apps" Style="{StaticResource menuButtonText}"/>
                        </StackPanel>
                    </Button>
    
                    <Button Style="{StaticResource menuButton}">
                        <StackPanel Orientation="Horizontal">
                            <fa:IconImage Icon="Columns" Style="{StaticResource menuButtonIcon}" Width="70" Height="30"/>
                            <TextBlock Text="Legacy Panels" Style="{StaticResource menuButtonText}"/>
                        </StackPanel>
                    </Button>
    
                </StackPanel>
            </Border>
            <!--Main Section-->
            <Border Grid.Column="1" CornerRadius="0 10 10 0" MouseDown="Border_MouseDown">
                <Border.Background>
                    <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">
                        <GradientStop Color="#223266" Offset="1"/>
                        <GradientStop Color="#41518f" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
    
                <Grid>
    
    
    
                    <!--Top Menu-->
    
                    <Grid.RowDefinitions>
                        <RowDefinition Height="35" MinHeight="20" />
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
    
                    <Border Grid.Row="1" CornerRadius="0 0 10 0" MouseDown="Border_MouseDown">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">
                                <GradientStop Color="#41518f" Offset="0"/>
                                <GradientStop Color="#2c386c" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
    
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0 0,0,0" Height="60" Width="203">
                        <Button Style="{StaticResource topMenuButton}">
                            <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                                <fa:IconImage Icon="WindowMinimize" Style="{StaticResource menuButtonIcon}" Width="20" Height="30" Margin="40,-50,0,15"/>
    
                            </StackPanel>
                        </Button>
    
                        <Button Style="{StaticResource topMenuButton}">
                            <StackPanel Orientation="Horizontal">
                                <fa:IconImage Icon="WindowRestore" Style="{StaticResource menuButtonIcon}" Width="20" Height="30" Margin="60,-50,5,15"/>
    
                            </StackPanel>
                        </Button>
    
                        <Button Style="{StaticResource topMenuButton}">
                            <StackPanel Orientation="Horizontal">
                                <fa:IconImage Icon="WindowClose" Style="{StaticResource menuButtonIcon}" Width="20" Height="30" Margin="60,-50,20,15"/>
    
                            </StackPanel>
                        </Button>
    
                        <Menu Margin="0,0,0,697">
                            <MenuItem Header="File">
                                <MenuItem Header="Exit"/>
                            </MenuItem>
                        </Menu>
    
                    </StackPanel>
                </Grid>
    
            </Border>
        </Grid>
    </Window>
    
    "@ 
     
    $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    [xml]$XAML = $inputXML
    
    #Read XAML
     
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    try{$Form1=[Windows.Markup.XamlReader]::Load( $reader )}
    catch [System.Management.Automation.MethodInvocationException] {
        Write-Warning "We ran into a problem with the XAML code.  Check the syntax for this control..."
        write-host $error[0].Exception.Message -ForegroundColor Red
        If ($error[0].Exception.Message -like "*button*") {
            write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property.  PS can't handle this`n`n`n`n"
        }
    }
    catch{# If it broke some other way <img draggable="false" role="img" class="emoji" alt="😀" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f600.svg">
        Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
            }
     
    #===========================================================================
    #Store Form Objects In PowerShell
    #===========================================================================
     
    $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
     
    Function Get-FormVariables{
    If ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
    write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
    get-variable WPF*
    }
    #===========================================================================
    # Shows the form
    #===========================================================================
    write-host "To show the form, run the following" -ForegroundColor Cyan
    $Form1.ShowDialog() | out-null

Alternatively, if anyone knows how to run powershell commandlets in XAML, that would be great too. Thanks for any help that anyone can provide.

3
  • 2
    Consider reducing the code in your question to a minimal reproducible example. Commented Jul 24, 2022 at 23:04
  • Ok, I'll try to reduce it tomorrow. Thanks for your reply. Commented Jul 25, 2022 at 1:09
  • Hi everyone. I'm real sorry, but I was unable to reduce the code and get the same error. To produce the error I reported, I have to use the entire code I provided. If I reduce the code, I get a different error messages. I hope that somebody is still able to help me. Commented Jul 25, 2022 at 14:48

1 Answer 1

1

In the line :

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'

You have to replace

-replace "x:N",'N'

by

-replace "x:Name",'Name'

That way it will not convert the {x:Null} in your XAML.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.