在powershell中使用wpf usercontrol [英] Wpf usercontrol in powershell

查看:129
本文介绍了在powershell中使用wpf usercontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



要求是在标签项中显示用户控件。以下是我正在尝试的代码。




The requirement is to display a user control in a tab item. The below is the code I am trying.

Add-Type -AssemblyName PresentationFramework
[xml]$MainXAML = @"
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="42*" />
            <RowDefinition Height="269*" />
        </Grid.RowDefinitions>
            <Button Name="Button1" Content="ClickMe" Width="61" Height="20" Margin="12,12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"></Button>
        <TabControl Name="tabChildContainer" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1"></TabControl>

    </Grid>
</Window>
"@
[xml]$UserControlXAML = @"
<UserControl 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             Height="292" Width="489">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="150,86,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

"@

$MainReader = New-Object System.Xml.XmlNodeReader $MainXAML
$Window_Main= [Windows.Markup.XamlReader]::Load($MainReader)

$UC_Reader = New-Object System.Xml.XmlNodeReader $UserControlXAML
$Window_UC= [Windows.Markup.XamlReader]::Load($UC_Reader)


$MainXAML.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ("WPF_Main_"+$_.Name) -Value $Window_Main.FindName($_.Name) -Scope "Script" }
$UserControlXAML.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ("WPF_UC_"+$_.Name) -Value $Window_UC.FindName($_.Name) -Scope "Script" }
$script:WPF_Main_button1.Add_Click(
{
    Write-Host -ForegroundColor Green "I am in the click event."
    $TabItem1 = New-Object System.Windows.Controls.TabItem
    $TabItem1.Header = "Eureka"
    $TabItem1.AddChild($WPF_UC_button1)
    $WPF_Main_tabChildContainer.Items.Add($TabItem1)
})
$Window_Main.ShowDialog() 



以下是错误。有人可以帮我解决一下吗?提前谢谢。


The below is the error. Could someone pls help me to fix it? thank you in advance.

Exception calling "ShowDialog" with "0" argument(s): "Must disconnect specified child from current parent Visual before attaching to new parent Visual."
At line:50 char:1
+ $Window_Main.ShowDialog()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException





我尝试过:



hi,

我试图在PowerShell中使用WPF用户控件。一切正常,但是当我选择标签项然后得到焦点错误。



What I have tried:


I am trying to use a WPF User control in PowerShell. Everything is working fine but when I select the tab item then getting the focus error.

推荐答案

MainXAML = @
< Window
xmlns =
http://schemas.microsoft.com/winfx/2006/xaml/ presentation
xmlns:x =
http://schemas.microsoft.com/winfx / 2006 / xaml
Title =
MainWindow 高度= 350 Width = 525 >
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =
42 * />
< RowDefinition Height =
269 * />
< /Grid.RowDefinitions>
< Button Name =
Button1 Content = ClickMe Width = 61 Height = 20 Margin = 12 12 0 0 Horizo​​ntalAlignment = Left VerticalAlignment = Top >< / Button>
< TabControl Name =
tabChildContainer Horizo​​ntalContentAlignment = Stretch VerticalContentAlignment =拉伸 Grid.Row = 1 >< / TabControl的>

< / Grid>
< / Window>
@
[ xml ]
MainXAML = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="42*" /> <RowDefinition Height="269*" /> </Grid.RowDefinitions> <Button Name="Button1" Content="ClickMe" Width="61" Height="20" Margin="12,12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"></Button> <TabControl Name="tabChildContainer" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1"></TabControl> </Grid> </Window> "@ [xml]


UserControlXAML = @
< UserControl
xmlns =
http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =
http://schemas.microsoft.com/winfx/2006/xaml
xmlns:mc =
http://schemas.openxmlformats.org/markup- compatibility / 2006
xmlns:d =
http://schemas.microsoft.com / expression / blend / 2008

Height =
292 Width = 489 >
< Grid>
< Button Content =
按钮 高度= 23 Horizo​​ntalAlignment = Left Margin = 150 86 0 0 Name = button1 VerticalAlignment = Top Width = 75 />
< / Grid>
< / UserControl>

@

UserControlXAML = @" <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Height="292" Width="489"> <Grid> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="150,86,0,0" Name="button1" VerticalAlignment="Top" Width="75" /> </Grid> </UserControl> "@


MainReader =新-Object System.Xml.XmlNodeReader
MainReader = New-Object System.Xml.XmlNodeReader


这篇关于在powershell中使用wpf usercontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆