World War I Windows 10 (65) and World War I 65
[Download source code]
Backwater world war I Windows 10 (65)-control (WebView): Share the selected content in WebView with Share Contract
Author: webabcd
Introduction
WebView)
- For WebView content
- Share Contract to Share the selected content in WebView
Example
1. demonstrate how to view the content in WebView
Controls/WebViewDemo/WebViewDemo5.xaml
<Page x: Class = "Windows10.Controls. WebViewDemo. WebViewDemo5" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Xmlns: local =" using: Windows10.Controls. WebViewDemo "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "Xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "Mc: ignorable = "d"> <Grid Background = "Transparent"> <StackPanel Margin = "10 0 10 10"> <Button Name = "btnCapture" Content = "current Content in this WebView "Click =" btnCapture_Click "Margin =" 5 "/> <WebView Name =" webView "Width =" 400 "Height =" 300 "Source =" http://webabcd.cnblogs.com/ "HorizontalAlignment =" Left "Margin =" 5 "/> <StackPanel Margin =" 5 "Orientation =" Horizontal "> <Image Name =" imageOriginal "Width =" 400 "Height = ""300" HorizontalAlignment = "Left"/> <Image Name = "imageThumbnail" Width = "400" Height = "300" HorizontalAlignment = "Left" Margin = "10 0 0 0" /> </StackPanel> </Grid> </Page>
Controls/WebViewDemo/WebViewDemo5.xaml. cs
/** WebView-embedded browser Controls (inherited from FrameworkElement, see/Controls/BaseControl/FrameworkElementDemo/) * CapturePreviewToStreamAsync ()-for the content currently displayed in WebView, and write the image to the specified stream ** this example is used to demonstrate how to content in the WebView */using System; using System. runtime. interopServices. windowsRuntime; using System. threading. tasks; using Windows. graphics. imaging; using Windows. storage. streams; using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. media. imaging; namespace Windows10.Controls. webViewDemo {public sealed partial class WebViewDemo5: Page {public WebViewDemo5 () {this. initializeComponent ();} private async void btnCapture_Click (object sender, RoutedEventArgs e) {// content in WebView, And put original image data into memory stream InMemoryRandomAccessStream MS = new bytes (); await webView. capturePreviewToStreamAsync (MS); // display original BitmapImage bitmapImage = new BitmapImage (); bitmapImage. setSource (MS); imageOriginal. source = bitmapImage; // defines the thumbnail size (the longest side is 180) int longlength = 180, width = 0, height = 0; double srcwidth = webView. actualWidth, srcheight = webView. actualHeight; double factor = srcwidth/srcheight; if (factor <1) {height = longlength; width = (int) (longlength * factor);} else {width = longlength; height = (int) (longlength/factor);} // display original thumbnail BitmapSource thumbnail = await resize (width, height, MS); imageThumbnail. source = thumbnail;} // modify the specified image to the specified size, and return the Modified Image private async Task <BitmapSource> resize (int width, int height, IRandomAccessStream source) {WriteableBitmap thumbnail = new WriteableBitmap (width, height); BitmapDecoder decoder = await BitmapDecoder. createAsync (source); BitmapTransform transform = new BitmapTransform (); transform. scaledHeight = (uint) height; transform. scaledWidth = (uint) width; PixelDataProvider pixelData = await decoder. getPixelDataAsync (BitmapPixelFormat. bgra8, BitmapAlphaMode. straight, transform, ExifOrientationMode. respectExifOrientation, ColorManagementMode. doNotColorManage); pixelData. detachPixelData (). copyTo (thumbnail. pixelBuffer); return thumbnail ;}}}
2. demonstrate how to Share selected content in WebView through Share Contract
Controls/WebViewDemo/WebViewDemo6.xaml
<Page x: Class = "Windows10.Controls. webViewDemo. webViewDemo6 "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: local =" using: Windows10.Controls. webViewDemo "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "mc: ignorable = "d"> <Grid Background = "Transparent"> <StackPanel Margin = "10 0 10 10"> <Button Name = "btnShare" Content = "Share Contract to Share WebView (If NO content is selected, share the webpage address) "Click =" btn1__click "Margin =" 5 "/> <WebView Name =" webView "Width =" 400 "Height =" 300 "Source =" http://webabcd.cnblogs.com/"HorizontalAlignment =" Left "Margin =" 5 "/> </StackPanel> </Grid> </Page>
Controls/WebViewDemo/WebViewDemo6.xaml. cs
/** WebView-embedded browser Controls (inherited from FrameworkElement, see/Controls/BaseControl/FrameworkElementDemo/) * CaptureSelectedContentToDataPackageAsync () -convert the selected content to the DataPackage object * DataRequested-events triggered when the sharing operation starts (the event parameter DataRequestedEventArgs) ** DataRequestedEventArgs * GetDeferral ()-Get the asynchronous operation object, start asynchronous operations at the same time, and then Complete asynchronous operations through the Complete () Notification *** This example shows how to Share the selected content in the WebView through Share Contract (If NO content is selected, share the webpage address) */using System; Using Windows. applicationModel. dataTransfer; using Windows. UI. xaml; using Windows. UI. xaml. controls; namespace Windows10.Controls. webViewDemo {public sealed partial class WebViewDemo6: Page {private DataTransferManager _ dataTransferManager; public WebViewDemo6 () {this. initializeComponent ();} private void btn1__click (object sender, RoutedEventArgs e) {_ dataTransferManager = DataTransferManager. GetForCurrentView (); _ dataTransferManager. dataRequested + = _ dataTransferManager_DataRequested; DataTransferManager. showemediui () ;}// share the selected content async void _ dataTransferManager_DataRequested (DataTransferManager sender, DataRequestedEventArgs args) {DataRequest request = args. request; DataRequestDeferral deferral = args. request. getDeferral (); // If dataPackage is null, the user does not select any content Dat APackage dataPackage = await webView. CaptureSelectedContentToDataPackageAsync (); // determines whether the user has selected the shared content bool hasSelection = false; try {hasSelection = (dataPackage! = Null) & (dataPackage. getView (). availableFormats. count> 0);} catch (Exception ex) {switch (ex. HResult) {// cannot generate data package case unchecked (int) 0x80070490): hasSelection = false; break; default: throw;} if (hasSelection) for the selected content) {dataPackage. properties. title = "Title (hasSelection)";} else {// if you have not selected any content, share the webpage address dataPackage = new DataPackage (); dataPackage. setWebLink (webView. source); dataPackage. properties. title = "Title";} dataPackage. properties. description = "Description"; request. data = dataPackage; _ dataTransferManager. dataRequested-= _ dataTransferManager_DataRequested; deferral. complete ();}}}
OK
[Download source code]