信息
您所需的主题如下所示。但此主题未包含在此库中。

WebView Class

提供在应用程序中承载 HTML 内容的控件。

继承

Object
  DependencyObject
    UIElement
      FrameworkElement
        WebView

语法


public sealed class WebView : FrameworkElement

特性

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]
[WebHostHidden()]

成员

WebView类 具有这些类型的成员:

构造函数

WebView类 具有这些构造函数。

构造函数说明
WebView Initializes a new instance of the WebView class.

 

事件

WebView类 具有这些事件。

事件说明
ContentLoading Occurs when the WebView has started loading new content.
DataContextChanged Occurs when the value of the FrameworkElement.DataContext property changes. (继承自FrameworkElement)
DOMContentLoaded Occurs when the WebView has finished parsing the current HTML content.
DoubleTapped (not supported; see Remarks) Occurs when an otherwise unhandled DoubleTap interaction occurs over the hit test area of this element. (继承自UIElement)
DragEnter (not supported; see Remarks) Occurs when the input system reports an underlying drag event with this element as the target. (继承自UIElement)
DragLeave (not supported; see Remarks) Occurs when the input system reports an underlying drag event with this element as the origin. (继承自UIElement)
DragOver (not supported; see Remarks) Occurs when the input system reports an underlying drag event with this element as the potential drop target. (继承自UIElement)
Drop (not supported; see Remarks) Occurs when the input system reports an underlying drop event with this element as the drop target. (继承自UIElement)
FrameContentLoading Occurs when a frame in the WebView has started loading new content.
FrameDOMContentLoaded Occurs when a frame in the WebView has finished parsing its current HTML content.
FrameNavigationCompleted Occurs when a frame in the WebView has finished loading its content.
FrameNavigationStarting Occurs before a frame in the WebView navigates to new content.
GotFocus Occurs when a UIElement receives focus. (继承自UIElement)
Holding (not supported; see Remarks) Occurs when an otherwise unhandled Hold interaction occurs over the hit test area of this element. (继承自UIElement)
KeyDown (not supported; see Remarks) Occurs when a keyboard key is pressed while the UIElement has focus. (继承自UIElement)
KeyUp (not supported; see Remarks) Occurs when a keyboard key is released while the UIElement has focus. (继承自UIElement)
LayoutUpdated Occurs when the layout of the visual tree changes, due to layout-relevant properties changing value or some other action that refreshes the layout. (继承自FrameworkElement)
LoadCompleted Occurs when top-level navigation completes and the content loads into the WebView control or when an error occurs during loading.
Loaded Occurs when a FrameworkElement has been constructed and added to the object tree, and is ready for interaction. (继承自FrameworkElement)
LongRunningScriptDetected Occurs periodically while the WebView executes JavaScript, letting you halt the script.
LostFocus Occurs when a UIElement loses focus. (继承自UIElement)
ManipulationCompleted (not supported; see Remarks) Occurs when a manipulation on the UIElement is complete. (继承自UIElement)
ManipulationDelta (not supported; see Remarks) Occurs when the input device changes position during a manipulation. (继承自UIElement)
ManipulationInertiaStarting (not supported; see Remarks) Occurs when the input device loses contact with the UIElement object during a manipulation and inertia begins. (继承自UIElement)
ManipulationStarted (not supported; see Remarks) Occurs when an input device begins a manipulation on the UIElement. (继承自UIElement)
ManipulationStarting (not supported; see Remarks) Occurs when the manipulation processor is first created. (继承自UIElement)
NavigationCompleted Occurs when the WebView has finished loading the current content or if navigation has failed.
NavigationFailed Occurs when the WebView cannot complete the navigation attempt.
NavigationStarting Occurs before the WebView navigates to new content.
PointerCanceled (not supported; see Remarks) Occurs when a pointer that made contact abnormally loses contact. (继承自UIElement)
PointerCaptureLost (not supported; see Remarks) Occurs when pointer capture previously held by this element moves to another element or elsewhere. (继承自UIElement)
PointerEntered (not supported; see Remarks) Occurs when a pointer enters the hit test area of this element. (继承自UIElement)
PointerExited (not supported; see Remarks) Occurs when a pointer leaves the hit test area of this element. (继承自UIElement)
PointerMoved (not supported; see Remarks) Occurs when a pointer moves while the pointer remains within the hit test area of this element. (继承自UIElement)
PointerPressed (not supported; see Remarks) Occurs when the pointer device initiates a Press action within this element. (继承自UIElement)
PointerReleased (not supported; see Remarks) Occurs when the pointer device that previously initiated a Press action is released, while within this element. (继承自UIElement)
PointerWheelChanged (not supported; see Remarks) Occurs when the delta value of a pointer wheel changes. (继承自UIElement)
RightTapped (not supported; see Remarks) Occurs when a right-tap input stimulus happens while the pointer is over the element. (继承自UIElement)
ScriptNotify Occurs when the content contained in the WebView control passes a string to the application by using JavaScript.
SizeChanged Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (继承自FrameworkElement)
Tapped (not supported; see Remarks) Occurs when an otherwise unhandled Tap interaction occurs over the hit test area of this element. (继承自UIElement)
Unloaded Occurs when this object is no longer connected to the main object tree. (继承自FrameworkElement)
UnsafeContentWarningDisplaying Occurs when the WebView shows a warning page for content that was reported as unsafe by SmartScreen Filter.
UnviewableContentIdentified Occurs when the WebView attempts to download an unsupported file.

 

方法

The WebView 类 具有这些方法。 它还继承方法于Object 类.

方法说明
AddHandler Adds a routed event handler for a specified routed event, adding the handler to the handler collection on the current element. Specify handledEventsToo as true to have the provided handler be invoked even if the event is handled elsewhere. (继承自UIElement)
Arrange Positions child objects and determines a size for a UIElement. Parent objects that implement custom layout for their child elements should call this method from their layout override implementations to form a recursive layout update. (继承自UIElement)
ArrangeOverride Provides the behavior for the Arrange pass of layout. Classes can override this method to define their own Arrange pass behavior. (继承自FrameworkElement)
BuildLocalStreamUri Creates a URI that you can pass to NavigateToLocalStreamUri.
CancelDirectManipulations Cancels ongoing direct manipulation processing (system-defined panning/zooming) on any ScrollViewer parent that contains the current UIElement. (继承自UIElement)
CapturePointer Sets pointer capture to a UIElement. Once captured, only the element that has capture will fire pointer-related events. (继承自UIElement)
CapturePreviewToStreamAsync Creates an image of the current WebView contents and writes it to the specified stream.
CaptureSelectedContentToDataPackageAsync Asynchronously gets a DataPackage that contains the selected content within the WebView.
ClearValue Clears the local value of a dependency property. (继承自DependencyObject)
FindName Retrieves an object that has the specified identifier name. (继承自FrameworkElement)
FindSubElementsForTouchTargeting Enables a UIElement subclass to expose child elements that assist with resolving touch targeting. (继承自UIElement)
Focus Sets the input focus to the WebView.
GetAnimationBaseValue Returns any base value established for a dependency property, which would apply in cases where an animation is not active. (继承自DependencyObject)
GetBindingExpression Returns the BindingExpression that represents the binding on the specified property. (继承自FrameworkElement)
GetValue Returns the current effective value of a dependency property from a DependencyObject. (继承自DependencyObject)
GoBack Navigates the WebView to the previous page in the navigation history.
GoForward Navigates the WebView to the next page in the navigation history.
GoToElementStateCore When implemented in a derived class, enables per-state construction of a visual tree for a control template in code, rather than by loading XAML for all states at control startup. (继承自FrameworkElement)
InvalidateArrange Invalidates the arrange state (layout) for a UIElement. After the invalidation, the UIElement will have its layout updated, which will occur asynchronously. (继承自UIElement)
InvalidateMeasure Invalidates the measurement state (layout) for a UIElement. (继承自UIElement)
InvokeScript Executes the specified script function from the currently loaded HTML, with specific arguments.
InvokeScriptAsync As an asynchronous action, executes the specified script function from the currently loaded HTML, with specific arguments.
Measure Updates the DesiredSize of a UIElement. Typically, objects that implement custom layout for their layout children call this method from their own MeasureOverride implementations to form a recursive layout update. (继承自UIElement)
MeasureOverride Provides the behavior for the Measure pass of the layout cycle. Classes can override this method to define their own Measure pass behavior. (继承自FrameworkElement)
Navigate Loads the HTML content at the specified Uniform Resource Identifier (URI).
NavigateToLocalStreamUri Loads local web content at the specified URI using an IUriToStreamResolver.
NavigateToString Loads the specified HTML content as a new document.
NavigateWithHttpRequestMessage Navigates the WebView to a URI with a POST request and HTTP headers.
OnApplyTemplate Invoked whenever application code or internal processes (such as a rebuilding layout pass) call ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays in your app. Override this method to influence the default post-template logic of a class. (继承自FrameworkElement)
OnCreateAutomationPeer When implemented in a derived class, returns class-specific AutomationPeer implementations for the Microsoft UI Automation infrastructure. (继承自UIElement)
OnDisconnectVisualChildren Override this method to implement how layout and logic should behave when items are removed from a class-specific content or children property. (继承自UIElement)
ReadLocalValue Returns the local value of a dependency property, if a local value is set. (继承自DependencyObject)
Refresh Reloads the current content in the WebView.
ReleasePointerCapture Releases pointer captures for capture of one specific pointer by this UIElement. (继承自UIElement)
ReleasePointerCaptures Releases all pointer captures held by this element. (继承自UIElement)
RemoveHandler Removes the specified routed event handler from this UIElement. Typically the handler in question was added by AddHandler. (继承自UIElement)
SetBinding Attaches a binding to a FrameworkElement, using the provided binding object. (继承自FrameworkElement)
SetValue Sets the local value of a dependency property on a DependencyObject. (继承自DependencyObject)
Stop Halts the current WebView navigation or download.
TransformToVisual Returns a transform object that can be used to transform coordinates from the UIElement to the specified object. (继承自UIElement)
UpdateLayout Ensures that all positions of child objects of a UIElement are properly updated for layout. (继承自UIElement)

 

属性

WebView类 具有这些属性。

属性访问类型说明

ActualHeight

只读Gets the rendered height of a FrameworkElement. (继承自FrameworkElement)

ActualWidth

只读Gets the rendered width of a FrameworkElement. (继承自FrameworkElement)

AllowDrop

读/写Gets or sets a value that determines whether this UIElement can be a drop target for purposes of drag-and-drop operations. (继承自UIElement)

AllowedScriptNotifyUris

读/写Gets or sets a safe list of URIs that are permitted to fire ScriptNotify events to this WebView.

AllowedScriptNotifyUrisProperty

只读Identifies the AllowedScriptNotifyUris dependency property.

AnyScriptNotifyUri

只读Gets a value that you can use to set the AllowedScriptNotifyUris property to indicate that any page can fire ScriptNotify events to this WebView.

BaseUri

只读Gets a Uniform Resource Identifier (URI) that represents the base Uniform Resource Identifier (URI) for an XAML-constructed object at XAML load time. This property is useful for Uniform Resource Identifier (URI) resolution at run time. (继承自FrameworkElement)

CacheMode

读/写Gets or sets a value that indicates that rendered content should be cached as a composited bitmap when possible. (继承自UIElement)

CanGoBack

只读Gets a value that indicates whether there is at least one page in the backward navigation history.

CanGoBackProperty

只读Identifies the CanGoBack dependency property.

CanGoForward

只读Gets a value that indicates whether there is at least one page in the forward navigation history.

CanGoForwardProperty

只读Identifies the CanGoForward dependency property.

Clip

读/写Gets or sets the RectangleGeometry used to define the outline of the contents of a UIElement. (继承自UIElement)

CompositeMode

读/写Gets or sets a property that declares alternate composition and blending modes for the element in its parent layout and window. This is relevant for elements that are involved in a mixed XAML / Microsoft DirectX UI. (继承自UIElement)

DataContext

读/写Gets or sets the data context for a FrameworkElement when it participates in data binding. (继承自FrameworkElement)

DataTransferPackage

只读Gets a clipboard DataPackage as passed to the WebView.

DataTransferPackageProperty

只读Identifies the DataTransferPackage dependency property.

DefaultBackgroundColor

读/写Gets or sets the color to use as the WebView background when the HTML content does not specify a color.

DefaultBackgroundColorProperty

只读Identifies the DefaultBackgroundColor dependency property.

DesiredSize

只读Gets the size that this UIElement computed during the measure pass of the layout process. (继承自UIElement)

Dispatcher

只读Gets the CoreDispatcher that this object is associated with. (继承自DependencyObject)

DocumentTitle

只读Gets the title of the page currently displayed in the WebView.

DocumentTitleProperty

只读Identifies the DocumentTitle dependency property.

FlowDirection

读/写Gets or sets the direction in which text and other UI elements flow within any parent element that controls their layout. This property can be set to either LeftToRight or RightToLeft. Setting FlowDirection to RightToLeft on any element sets the alignment to the right, the reading order to right-to-left and the layout of the control to flow from right to left. (继承自FrameworkElement)

Height

读/写Gets or sets the suggested height of a FrameworkElement. (继承自FrameworkElement)

HorizontalAlignment

读/写Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (继承自FrameworkElement)

IsDoubleTapEnabled

读/写Gets or sets a value that determines whether the DoubleTapped event can originate from that element. (继承自UIElement)

IsHitTestVisible

读/写Gets or sets whether the contained area of this UIElement can return true values for hit testing. (继承自UIElement)

IsHoldingEnabled

读/写Gets or sets a value that determines whether the Holding event can originate from that element. (继承自UIElement)

IsRightTapEnabled

读/写Gets or sets a value that determines whether the RightTapped event can originate from that element. (继承自UIElement)

IsTapEnabled

读/写Gets or sets a value that determines whether the Tapped event can originate from that element. (继承自UIElement)

Language

读/写Gets or sets localization/globalization language information that applies to a FrameworkElement, and also to all child elements of the current FrameworkElement in the object representation and in UI. (继承自FrameworkElement)

ManipulationMode

读/写Gets or sets the ManipulationModes value used for UIElement behavior and interaction with gestures. Setting this value enables handling the manipulation events from this element in app code. (继承自UIElement)

Margin

读/写Gets or sets the outer margin of a FrameworkElement. (继承自FrameworkElement)

MaxHeight

读/写Gets or sets the maximum height constraint of a FrameworkElement. (继承自FrameworkElement)

MaxWidth

读/写Gets or sets the maximum width constraint of a FrameworkElement. (继承自FrameworkElement)

MinHeight

读/写Gets or sets the minimum height constraint of a FrameworkElement. (继承自FrameworkElement)

MinWidth

读/写Gets or sets the minimum width constraint of a FrameworkElement. (继承自FrameworkElement)

Name

读/写Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML-declared object by this name. (继承自FrameworkElement)

Opacity

读/写Gets or sets the degree of the object's opacity. (继承自UIElement)

Parent

只读Gets the parent object of this FrameworkElement in the object tree. (继承自FrameworkElement)

PointerCaptures

只读Gets the set of all captured pointers, represented as Pointer values. (继承自UIElement)

Projection

读/写Gets or sets the perspective projection (3-D effect) to apply when rendering this element. (继承自UIElement)

RenderSize

只读Gets the final render size of a UIElement. (继承自UIElement)

RenderTransform

读/写Gets or sets transform information that affects the rendering position of a UIElement. (继承自UIElement)

RenderTransformOrigin

读/写Gets or sets the origin point of any possible render transform declared by RenderTransform, relative to the bounds of the UIElement. (继承自UIElement)

RequestedTheme

读/写Gets or sets the UI theme that is used by the UIElement (and its child elements) for resource determination. The UI theme you specify with RequestedTheme can override the app-level RequestedTheme. (继承自FrameworkElement)

Resources

读/写Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement.Resources property element, through XAML implicit collection syntax. (继承自FrameworkElement)

Source

读/写Gets or sets the Uniform Resource Identifier (URI) source of the HTML content to display in the WebView control.

SourceProperty

只读Identifies the Source dependency property.

Style

读/写Gets or sets an instance Style that is applied for this object during layout and rendering. (继承自FrameworkElement)

Tag

读/写Gets or sets an arbitrary object value that can be used to store custom information about this object. (继承自FrameworkElement)

Transitions

读/写Gets or sets the collection of Transition style elements that apply to a UIElement. (继承自UIElement)

Triggers

只读Gets the collection of triggers for animations that are defined for a FrameworkElement. Not commonly used. (继承自FrameworkElement)

UseLayoutRounding

读/写Gets or sets a value that determines whether rendering for the object and its visual subtree should use rounding behavior that aligns rendering to whole pixels. (继承自UIElement)

VerticalAlignment

读/写Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (继承自FrameworkElement)

Visibility

读/写Gets or sets the visibility of a UIElement. A UIElement that is not visible is not rendered and does not communicate its desired size to layout. (继承自UIElement)

Width

读/写Gets or sets the width of a FrameworkElement. (继承自FrameworkElement)

 

备注

使用 WebView 控件承载应用中的 Web 内容。请注意,从 Windows 8.1 开始,控件行为已经更改。有关详细信息,请参见以下内容。

WebView 不是 Control 子类且没有控件模板。显示区域由 WidthHeight 属性约束。

虽然 WebView 不是 Control 子类,它可以接收键盘焦点和 tab 键顺序参与。它提供一 Focus 方法和 GotFocusLostFocus 事件,但它没有与 Tab 键相关的属性。它在 Tab 键顺序中的位置与它在 XAML 文档顺序中的位置相同。Tab 键序列包括 WebView 内容中所有可以接收输入焦点的元素。

如事件表中所示,WebView 不支持从 UIElement 继承的大多数用户输入事件,如 KeyDownKeyUpPointerPressed。 此问题的常见解决方法是将 WebView.InvokeScript(在 Windows 8.1 中以 InvokeScriptAsync 开头)与 eval 一起使用,以便使用 HTML 事件处理程序;以及使用 HTML 事件处理程序中的 window.external.notify,以便使用 WebView.ScriptNotify 来通知应用程序。

此外,WebView 目前不支持某些 HTML5 功能,包括 AppCache、IndexedDB、对剪贴板的编程访问或地理位置。

Windows 8

这些备注仅适用于针对 Windows 8 编译的应用程序(即使在 Windows 8.1 上运行也是如此)。

WebView 具有其他 UI 区域特征,如控件无法呈现在 WebView 顶部。这样的原因是窗口区域如何在内部处理,尤其是输入事件是如何处理以及屏幕是如何绘制的。如果要呈现 HTML 内容并且还要将其他用户界面元素放置在该 HTML 内容的顶部,则应使用 WebViewBrush 作为呈现区域。WebView 仍提供 HTML 源信息,可通过元素名称绑定和 SourceName 属性引用该 WebViewWebViewBrush 没有此复盖限制。

如果要显示仅是偶尔有重叠内容的交互 WebView(例如下拉列表或应用程序栏),可以在必要时临时隐藏 WebView 控件,将其替换为一个使用 WebViewBrush 填充的元素。然后,当重叠的内容不存在时,可以再次显示原始 WebView

注意  从 Windows 8.1 开始,“空间问题”已修复。

WebView 始终使用文档模式中 Internet Explorer 10。

WebView 不支持 ms-appdata 方案,不过,它支持 ms-appx-web 方案,你可以使用该方案从应用程序包加载内容文件。



<WebView Source="ms-appx-web:///HTMLPage1.html"/>


Windows 8.1

Windows 8.1 引入了对 WebView 控件的更改,该更改修复了多个问题并添加了新功能,包括:

WebView 始终使用文档模式中 Internet Explorer 11。

Windows 8.1 弃用以下 WebView API:

这些更改不会影响为 Windows 8 生成的应用程序,即使它们在 Windows 8.1 上运行也是如此。

示例

下面的代码示例演示如何将 WebView 控件定位到包含在 TextBoxnamedAddress 中的 URL。此示例和下面示例的完整代码列表可用于 WebView 控件示例


try
{
    Uri targetUri = new Uri(Address.Text);
    WebView1.Navigate(targetUri);
}
catch (FormatException ex)
{
    // Bad address.
}


下面的代码示例演示如何将 WebView 控件加载到本地 HTML。


WebView2.NavigateToString(
    "<html><body><h2>This is an HTML fragment</h2></body></html>");

下面的代码示例演示如何使用 WebView 控件调用它加载的 HTML 中包含的 JavaScript 函数。

注意  从 Windows 8.1 开始,你应使用 InvokeScriptAsync 而不是 InvokeScript


string htmlFragment = 
    "<html><head><script type='text/javascript'>function SayGoodbye() { " +
    "document.getElementById('myDiv').innerText = 'GoodBye'; } " +
    "</script></head><body><div id='myDiv'>Hello</div></body></html>";

WebView3.NavigateToString(htmlFragment);

WebView3.InvokeScript("SayGoodbye", null);


要求

最低支持客户端

Windows 8 [仅 Windows 应用商店应用程序]

最低支持服务器

Windows Server 2012 [仅 Windows 应用商店应用程序]

命名空间

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

元数据

Windows.winmd

另请参见

FrameworkElement
XAML WebView 控件示例

 

 

显示:
© 2014 Microsoft