【问题标题】:WPF application with Powershell code带有 Powershell 代码的 WPF 应用程序
【发布时间】: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 - 成功启动),但没有这些行,无法使用。

如果有人知道如何解决它?

非常感谢!

【问题讨论】:

  • 您是否尝试过设置图片的完整路径:&lt;RibbonButton x:Name="tempbtn" SmallImageSource="C:\Form\Images\help.png" /&gt;
  • 哦,它有帮助:) 但是如果在此操作期间 XML 应该检查 *.png 是否真的存在?我认为它只会创建 xml 标记而不进行检查。我还有另一个问题。现在,错误出现在一行中:使用“1”参数调用“加载”的异常:“无法从文本'UserSearchButton_PreviewMouseLeftButtonDown'创建'PreviewMouseLeftButtonDown'。”
  • 你用PreviewMouseLeftButtonDown做什么?
  • 这是 xaml 中的行:` `
  • 这是此方法的代码:` private void UserSearchButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { frame.Content = new usersSearchPage(); } ` 它用于在应用程序的主要部分加载另一个框架内容

标签: c# wpf powershell


【解决方案1】:

目前您正尝试在 Powershell 中加载您的 xaml 表单。 如果您删除对Preview Mouse 的引用并在$form 上调用ShowDialog 方法,则会显示您的xaml 表单。

$form.ShowDialog() | Out-Null

您实际上想要的是从您的 C# 应用程序中运行一些 Powershell 代码,您需要使用 System.Management.Automation.Runspaces 类。

文档here

示例:https://msdn.microsoft.com/en-us/library/ee706576%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

如果您打算使用 powershell 运行 Active Directory 命令,请记住您需要安装远程服务器管理工​​具RSAT 或远程连接到域控制器。

在 C# 中使用 Active Directory 的“正确”方法是使用 System.Directoryservices NameSpace: https://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory(v=vs.110).aspx

这个例子Site 在 C# 中创建新的 AD 用户

public string CreateUserAccount(string ldapPath, string userName, 
string userPassword)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多