Share via


BrowserInteropHelper.HostScript 属性

定义

如果 XAML 浏览器应用程序 (XBAP) 托管在框架中,则获取一个脚本对象,该对象提供对 HTML 页面的 HTML 窗口对象、自定义脚本函数和全局变量的访问权限。

public:
 static property System::Object ^ HostScript { System::Object ^ get(); };
public static dynamic HostScript { [System.Security.SecurityCritical] [System.Security.SecurityTreatAsSafe] get; }
public static dynamic HostScript { get; }
[<get: System.Security.SecurityCritical>]
[<get: System.Security.SecurityTreatAsSafe>]
static member HostScript : obj
static member HostScript : obj
Public Shared ReadOnly Property HostScript As Object

属性值

如果 XAML 浏览器应用程序 (XBAP) 托管在框架中,则为 HTML 页面提供对 HTML 窗口对象、自定义脚本函数和全局变量的访问权限的脚本对象;否则为 null

属性

示例

以下示例演示如何检索 HostScript 对象并关闭浏览器窗口。

<Grid>
   <Button Width="160" Height="60" Click="Button_Click" Content="Close Browser Window" />
 </Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
    // Retrieve the script object. The XBAP must be hosted in a frame or
    // the HostScript object will be null.
    var scriptObject = BrowserInteropHelper.HostScript;

    // Call close to close the browser window. 
    scriptObject.Close();
}
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Retrieve the script object  The XBAP must be hosted in a frame or
    ' the HostScript object will be null.
    Dim scriptObject = BrowserInteropHelper.HostScript

    ' Call close to close the browser window.
    scriptObject.Close()
End Sub

注解

使用返回的对象 HostScript ,可以访问 HTML 窗口对象的属性、方法和事件、直接调用脚本函数或访问全局变量。 访问这些项目的语法使用熟悉的点表示法。

有关窗口对象的详细信息,请参阅 Window

如果从跨域位置导航到框架,则 HostScript 属性返回 null

可以通过设置 ScriptInteropDisallow 注册表值为浏览器关闭此功能。 有关详细信息,请参阅 安全 (WPF)

适用于