简体   繁体   English

将 XML 输入 XAML/PowerShell WPF 表单

[英]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.上周我有一个关于类似主题的问题,这最终是同一个问题,但在那种情况下,由于论坛上有人建议,我设法通过静态数据上的 PowerShell 数组解决了这个问题。

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'.我在 Visual Studio 中创建了一个 WPF XAML 表单,并将其带回 Powershell,因为我与客户的职责是“低代码”。 The item in question is loading an xml file into the form to populate a list box.有问题的项目是将 xml 文件加载到表单中以填充列表框。 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.现在在 VS 中,这可以通过数据提供程序找到,但由于我找不到答案的原因,这在带回 PowerShell 时根本不起作用,因此我寻找了替代方法。

So I have a simple XML as below:所以我有一个简单的 XML,如下所示:

<?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)我想将它输入到我的 PowerShell/XAML 混合脚本中,然后简单地将内容绑定到适当的列表框(下面的代码只是从允许的应用程序开始)

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.这是从 PowerShell 启动的,这是我必须以某种方式让它工作的地方。

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.您真正需要做的就是移动读取配置文件的部分并执行foreach ($entry..到创建窗口的下方

As the code is now, you're trying to use variable $window where it is not yet defined.就像现在的代码一样,您正在尝试使用尚未定义的变量$window

Directly below the XAML code put:在 XAML 代码的正下方放置:

#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()        # 
##########################################

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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