【发布时间】:2018-08-31 03:25:52
【问题描述】:
我想在此应用程序中使用 powershell 代码创建 WPF 应用程序(带有功能区菜单)。 我已经创建了 XAML,并且在 VS 中它已正确启动。这是原始代码:
<RibbonWindow x:Class="ADUserMgmtTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
Title="MainWindow" Height="632.5" Width="931">
<Grid Margin="0,0,2,24">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Ribbon x:Name="RibbonWin" SelectedIndex="0" >
<Ribbon.Background>
<ImageBrush/>
</Ribbon.Background>
<!-- Help Pane, located at the right-hand side -->
<Ribbon.HelpPaneContent>
<RibbonButton x:Name="tempbtn" SmallImageSource="Images\help.png" />
</Ribbon.HelpPaneContent>
<!-- Quick Access Toolbar - located at the upper-left corner -->
<Ribbon.QuickAccessToolBar>
<RibbonQuickAccessToolBar>
<RibbonButton x:Name ="Save" SmallImageSource="Images\save.png" />
<RibbonSplitButton x:Name ="Undo" SmallImageSource="Images\undo.png">
<RibbonSplitMenuItem Header="Undo 1" />
<RibbonSplitMenuItem Header="Undo 2" />
<RibbonSplitMenuItem Header="Undo 3" />
</RibbonSplitButton>
<RibbonSplitButton x:Name="Redo" SmallImageSource="Images\redo.png" >
<RibbonSplitMenuItem Header="Redo 1" />
<RibbonSplitMenuItem Header="Redo 2" />
</RibbonSplitButton>
</RibbonQuickAccessToolBar>
</Ribbon.QuickAccessToolBar>
<!-- Application Menu, located at the left-hand side (down arrow) -->
<Ribbon.ApplicationMenu>
<RibbonApplicationMenu KeyTip="F">
<RibbonApplicationMenuItem Header="Options" ImageSource="Images\options.png" />
<RibbonApplicationMenuItem Header="Exit" ImageSource="Images\quit.png" />
</RibbonApplicationMenu>
</Ribbon.ApplicationMenu>
<!-- Ribbon Tab #1: Home -->
<RibbonTab Header="Użytkownicy" KeyTip="H" >
<!-- Home group-->
<RibbonGroup x:Name="UsersSearchGroup" Header="Szukaj">
<RibbonMenuButton x:Name="UserSearchButton" LargeImageSource="Images\paste.png" Label="Szukaj" KeyTip="V" Width="111" PreviewMouseLeftButtonDown="UserSearchButton_PreviewMouseLeftButtonDown" />
</RibbonGroup>
<!-- Employee And Payroll group-->
<RibbonGroup x:Name="UsersOperationsGroup" Header="Operacje">
<RibbonMenuButton LargeImageSource="Images\personal.png" Label="Użytkownicy" KeyTip="V" Height="66" Width="108" />
<RibbonButton x:Name="usersOperationMofifyButton" SmallImageSource="Images\save.png" Label="Modyfikuj" KeyTip="X" />
<RibbonButton x:Name="usersOperationAddButton" SmallImageSource="Images\add.png" Label="Dodaj" KeyTip="C" PreviewMouseLeftButtonDown="usersOperationAddButton_PreviewMouseLeftButtonDown" />
<RibbonButton x:Name="usersOperationDeleteButton" SmallImageSource="Images\add.png" Label="Usuń" />
<RibbonButton x:Name="usersOperationResetButton" SmallImageSource="Images\add.png" Label="Reset hasła" />
<RibbonButton x:Name="usersOperationBlockButton" SmallImageSource="Images\add.png" Label="Zablokuj" />
</RibbonGroup>
<!-- Employee And Payroll group-->
<RibbonGroup x:Name="UsersReportsGroup" Header="Raporty">
<RibbonMenuButton LargeImageSource="Images\personal.png" Label="Raporty" Height="66" Width="108" />
<RibbonButton x:Name="usersReportsLastLoon" SmallImageSource="Images\add.png" Label="Ostatnie logowanie" />
</RibbonGroup>
</RibbonTab>
<!-- Ribbon Tab #2: -->
<RibbonTab Header="Komputery" KeyTip="I">
</RibbonTab>
<!-- Ribbon Tab #3: -->
<RibbonTab Header="PageLayout" KeyTip="L" Margin="2,-1,-2,1">
<ContentControl Content="ContentControl" Height="100" Width="100"/>
</RibbonTab>
</Ribbon>
<Frame x:Name="frame" Content="Frame" HorizontalAlignment="Left" Height="418" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="892" RenderTransformOrigin="0.5,0.5" NavigationUIVisibility="Hidden"/>
<Label x:Name="lbltest" Content="Label" HorizontalAlignment="Left" Margin="100,5,0,0" Grid.Row="1" VerticalAlignment="Top"/>
</Grid> </RibbonWindow>
当我想将它解析为 XML 时,我阅读了很多教程,我应该删除 XAML 中的所有“x:”并从第一行删除这段代码:“x:Class="ADUserMgmtTool.MainWindow"",所以我删除了提到的片段。
接下来,我运行:
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
[xml]$XAML = $inputXML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
这里我有错误:
Exception calling "Load" with "1" argument(s): "Failed to create a ' 'SmallImageSource' from the text 'Images\help.png'."
At line:1 char:1
+ $Form=[Windows.Markup.XamlReader]::Load( $reader )
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : XamlParseException
如果我删除所有带有图像的参数和带有方法名称的操作参数(例如 PreviewMouseLeftButtonDown="usersOperationAddButton_PreviewMouseLeftButtonDown" ),脚本将成功启动(如果我创建没有功能区的 WPF - 成功启动),但没有这些行,无法使用。
如果有人知道如何解决它?
非常感谢!
【问题讨论】:
-
您是否尝试过设置图片的完整路径:
<RibbonButton x:Name="tempbtn" SmallImageSource="C:\Form\Images\help.png" /> -
哦,它有帮助:) 但是如果在此操作期间 XML 应该检查 *.png 是否真的存在?我认为它只会创建 xml 标记而不进行检查。我还有另一个问题。现在,错误出现在一行中:使用“1”参数调用“加载”的异常:“无法从文本'UserSearchButton_PreviewMouseLeftButtonDown'创建'PreviewMouseLeftButtonDown'。”
-
你用
PreviewMouseLeftButtonDown做什么? -
这是 xaml 中的行:`
` -
这是此方法的代码:` private void UserSearchButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { frame.Content = new usersSearchPage(); } ` 它用于在应用程序的主要部分加载另一个框架内容
标签: c# wpf powershell