我需要使用PowerShell执行验证,并根据结果在WPF应用程序中执行操作。我知道我可以从PowerShell修改TextBlocks,但是当我尝试从PowerShell中修改WPF变量的值时,什么都不会发生。WPF和PowerShell - 将变量从PS传递到C#或修改值
下面是一个例子。
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!";
}
}
}
}
我如何修改C#/ WPF变量在PowerShell中做一些验证后?这甚至有可能吗?
我不想为临时变量创建随机文本块/标签/文本框。
TRUE;在PowerShell中没有任何特殊含义,尝试改变'check'字符串' “$存在= $真”' –
试了一下。不起作用。 也试过使用:[System.Boolean] :: true,它也不起作用。 感谢您的答复。 –
运行脚本以获取值后使用'GetVariable'。 –