簡體   English   中英

WPF和PowerShell-將變量從PS傳遞到C#或修改值

[英]WPF & PowerShell - Passing Variables from PS to C# or modifying value

我需要使用PowerShell執行驗證,並根據結果在WPF應用程序中執行操作。 我知道我可以從PowerShell修改TextBlocks,但是當我嘗試從PowerShell修改WPF變量的值時,沒有任何反應。

這是一個例子。

MainWindow.xaml:

    <Window x:Class="Test.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:Test"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="74,76,0,0" TextWrapping="Wrap" Text="false" VerticalAlignment="Top" Height="177" Width="371"/>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text.RegularExpressions;
using System.IO;

namespace Test
{
    public partial class MainWindow : Window
    {
        private bool exists = false;

        public MainWindow()
        {
            InitializeComponent();

            // Setup PowerShell Environment
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.ThreadOptions = PSThreadOptions.UseCurrentThread;
            runSpace.Open();
            runSpace.SessionStateProxy.SetVariable("exists", exists);
            PowerShell ps = PowerShell.Create();
            ps.Runspace = runSpace;
            string check = "$exists = true";
            ps.AddScript(check);
            // Execute
            ps.Invoke();
            runSpace.Close();

            if (exists == true)
            {
                textBlock.Text = "It is true!";
            }
        }
    }
}

經過一些驗證后,如何在PowerShell中修改C#/ WPF變量? 那有可能嗎?

我不想為臨時變量創建隨機文本塊/標簽/文本框。

首先, exists一個值類型。 要從PowerShell更新exists ,您必須通過引用PowerShell腳本來傳遞它。 我可能是錯的,但似乎不太可能。 即使有可能,我仍然不建議這樣做。 這樣的狀態看起來簡直是毛骨悚然。 我認為更好的方法是將要驗證的數據傳遞到PowerShell腳本,然后將驗證結果返回給C#。 您可以根據該結果在C#中修改變量狀態。

這是我在LinqPad中使用輸出運行的代碼的略微修改版本:

var exists = true;
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runSpace.Open();

PowerShell ps = PowerShell.Create();
ps.Runspace = runSpace;
string validation = @"
    # Perform some validation.

    # For the purposes of this demonstration I am going to pretend
    # that validation failed and set 'result' to false.
    $result = $false

    # Return 'result'
    $result
";
ps.AddScript(validation);

// Write the state of `exists` to the console before invoking PowerShell.
Console.WriteLine("exists: " + exists.ToString());

// Execute
var result = ps.Invoke();

// Update exists based on the result of the validation.
exists = bool.Parse(result[0].ToString());

// Or you can use 'GetVariable' to get the result back from PowerShell:
// exists = bool.Parse(runSpace.SessionStateProxy.GetVariable("result").ToString());

// Write the state to the console after invoking PowerShell.
Console.WriteLine("exists: " + exists.ToString());

這是寫入控制台的結果:

存在:真實
存在:錯誤

根據邁克的建議,我什至不必映射就可以從PS中獲取變量。 像這樣:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text.RegularExpressions;
using System.IO;

namespace Test
{
    public partial class MainWindow : Window
    {
        public bool itExists = false;

        public MainWindow()
        {
            InitializeComponent();

            // Setup PowerShell Environment
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.ThreadOptions = PSThreadOptions.UseCurrentThread;
            runSpace.Open();
            PowerShell ps = PowerShell.Create();
            ps.Runspace = runSpace;
            string check = "$itExists = $TRUE";
            ps.AddScript(check);
            // Execute
            ps.Invoke();

            var result = runSpace.SessionStateProxy.PSVariable.GetValue("itExists").ToString();

            runSpace.Close();

            itExists = result.Equals("True");

            if (itExists)
            {
                textBlock.Text = "It is true!";
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM