【问题标题】:PowerShell Form with Web Browser that Automatically Refreshes带有自动刷新的 Web 浏览器的 PowerShell 表单
【发布时间】:2019-06-02 10:52:35
【问题描述】:

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

现在这是我所拥有的(用谷歌测试),没有自动刷新:

[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()

我尝试在代码末尾添加以下循环来替换$Form.ShowDialog(),但显然这行不通。

While (1) {
    Start-Sleep -seconds 10
    $WebBrowser.Refresh()
    $Form.ShowDialog()
}

我认为我需要使用 System.Windows.Forms.Timer 类,但我不确定如何在没有任何用户交互的情况下将其实现到此示例中。

谁知道这个?

【问题讨论】:

    标签: wpf powershell xaml refresh webbrowser-control


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      是的,您将不得不使用计时器。

      有关详细信息,请参阅本文中的示例。不,它不是直接与 Web 控件对话,但同样的方法可以让你到达那里。

      Weekend Scripter: Create a Holiday Greeting Using PowerShell and WPF

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

      # 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)"
      }
      

      另见此示例:

      A PowerShell clock

      我最终使用了 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"            
          }            
       } ) 
      

      【讨论】:

      • 谢谢,但我真的还在为如何让网络浏览器页面刷新而苦苦挣扎。我假设我会将刷新页面的代码放入$countDownUpdate 脚本块中?我不断收到此错误 Error HRESULT E_FAIL has been returned from a call to a COM component on $WebBrowser.Refresh().
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2018-12-05
      • 2018-06-12
      • 2015-05-10
      • 2015-03-23
      • 2011-10-25
      • 2017-06-07
      相关资源
      最近更新 更多