【发布时间】:2020-01-08 19:38:13
【问题描述】:
我在使用 WPF 时遇到了问题。当我打开窗口时,我希望它在所有其他程序之上打开,但我不希望它始终保持在最前面,只是最初的打开。我知道将 topmost 设置为 true 会在顶部打开(我在 Xaml 中有),但我似乎找不到在打开后将其更改为 false 的方法。
这是一个带有 WPF 窗口的简单测试函数。
function foo{
#Load Assembly and Library
Add-Type -AssemblyName PresentationFramework
$inputXaml = @"
<Window x:Class="SharepointCreateOpportunity.completeWindow"
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:SharepointCreateOpportunity"
mc:Ignorable="d"
Title="Window Title" Height="250" MinHeight="250" MaxHeight="250" Width="500" MinWidth="500" MaxWidth="500" Topmost="True" WindowStartupLocation="CenterScreen" Background="Black" >
<Grid>
<Button Name="oKBtn" Content="OK" Margin="0,0,20,20" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="72" Height="23"/>
</Grid>
</Window>
"@
#Gets rid of elements from the Xaml so it can be converted
$inputXamlClean = $inputXaml -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"',''
[xml]$xaml = $inputXamlClean
#Creates the Window
$XMLReader = (New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load($XMLReader)
#Creates variables for all the elements on the window
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)}
#OK Button Action
$okBtn.Add_Click({
$Window.Close()
})
#Show window
$Window.ShowDialog()
}
foo
【问题讨论】:
标签: wpf powershell window topmost