Powershell WPF和拉动变量

时间:2014-10-23 01:45:56

标签: c# wpf powershell

我将此作为我的主要脚本:

.\loadDialog.ps1 -XamlPath 'form.xaml'

#EVENT Handler 
    $CREATE.add_Click({  
        $NAME = $NAME.Content
        $PATH = $PATH.Content
        $STARTMEM = $STARTMEM.Content
        New-VM -Name $NAME -path $PATH --MemoryStartupBytes $STARTMEM
    })

#Launch the window
$xamGUI.ShowDialog() | out-null

LOADDIALOG

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$XamlPath
)

[xml]$Global:xmlWPF = Get-Content -Path $XamlPath

#Add WPF and Windows Forms assemblies
try{
    Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
    Throw "Failed to load Windows Presentation Framework assemblies."
}

#Create the XAML reader using a new XML node reader
$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))

#Create hooks to each named object in the XAML
$xmlWPF.SelectNodes("//*[@Name]") | %{
    Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
    }

form.xaml

<Window x:Class="CreateHyperVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="CREATE" Content="CREATE" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75"/>
        <ComboBox HorizontalAlignment="Left" Margin="114,14,0,0" VerticalAlignment="Top" Width="377"/>
        <Label Content="OS TYPE:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.695,0.288"/>
        <TextBox Name="NAME" HorizontalAlignment="Left" Height="23" Margin="114,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="377"/>
        <Label Content="NAME" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" Width="57"/>
        <TextBox Name="STARTMEM" HorizontalAlignment="Left" Height="23" Margin="114,72,0,0" TextWrapping="Wrap" Text="1024" VerticalAlignment="Top" Width="377"/>
        <Label Content="MEMORY (MB)" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Width="99"/>
        <TextBox Name="PATH" HorizontalAlignment="Left" Height="23" Margin="114,103,0,0" TextWrapping="Wrap" Text="C:\ProgramData\Microsoft\Windows\Hyper-V\" VerticalAlignment="Top" Width="377"/>
        <Label Content="PATH" HorizontalAlignment="Left" Margin="10,103,0,0" VerticalAlignment="Top" Width="57"/>

    </Grid>
</Window>

错误:

Exception calling "Load" with "1" argument(s): "Specified class name 'CreateHyperVVM.MainWindow' doesn't match actual
root instance type 'System.Windows.Window'. Remove the Class directive or provide an instance via
XamlObjectWriterSettings.RootObjectInstance."
At C:\Users\frank\Documents\POWERSHELLWORK\HyperV\loadDialog.ps1:18 char:1
+ $Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNod ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : XamlParseException

You cannot call a method on a null-valued expression.
At C:\Users\frank\Documents\POWERSHELLWORK\HyperV\loadDialog.ps1:22 char:5
+     Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\frank\Documents\POWERSHELLWORK\HyperV\CreateVM.ps1:4 char:2
+     $CREATE.add_Click({
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\frank\Documents\POWERSHELLWORK\HyperV\CreateVM.ps1:12 char:1
+ $xamGUI.ShowDialog() | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

是从WPF项目中提取变量的正确方法吗?我不断收到错误,并想知道我做错了。

查看类错误我认为它可能与我的VS.NET项目有关吗?

0 个答案:

没有答案