是否可以在使用PowerShell打开的WPF中包含Gong-WPF-DragDrop ListBox?

时间:2019-06-01 00:40:14

标签: c# wpf visual-studio powershell

我正在尝试创建一个可以使用Powershell脚本控制的WPF。但是,我不知道如何添加Gong-WPF-DragDrop列表框并使用Powershell打开WPF。

我可以通过过滤XAML文件来使用Powershell打开WPF。但是,当我添加Gong-WPF-DragDrop listBox时,powershell将不会显示WPF并显示我帖子底部列出的错误。

我可以使用Gong-WPF-DragDrop列表框在Visual Studio中编译WPF,但不能使用Powershell编译。

要导入Gong-WPF-DragDrop,我使用了NuGet并遵循了此简短教程:https://jm2k69.github.io/2018-11-14-WPF-Solution-Drag-and-Drop-GongSolutions/

我做了一些研究,我想我需要使用DataBinding吗?我已经在互联网上进行了搜索,但是找不到我遇到的任何文档。谁能帮助我或为我指明正确的方向?

这是我用于该项目的代码:

打开WPF的PowerShell脚本:

Add-Type -AssemblyName presentationframework, presentationcore

$wpf = @{}

$inputXML = Get-Content -Path "C:\Users\Admin\source\repos\GUI\GUI\MainWindow.xaml"

$inputXML

$firstItem = $inputXML | select-object -first 1
$firstItem.gettype().Fullname

$inputXMLClean = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"',''

$inputXMLClean

[xml]$xaml = $inputXMLClean

$xaml.GetType().Fullname

$reader = New-Object System.Xml.XmlNodeReader $xaml

$tempform = [Windows.Markup.XamlReader]::Load($reader)
$tempform.GetType().Fullname

$namedNodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")

$namedNodes | ForEach-Object {

    $wpf.Add($_.Name, $tempform.FindName($_.Name))

}

$wpf.TestWindow.ShowDialog() | Out-Null

MainWindow.xaml

<Window x:Name="TestWindow" x:Class="WPF.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:WPF"
        xmlns:dd="urn:gong-wpf-dragdrop"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox 
    Height="Auto"
    x:Name="DragDrop"
    HorizontalAlignment="Left"
    dd:DragDrop.IsDragSource="True"
    dd:DragDrop.IsDropTarget="True"
    dd:DragDrop.UseDefaultDragAdorner="False"
    dd:DragDrop.UseDefaultEffectDataTemplate="False"
    ItemContainerStyle="{x:Null}"
    SelectionMode="Extended" Margin="315,32,0,66" Width="130">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Margin="5" Orientation="vertical" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem>ListBox Item #1</ListBoxItem>
            <ListBoxItem>ListBox Item #2</ListBoxItem>
            <ListBoxItem>ListBox Item #3</ListBoxItem>
        </ListBox>

    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

我希望Powershell像没有Gong-WPF-DragDrop listBox一样打开WPF,但是却给了我这个错误:

Exception calling "ShowDialog" with "0" argument(s): "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle 
after a Window has closed."
At C:\Users\Admin\Desktop\PowerShell WPF.ps1:96 char:1
+ $wpf.TestWindow.ShowDialog() | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

@ punker76您是否会知道解决方案?

0 个答案:

没有答案