Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 带自动刷新的Web浏览器的PowerShell表单_Wpf_Powershell_Xaml_Refresh_Webbrowser Control - Fatal编程技术网

Wpf 带自动刷新的Web浏览器的PowerShell表单

Wpf 带自动刷新的Web浏览器的PowerShell表单,wpf,powershell,xaml,refresh,webbrowser-control,Wpf,Powershell,Xaml,Refresh,Webbrowser Control,我正在尝试在PowerShell中创建一个小窗口中的简单表单,该窗口始终位于其他窗口的顶部,并且只包含一个web浏览器对象,该对象每10秒自动刷新一次。我一直在网上搜索并尝试各种选择,但似乎没有任何效果。几乎我遇到的每个示例都会在按下按钮时刷新表单或web浏览器,但我需要web浏览器自动刷新,而无需任何用户交互。从我所读到的内容来看,使用XAML(可扩展应用程序标记语言)的WPF(Windows Presentation Foundation)似乎是一条可行之路,因为WinForms仅限于一个线

我正在尝试在PowerShell中创建一个小窗口中的简单表单,该窗口始终位于其他窗口的顶部,并且只包含一个web浏览器对象,该对象每10秒自动刷新一次。我一直在网上搜索并尝试各种选择,但似乎没有任何效果。几乎我遇到的每个示例都会在按下按钮时刷新表单或web浏览器,但我需要web浏览器自动刷新,而无需任何用户交互。从我所读到的内容来看,使用XAML(可扩展应用程序标记语言)的WPF(Windows Presentation Foundation)似乎是一条可行之路,因为WinForms仅限于一个线程,这使得刷新表单更加困难,或者可能无法有效完成

目前,这是我在没有自动刷新的情况下(使用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()
我想我需要使用
System.Windows.Forms.Timer
类,但我不确定如何在没有任何用户交互的情况下在这个示例中实现它


有人能弄明白吗?

是的,你必须使用计时器

有关更多详细信息,请参见本文中的示例。不,它不是直接与web控件对话,但是同样的方法可以让您达到目的

最后一段有趣的代码是如何更新内容 一个WPF对象的。脚本使用此功能来显示 新年前的天数、小时数、分钟数和秒数。做 为此,我们创建一个$countDownUpdate脚本块,其中包含 新的Timespan cmdlet。这将计算现在和现在之间的差异 2010年12月31日午夜,然后在内容上设置此值 标签控件的属性

另请参见此示例:

我最终使用了Windows演示基金会


我从中的一位用户那里得到了这方面的帮助,因为这里提供的当前答案,特别是答案的第一部分,只会让我更加困惑。

谢谢,但我仍然在为如何让web浏览器页面刷新而苦苦挣扎。我假设我会将刷新页面的代码放入
$countDownUpdate
脚本块?我不断收到此错误,调用
$WebBrowser.Refresh()
上的COM组件时返回了HRESULT E_FAIL。
While (1) {
    Start-Sleep -seconds 10
    $WebBrowser.Refresh()
    $Form.ShowDialog()
}
# 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)"
}
## 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"            
    }            
 } )