WPF到powershell erros
我在PowerShell和GUI上非常新。 我尝试使用PowerShell V2与PowerShell Windows 7一起工作
这是我的xaml代码
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="558.138" Width="474.668">
<Grid Margin="0,0,0,46">
<Grid.RowDefinitions>
<RowDefinition Height="335*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<CheckBox x:Name="checkBox" Content="Adobe READER" HorizontalAlignment="Left" Margin="6,215,0,0" VerticalAlignment="Top" Checked="checkBox_Checked"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="98" Margin="6,10,0,0" VerticalAlignment="Top" Width="454">
<Image x:Name="logo_europe_jpg" Margin="177,0,0,9" Source="logo_europe.jpg" Stretch="Fill"/>
</Border>
<CheckBox x:Name="checkBox_Copy" Content="PDF Creator" HorizontalAlignment="Left" Margin="6,236,0,0" VerticalAlignment="Top" Checked="checkBox_Checked"/>
<CheckBox x:Name="checkBox_Copy1" Content="VPN Client" HorizontalAlignment="Left" Margin="6,306,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,18,0,0" TextWrapping="Wrap" Text="EUROPE TECHNOLOGIES STORE" VerticalAlignment="Top" Width="310"/>
<CheckBox x:Name="checkBox_Copy2" Content="WinSCP" HorizontalAlignment="Left" Margin="6,285,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<CheckBox x:Name="checkBox_Copy3" Content="Google Chrome" HorizontalAlignment="Left" Margin="6,172,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<CheckBox x:Name="checkBox_Copy4" Content="Mozilla Firefox" HorizontalAlignment="Left" Margin="6,151,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<Button x:Name="button" Content="OK" HorizontalAlignment="Left" Margin="115,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="42,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="404"><Run Text="Sélectionn"/><Run Text="ez"/><Run Text=" "/><Run Text="les applications à installer et appuyez sur OK"/></TextBlock>
<Button x:Name="button1" Content="Annuler" HorizontalAlignment="Left" Margin="263,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="22"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" Text="Suggestion de programmes" VerticalAlignment="Top" Width="278" Grid.Row="2"/>
<Button x:Name="button2" Content="Envoyer" HorizontalAlignment="Left" Margin="311,1,0,0" VerticalAlignment="Top" Width="75" Grid.Row="2"/>
</Grid>
</Window>
我改变了x:name by name i deleted x:Class =“WpfApplication1.MainWindow”
这是错误
Exception lors de l'appel de « Load » avec « 1 » argument(s) : « XmlNamespace, Assembly ou ClrNamespace absent de l'ins
truction Mapping. Ligne '0' position '0'. »
Au niveau de STOREEUROPE_ALEX_STORE.ps1 : 43 Caractère : 40
+ $Form=[Windows.Markup.XamlReader]::Load <<<< ($reader)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
Au niveau de STOREEUROPE_ALEX_STORE.ps1 : 46 Caractère : 17
+ $Form.ShowDialog <<<< () | out-null
+ CategoryInfo : InvalidOperation: (ShowDialog:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我删除了四行:xmlns:d =“http://schemas.microsoft.com/expression/blend/2008”
的xmlns:MC = “http://schemas.openxmlformats.org/markup-compatibility/2006”
XMLNS:地方= “CLR的命名空间:WpfApplication1”
MC:可忽略= “d”
另一个错误:
Exception lors de l'appel de « Load » avec « 1 » argument(s) : « Compilation obligatoire du fichier XAML qui spécifie l
es événements. Ligne '0' position '0'. »
Au niveau de STOREEUROPE_ALEX_STORE.ps1 : 39 Caractère : 40
+ $Form=[Windows.Markup.XamlReader]::Load <<<< ($reader)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
Au niveau de STOREEUROPE_ALEX_STORE.ps1 : 42 Caractère : 17
+ $Form.ShowDialog <<<< () | out-null
+ CategoryInfo : InvalidOperation: (ShowDialog:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我的按钮上没有事件,我只想首先显示我的表单。
这里是整个PowerShell脚本
Add-Type -AssemblyName presentationframework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
[xml]$xaml = @’
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EUROPE STORE" Height="560" Width="480">
<Grid Margin="0,0,0,46">
<Grid.RowDefinitions>
<RowDefinition Height="335*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<CheckBox Name="checkBox" Content="Adobe READER" HorizontalAlignment="Left" Margin="6,215,0,0" VerticalAlignment="Top" Checked="checkBox_Checked"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="98" Margin="6,10,0,0" VerticalAlignment="Top" Width="454">
<Image Name="logo_europe_jpg" Margin="177,0,0,9" Source="logo_europe.jpg" Stretch="Fill"/>
</Border>
<CheckBox Name="checkBox_Copy" Content="PDF Creator" HorizontalAlignment="Left" Margin="6,236,0,0" VerticalAlignment="Top" Checked="checkBox_Checked"/>
<CheckBox Name="checkBox_Copy1" Content="VPN Client" HorizontalAlignment="Left" Margin="6,306,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="10,18,0,0" TextWrapping="Wrap" Text="EUROPE TECHNOLOGIES STORE" VerticalAlignment="Top" Width="310"/>
<CheckBox Name="checkBox_Copy2" Content="WinSCP" HorizontalAlignment="Left" Margin="6,285,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<CheckBox Name="checkBox_Copy3" Content="Google Chrome" HorizontalAlignment="Left" Margin="6,172,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<CheckBox Name="checkBox_Copy4" Content="Mozilla Firefox" HorizontalAlignment="Left" Margin="6,151,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" RenderTransformOrigin="0.504,3.382"/>
<Button Name="button" Content="OK" HorizontalAlignment="Left" Margin="115,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<TextBlock Name="textBlock1" HorizontalAlignment="Left" Margin="42,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="404"><Run Text="Sélectionn"/><Run Text="ez"/><Run Text=" "/><Run Text="les applications à installer et appuyez sur OK"/></TextBlock>
<Button Name="button1" Content="Annuler" HorizontalAlignment="Left" Margin="263,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="22"/>
<TextBox Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" Text="Suggestion de programmes" VerticalAlignment="Top" Width="278" Grid.Row="2"/>
<Button Name="button2" Content="Envoyer" HorizontalAlignment="Left" Margin="311,1,0,0" VerticalAlignment="Top" Width="75" Grid.Row="2"/>
</Grid>
</Window>
'@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
#Show Form
$Form.ShowDialog() | out-null
# SIG # Begin signature block
你可以帮我吗? 非常感谢你。
在你的XAML代码中为你做了一些改变。 您无法像在C#中使用PowerShell一样在XAML中指定事件操作。 我看到了XAML代码中的点击(对于按钮)和检查(对于复选框)事件。 这些应该在加载XAML后处理。
请注意我在代码示例中关于加载控件和处理事件的注释
Add-Type -AssemblyName presentationframework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
[xml]$xaml = @’
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="EUROPE STORE" Height="560" Width="480">
<Grid Margin="0,0,0,46">
<Grid.RowDefinitions>
<RowDefinition Height="335*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="12*"/>
</Grid.RowDefinitions>
<CheckBox Name="checkBox" Content="Adobe READER" HorizontalAlignment="Left" Margin="6,215,0,0" VerticalAlignment="Top" />
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="98" Margin="6,10,0,0" VerticalAlignment="Top" Width="454">
<Image Name="logo_europe_jpg" Margin="177,0,0,9" Source="logo_europe.jpg" Stretch="Fill"/>
</Border>
<CheckBox Name="checkBox_Copy" Content="PDF Creator" HorizontalAlignment="Left" Margin="6,236,0,0" VerticalAlignment="Top"/>
<CheckBox Name="checkBox_Copy1" Content="VPN Client" HorizontalAlignment="Left" Margin="6,306,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.504,3.382"/>
<TextBlock Name="textBlock" HorizontalAlignment="Left" Margin="10,18,0,0" TextWrapping="Wrap" Text="EUROPE TECHNOLOGIES STORE" VerticalAlignment="Top" Width="310"/>
<CheckBox Name="checkBox_Copy2" Content="WinSCP" HorizontalAlignment="Left" Margin="6,285,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.504,3.382"/>
<CheckBox Name="checkBox_Copy3" Content="Google Chrome" HorizontalAlignment="Left" Margin="6,172,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.504,3.382"/>
<CheckBox Name="checkBox_Copy4" Content="Mozilla Firefox" HorizontalAlignment="Left" Margin="6,151,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.504,3.382"/>
<Button Name="button" Content="OK" HorizontalAlignment="Left" Margin="115,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" />
<TextBlock Name="textBlock1" HorizontalAlignment="Left" Margin="42,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="404"><Run Text="Sélectionn"/><Run Text="ez"/><Run Text=" "/><Run Text="les applications à installer et appuyez sur OK"/></TextBlock>
<Button Name="button1" Content="Annuler" HorizontalAlignment="Left" Margin="263,12,0,-22" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="22"/>
<TextBox Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,0,0,0" TextWrapping="Wrap" Text="Suggestion de programmes" VerticalAlignment="Top" Width="278" Grid.Row="2"/>
<Button Name="button2" Content="Envoyer" HorizontalAlignment="Left" Margin="311,1,0,0" VerticalAlignment="Top" Width="75" Grid.Row="2"/>
</Grid>
</Window>
'@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
#load the controls here
$button = $Form.FindName('button')
#Handle the event stuff here
$button.Add_Click({
Write-Verbose 'Button was clicked!' -Verbose
})
#Show Form
$Form.ShowDialog() | out-null
链接地址: http://www.djcxy.com/p/63725.html