简体   繁体   中英

Feed XML into XAML/PowerShell WPF Form

I had a question along a similar topic last week which ultimatly is the same issue but in that scenario I managed to get round this with a PowerShell array on static data thanks to someone on the forum suggesting.

This time round I cant use static so I am somewhat back to my root problem.

I have created a WPF XAML form in Visual Studio and am taking this back to Powershell as my remit with the customer is 'low-code'. The item in question is loading an xml file into the form to populate a list box. Reason for a list box is cleanliness of changing the colour of the background.

Now in VS this works find with a Data Provider but for reasons I cannot find an answer to, this just simply will not work when taken back to PowerShell so I have looked for alternate way.

So I have a simple XML as below:

<?xml version="1.0"?>
<Configuration>
  <AllowedAutoStart>Application 1</AllowedAutoStart>
  <DenyRemoveAutoStart>Application 2</DenyRemoveAutoStart>
</Configuration>

I want to feed this into my PowerShell/XAML Hybrid Script and simply bind the contents to the appropriate list box (below code is just starting with allowed apps)

I have tried a few different ideas from across the forum before posting but none quite get there. Below is the code I have at present, it doesnt work but doesnt produce any errors either :-)

Appreciate any guidance.

# Load Assembly
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase



#Declare XAML Code
[xml]$AppGeneratorWindow = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="App Config Generator" Height="350" Width="600" WindowStartupLocation="CenterScreen" Top="5" ResizeMode="NoResize">
    <Window.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="#FFAA3D3D" Offset="1"/>
        </LinearGradientBrush>
    </Window.Background>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="115*"/>
            <ColumnDefinition Width="132*"/>
            <ColumnDefinition Width="543*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="381*"/>
            <RowDefinition Height="241*"/>
        </Grid.RowDefinitions>
        <Label Name="AllProgramsLabel" Content="All Programs" HorizontalAlignment="Left" Margin="28.333,20,0,0" VerticalAlignment="Top" Foreground="#FFFEFBFB" FontSize="14" FontWeight="Bold" RenderTransformOrigin="2.696,-3.142" Grid.Column="1" Grid.ColumnSpan="2"/>
        <Button Name="AddButton" Content="Add &gt;&gt;" HorizontalAlignment="Left" Margin="34,83,0,0" VerticalAlignment="Top" Width="166" FontWeight="Bold" FontSize="14" Height="28" BorderBrush="#FF070606" Background="#FF933838" Foreground="#FFFCFAFA" Grid.Column="3" RenderTransformOrigin="0.416,-0.906"/>
        <Label Name="WindowsStartupLabel" Content="Windows Startup" HorizontalAlignment="Left" Margin="236,20,0,0" VerticalAlignment="Top" Foreground="#FFFEFBFB" FontSize="14" FontWeight="Bold" RenderTransformOrigin="2.696,-3.142" Grid.Column="3"/>
        <Button Name="RemoveButton" Content="&lt;&lt; Remove" HorizontalAlignment="Left" Margin="34,169,0,0" VerticalAlignment="Top" Width="166" FontWeight="Bold" FontSize="14" Height="28" BorderBrush="#FF070606" Background="#FF933838" Foreground="#FFFCFAFA" Grid.Column="3" Grid.RowSpan="2" RenderTransformOrigin="0.51,-0.503"/>
        <ListBox Name="AllProgramsListBox" Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Left" Height="132" Margin="28,65,0,0" Background="#FFAA3D3D" VerticalAlignment="Top" Width="123" Grid.RowSpan="2"/>
        <ListBox Name="StartupListBox" Grid.Column="3" HorizontalAlignment="Left" Height="132" Margin="237,65,0,0" Background="#FFAA3D3D" VerticalAlignment="Top" Width="123" Grid.RowSpan="2"/>


    </Grid>
</Window>
"@




#List Boxes
$AllProgramsListBox = $window.FindName("AllProgramsListBox")

$ConfigurationFile = "$env:ProgramData\WindowsStartupTool\AutoStartConfig.XML"
[xml]$ConfigFile = Get-Content $ConfigurationFile

foreach ($entry in $ConfigFile.Configuration.AllowedAutoStart.add)

    {
        write-host $entry
     
      $AllProgramsListBox.Items.Add($($entry))
    
    }



#Declare & Create the form
$reader=(New-Object System.Xml.XmlNodeReader $AppGeneratorWindow)
$window = [Windows.Markup.XamlReader]::Load($reader)

##########################################
#Launch the User Interface               #
       [void]$window.ShowDialog()        # 
##########################################

This is launched from PowerShell which is where I have to get it to work from in some capacity.

Thanks in advance

You're very close. All you really have to do is move the part where you read the config file and do the foreach ($entry.. to below the creation of the window.

As the code is now, you're trying to use variable $window where it is not yet defined.

Directly below the XAML code put:

#Declare & Create the form
$reader = New-Object System.Xml.XmlNodeReader $AppGeneratorWindow
$window = [Windows.Markup.XamlReader]::Load($reader)

#List Boxes
$AllProgramsListBox = $window.FindName("AllProgramsListBox")

$ConfigurationFile = "$env:ProgramData\WindowsStartupTool\AutoStartConfig.XML"
[xml]$ConfigFile = Get-Content $ConfigurationFile -Raw

foreach ($entry in $ConfigFile.Configuration.AllowedAutoStart) {
      Write-Host $entry
      $AllProgramsListBox.Items.Add($entry)
}

##########################################
#Launch the User Interface               #
       [void]$window.ShowDialog()        # 
##########################################

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM