[英]PowerShell Form with Web Browser that Automatically Refreshes
I'm trying to create a simple form in PowerShell in a small window that always stays on top other windows and just contains a web browser object that automatically refreshes every 10 seconds. 我试图在PowerShell中的一个小窗口中创建一个简单窗体,该窗口始终位于其他窗口的顶部,并且仅包含一个Web浏览器对象,该对象每10秒自动刷新一次。 I've been searching online and trying various options but nothing seems to work.
我一直在网上搜索并尝试各种选择,但似乎没有任何效果。 Almost every example I come across has the form or web browser refresh happen on a button press, but I need the web browser to automatically refresh on its own without any user interaction.
我遇到的每个示例几乎都在按下按钮时刷新表单或Web浏览器,但是我需要Web浏览器自行自动刷新,而无需任何用户交互。 From what I've been reading it sounds like WPF (Windows Presentation Foundation) with XAML (Extensible Application Markup Language) is the way to go, since WinForms is limited into a single thread, which makes refreshing the form more difficult or perhaps impossible to accomplish effectively.
根据我的阅读,听起来好像是带有XAML(可扩展应用程序标记语言)的WPF(Windows Presentation Foundation),因为WinForms被限制在一个线程中,这使得刷新表单变得更加困难甚至是不可能。有效地完成。
For now this is what I have (testing with google) without the automatic refresh: 现在,这是我没有自动刷新的内容(使用google测试):
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PowerShell HTML GUI" WindowStartupLocation="Manual">
<Grid>
<WebBrowser
HorizontalAlignment="Left"
Width="415"
Height="340"
Margin="10,10,0,0"
VerticalAlignment="Top"
Name="WebBrowser"
/>
</Grid>
</Window>
'@
$reader = New-Object System.Xml.XmlNodeReader($xaml)
$Form = [Windows.Markup.XamlReader]::Load($reader)
$Form.Width = 415
$Form.Height = 340
$Form.Topmost = $True
$WebBrowser = $Form.FindName('WebBrowser')
$WebBrowser.add_Loaded({$this.Navigate('http://www.google.com')})
$Form.ShowDialog()
I tried adding the following loop at the end of the code to replace $Form.ShowDialog()
, but obviously this isn't going to work. 我尝试在代码末尾添加以下循环以替换
$Form.ShowDialog()
,但是显然这行不通。
While (1) {
Start-Sleep -seconds 10
$WebBrowser.Refresh()
$Form.ShowDialog()
}
I think I need to use the System.Windows.Forms.Timer
class, but I'm not sure how to implement that into this example without any user interaction. 我想我需要使用
System.Windows.Forms.Timer
类,但是我不确定如何在没有任何用户交互的情况下将其实现到此示例中。
Anyone able to figure this out? 有人能弄清楚吗?
我从Reddit的一个用户那里得到了帮助,因为这里提供的当前答案(特别是答案的第一部分)只会让我更加困惑。
Yes, you are going to have to use a timer. 是的,您将必须使用计时器。
See the example in this article for more details. 有关更多详细信息,请参见本文中的示例。 No, it's not talking to the web control directly, but the same approach could get you there.
不,它不是直接与Web控件对话,但是相同的方法可以帮助您实现目标。
Weekend Scripter: Create a Holiday Greeting Using PowerShell and WPF 周末脚本:使用PowerShell和WPF创建节日问候
The final interesting piece of code is how we can update the content of a WPF object continually.
最后一段有趣的代码是我们如何连续更新WPF对象的内容。 The script uses this ability to display the number of days, hours, minutes & seconds until the New Year.
该脚本使用此功能来显示直到新年的天数,小时数,分钟数和秒数。 To do this, we create a $countDownUpdate scriptblock that contains a New-Timespan cmdlet.
为此,我们创建一个$ countDownUpdate脚本块,其中包含一个New-Timespan cmdlet。 This calculates the difference between now and midnight on 31st December 2010, then sets this value on the content property of a label control.
这将计算从现在到2010年12月31日午夜之间的差异,然后在标签控件的content属性上设置此值。
# scriptblock that gets called every second by the timer object
# updates the 'content' property of the countdownlabel
$countDownUpdate = {
$ts = New-TimeSpan -Start $([datetime]::now) -End $([datetime]"12/31/2010 00:00:00")
# find the countdown label control
$lbl = $objCanvas.FindName("countDown")
$lbl.content = "$($ts.Days) . $($ts.Hours) : $($ts.Minutes) : $($ts.Seconds)"
}
See also this example: 另请参见以下示例:
A PowerShell clock PowerShell时钟
I ended up using Windows Presentation Foundation
我最终使用Windows Presentation Foundation
## Create a script block which will update the UI
$counter = 0;
$updateBlock = {
# Update the clock
$clock.Resources["Time"] = [DateTime]::Now.ToString("T")
}
## Hook up some event handlers
$clock.Add_SourceInitialized( {
## Before the window's even displayed ...
## We'll create a timer
$timer = new-object System.Windows.Threading.DispatcherTimer
## Which will fire 4 times every second
$timer.Interval = [TimeSpan]"0:0:0.25"
## And will invoke the $updateBlock
$timer.Add_Tick( $updateBlock )
## Now start the timer running
$timer.Start()
if( $timer.IsEnabled ) {
Write-Host "Clock is running. Don't forget: RIGHT-CLICK to close it."
} else {
$clock.Close()
Write-Error "Timer didn't start"
}
} )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.