Overview
In the UWP Community Toolkit Extensions, there is an extension for WebView-webviewextensions, which we'll cover in detail with the code for WebView Extensions implementations.
WebView Extensions allows the use of additional properties to specify the destination address or target page content of the WebView in XAML, there is no corresponding example in the official sample application for this extension, and we implement a simple example:
Source:https://github.com/microsoft/uwpcommunitytoolkit/tree/master/microsoft.toolkit.uwp.ui/extensions/webview
Doc:https://docs.microsoft.com/zh-cn/windows/uwpcommunitytoolkit/extensions/webviewextensions
Namespace: Microsoft.Toolkit.Uwp.UI.Extensions; Nuget: Microsoft.Toolkit.Uwp.UI;
Development process
Code Analysis
Let's take a look at the class structure of webviewextensions:
Where the Webview.cs and WebView.Properties.cs classes have been marked as Obsolete and replaced by WebViewExtensions.cs.
WebViewExtensions.cs
Let's start by looking at several additional properties defined in the class:
- Content-webview the target page content, type string, change the oncontentchanged event triggered;
- Contenturi-webview destination page address, type Uri, trigger oncontenturichanged event when changing;
Next look at the processing logic for these two events:
1. oncontentchanged (d, E)
This method is very simple, is to read the changed string, using the WebView navigatetostring method to assign values; Note that the content here is not necessarily a valid HTML page, it can be a normal string, WebView This string is also displayed with the default font style;
Private Static void oncontentchanged (DependencyObject D, DependencyPropertyChangedEventArgs e) { as Windows.UI.Xaml.Controls.WebView; var as string ; if (string. IsNullOrEmpty (content)) { return; } WV? . navigatetostring (content);}
2. oncontenturichanged (d, E)
The implementation of this method is also very simple, read and convert the content to the URI type, if valid jump to the corresponding URI website; Here the value required must be a well-formed URL, although not necessarily a usable URL, it will cause crash;
Private Static void oncontenturichanged (DependencyObject D, DependencyPropertyChangedEventArgs e) { as Windows.UI.Xaml.Controls.WebView; var as Uri; if NULL ) { return; } WV? . Navigate (URI);}
Invoke Example
We let Webview binding TextBlock Text as Contenturi, you can see Webview correctly jump to the specified URL;
<Text= "http://www.cnblogs.com/shaomeng/" x:name= "uri"/ ><grid.row= "1" extensions:WebView.ContentUri = "{Binding Text, Elementname=uri}"/>
Summarize
Here we will take the UWP Community Toolkit Extensions in the webviewextensions of the source code implementation process and simple invocation of the sample to complete, I hope that we can better understand and use this extension to help. Welcome everyone to Exchange, thank you!
Finally, with everyone Amway Uwpcommunitytoolkit official micro-blog:https://weibo.com/u/6506046490, You can follow the latest news through Weibo.
Heartfelt thanks to the outstanding work of Uwpcommunitytoolkit's authors, Thank you much, uwpcommunitytoolkit authors!!!
Extensions in UWP Community toolkit-webviewextensions