重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

时间:2023-01-01 13:04:05

原文:重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

[源码下载]

重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

作者:webabcd

介绍
重新想象 Windows 8.1 Store Apps 之控件增强

  • WebView 加载本地 html,智能替换 html 中的 url 引用
  • WebView 通过 Share Contract 分享
  • WebView 截图

示例
1、演示如何通过 WebView 加载本地 html, 以及智能替换 html 中的 url 引用
WebView/Local.xaml

<Page
x:Class="Windows81.Controls.WebView.Local"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Controls.WebView"
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="120 0 0 0"> <Button Name="btnHtml" Content="解析指定的 html 字符串" Click="btnHtml_Click" />
<Button Name="btnPackageHtml" Content="解析 package 中的 html" Margin="0 10 0 0" Click="btnPackageHtml_Click" />
<Button Name="btnAppDataHtml" Content="解析 appData 中的 html" Margin="0 10 0 0" Click="btnAppDataHtml_Click" /> <Button Name="btnSmart" Content="智能替换 html 中的 url 引用" Margin="0 10 0 0" Click="btnSmart_Click" /> <WebView Name="webView" Width="800" Height="300" HorizontalAlignment="Left" Margin="0 10 0 0" /> </StackPanel>
</Grid> </Page>

WebView/Local.xaml.cs

/*
* 本例演示如何通过 WebView 加载指定的 html 字符串,加载 package 中的 html,加载 appData 中的 html
* 还会演示如何以内容流的方式加载 html 数据,其有如下作用:
* 1、可以实现完全从本地加载全部页面数据(包括 html,js,css,图片等)
* 2、可以实现智能替换 html 中的 url 引用
*
* WebView - 内嵌浏览器
*
*
* 提示:
* 在 win8 中 WebView 会挡住所有元素(包括 AppBar),而在 win8.1 中不会再有这种情况了
* 如果想看看 win8 中全屏 WebView 如何不挡 AppBar,以及如何使用 WebViewBrush 以达到不遮挡其他元素的目的,请参看:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*
* 注:win8 和 win8.1 中的 WebView 有很多区别,在 win8.1 中使用 WebView 请参考本系列教程,在 win8 中使用 WebView 请看考:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*/ using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web;
using Windows.Web.Http; namespace Windows81.Controls.WebView
{
public sealed partial class Local : Page
{
public Local()
{
this.InitializeComponent();
} private void btnHtml_Click(object sender, RoutedEventArgs e)
{
// 解析指定的 html 字符串
webView.NavigateToString("<html><body>I am webabcd</body></html>");
} private void btnPackageHtml_Click(object sender, RoutedEventArgs e)
{
// 解析 package 中的 html
string url = "ms-appx-web:///Controls/WebView/HtmlDemo.html";
webView.Navigate(new Uri(url));
} private async void btnAppDataHtml_Click(object sender, RoutedEventArgs e)
{
// 将程序包内的 html 文件复制到 ApplicationData 中的 LocalFolder
StorageFolder localFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("webabcdTest", CreationCollisionOption.OpenIfExists);
StorageFile htmlFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Controls/WebView/HtmlDemo.html"));
await htmlFile.CopyAsync(localFolder, "HtmlDemo.html", NameCollisionOption.ReplaceExisting); // 解析 appData 中的 html
string url = "ms-appdata:///local/webabcdTest/HtmlDemo.html";
webView.Navigate(new Uri(url));
} // 以内容流的方式加载 html 数据
// 智能替换 html 中的 url 引用
// 在本例中,html 页面来自本地,html 中的引用来自远程
private void btnSmart_Click(object sender, RoutedEventArgs e)
{
// 构造可传递给 NavigateToLocalStreamUri() 的 URI
Uri url = webView.BuildLocalStreamUri("contentIdentifier", "/Controls/WebView/HtmlDemo.html");
// 实例化一个实现 IUriToStreamResolver 接口的类
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver(); // 所有 url(包括 html 的 url 以及 html 内所有引用的 url)都会通过 StreamUriWinRTResolver 来返回数据流
webView.NavigateToLocalStreamUri(url, myResolver);
}
} // 实现 IUriToStreamResolver 接口(用于将 url 以内容流的方式返回)
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
// IUriToStreamResolver 接口只有一个需要实现的方法
// 根据 uri 返回对应的内容流
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
string path = uri.AbsolutePath;
return GetContent(path).AsAsyncOperation();
} // 根据 uri 返回对应的内容流
private async Task<IInputStream> GetContent(string path)
{
string url; // 按需求修改 url 引用
if (path == "/Controls/WebView/HtmlDemo.html")
url = "ms-appx://" + path;
else
url = "http://ajax.googleapis.com" + path; if (url.StartsWith("http"))
{
// http 方式获取内容数据
HttpResponseMessage response = await new HttpClient().GetAsync(new Uri(url), HttpCompletionOption.ResponseHeadersRead);
return await response.Content.ReadAsInputStreamAsync();
}
else
{
// 获取本地数据
StorageFile fileRead = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri(url, UriKind.Absolute));
return await fileRead.OpenAsync(FileAccessMode.Read);
}
}
}
}

本地 Html: 
WebView/HtmlDemo.html

<!--此 html 用于演示如何通过 WebView 加载本地 html,以及如何以内容流的方式加载 html 数据-->

<!doctype html>
<html>
<head>
<title></title>
<script type="text/javascript" src="/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<p>i am html2</p>
<p id="lblMsg"></p>
<script type="text/javascript">
document.getElementById("lblMsg").innerHTML = window.jQuery;
</script>
</body>
</html>

2、演示如何通过 Share Contract 分享 WebView 中的内容(复制到剪切板也是同理)
WebView/Share.xaml

<Page
x:Class="Windows81.Controls.WebView.Share"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Controls.WebView"
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="120 0 0 0"> <Button Name="btnShare" Content="通过 Share Contract 分享 WebView 中的被选中的文本内容" Click="btnShare_Click" /> <WebView Name="webView" Width="400" Height="300" Source="http://webabcd.cnblogs.com/" HorizontalAlignment="Left" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

WebView/Share.xaml.cs

/*
* 本例演示如何通过 Share Contract 分享 WebView 中的内容(复制到剪切板也是同理)
*
* WebView - 内嵌浏览器
* CaptureSelectedContentToDataPackageAsync() - 获取 DataPackage 对象
* DataRequested - 共享操作开始时触发的事件(事件参数 DataRequestedEventArgs)
*
* DataRequestedEventArgs - 共享操作开始时触发的事件
* GetDeferral() - 获取异步操作对象,同时开始异步操作,之后通过 Complete() 通知完成异步操作
*
* 关于 DataPackage 等共享相关的知识点请参见:http://www.cnblogs.com/webabcd/archive/2013/07/04/3171085.html
*
*
*
* 提示:
* 在 win8 中 WebView 会挡住所有元素(包括 AppBar),而在 win8.1 中不会再有这种情况了
* 如果想看看 win8 中全屏 WebView 如何不挡 AppBar,以及如何使用 WebViewBrush 以达到不遮挡其他元素的目的,请参看:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*
* 注:win8 和 win8.1 中的 WebView 有很多区别,在 win8.1 中使用 WebView 请参考本系列教程,在 win8 中使用 WebView 请看考:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*/ using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows81.Controls.WebView
{
public sealed partial class Share : Page
{
private DataTransferManager _dataTransferManager; public Share()
{
this.InitializeComponent();
} private void btnShare_Click(object sender, RoutedEventArgs e)
{
_dataTransferManager = DataTransferManager.GetForCurrentView();
_dataTransferManager.DataRequested += _dataTransferManager_DataRequested; DataTransferManager.ShowShareUI();
} // 分享 WebView 中的被选中的文本内容
async void _dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
DataRequest request = args.Request;
DataRequestDeferral deferral = args.Request.GetDeferral(); DataPackage dataPackage = await webView.CaptureSelectedContentToDataPackageAsync();
DataPackageView dataPackageView = dataPackage.GetView(); // 如果用户选择了一段内容,则通过 WebView.CaptureSelectedContentToDataPackageAsync() 获取到的数据里就会有 StandardDataFormats.Text 格式的内容,此内容就是用户所选中的内容
if (dataPackageView.Contains(StandardDataFormats.Text))
{
dataPackage.Properties.Title = "Title";
dataPackage.Properties.Description = "Description"; request.Data = dataPackage;
}
else
{
request.FailWithDisplayText("没有选中任何内容");
} _dataTransferManager.DataRequested -= _dataTransferManager_DataRequested; deferral.Complete();
}
}
}

3、演示如何对 WebView 中的内容截图
WebView/Capture.xaml

<Page
x:Class="Windows81.Controls.WebView.Capture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.Controls.WebView"
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="120 0 0 0"> <Button Name="btnCapture" Content="对此 WebView 中的当前内容截图" Click="btnCapture_Click" /> <WebView Name="webView" Width="400" Height="300" Source="http://webabcd.cnblogs.com/" HorizontalAlignment="Left" Margin="0 10 0 0" /> <StackPanel Margin="0 10 0 0" Orientation="Horizontal">
<Image Name="imageOriginal" Width="400" Height="300" HorizontalAlignment="Left" />
<Image Name="imageSmall" Width="400" Height="300" HorizontalAlignment="Left" Margin="10 0 0 0" />
</StackPanel> </StackPanel>
</Grid>
</Page>

WebView/Capture.xaml.cs

/*
* 本例演示如何对 WebView 中的内容截图
*
* WebView - 内嵌浏览器
*
*
* 提示:
* 在 win8 中 WebView 会挡住所有元素(包括 AppBar),而在 win8.1 中不会再有这种情况了
* 如果想看看 win8 中全屏 WebView 如何不挡 AppBar,以及如何使用 WebViewBrush 以达到不遮挡其他元素的目的,请参看:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*
* 注:win8 和 win8.1 中的 WebView 有很多区别,在 win8.1 中使用 WebView 请参考本系列教程,在 win8 中使用 WebView 请看考:http://www.cnblogs.com/webabcd/archive/2013/03/04/2942242.html
*/ 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 Windows81.Controls.WebView
{
public sealed partial class Capture : Page
{
public Capture()
{
this.InitializeComponent();
} private async void btnCapture_Click(object sender, RoutedEventArgs e)
{
// 对 WebView 中的内容截图,并将原始图像数据放入内存流
InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
await webView.CapturePreviewToStreamAsync(ms); // 显示原始截图
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
imageOriginal.Source = bitmapImage; // 定义缩略图的大小(最长边定义为 180)
int longlength = , width = , height = ;
double srcwidth = webView.ActualWidth, srcheight = webView.ActualHeight;
double factor = srcwidth / srcheight;
if (factor < )
{
height = longlength;
width = (int)(longlength * factor);
}
else
{
width = longlength;
height = (int)(longlength / factor);
} // 显示原始截图的缩略图
BitmapSource small = await resize(width, height, ms);
imageSmall.Source = small;
} // 返回指定原图的缩略图数据
private async Task<BitmapSource> resize(int width, int height, IRandomAccessStream source)
{
WriteableBitmap small = 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(small.PixelBuffer); return small;
}
}
}

OK
[源码下载]

重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图的更多相关文章

  1. 重新想象 Windows 8&period;1 Store Apps &lpar;81&rpar; - 控件增强&colon; WebView 之加载本地 html&comma; 智能替换 html 中的 url 引用&comma; 通过 Share Contract 分享 WebView 中的内容&comma; 为 WebView 截图

    [源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...

  2. 重新想象 Windows 8&period;1 Store Apps &lpar;77&rpar; - 控件增强&colon; 文本类控件的增强&comma; 部分控件增加了 Header 属性和 HeaderTemplate 属性&comma; 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  3. 重新想象 Windows 8&period;1 Store Apps &lpar;78&rpar; - 控件增强&colon; ScrollViewer&comma; FlipView&comma; Popup

    [源码下载] 重新想象 Windows 8.1 Store Apps (78) - 控件增强: ScrollViewer, FlipView, Popup 作者:webabcd 介绍重新想象 Wind ...

  4. 重新想象 Windows 8&period;1 Store Apps &lpar;79&rpar; - 控件增强&colon; MediaElement&comma; Frame

    [源码下载] 重新想象 Windows 8.1 Store Apps (79) - 控件增强: MediaElement, Frame 作者:webabcd 介绍重新想象 Windows 8.1 St ...

  5. 重新想象 Windows 8&period;1 Store Apps &lpar;80&rpar; - 控件增强&colon; WebView 之基本应用&comma; POST 数据&comma; 与 JavaScript 交互

    [源码下载] 重新想象 Windows 8.1 Store Apps (80) - 控件增强: WebView 之基本应用, POST 数据, 与 JavaScript 交互 作者:webabcd 介 ...

  6. 重新想象 Windows 8&period;1 Store Apps &lpar;93&rpar; - 控件增强&colon; GridView&comma; ListView

    [源码下载] 重新想象 Windows 8.1 Store Apps (93) - 控件增强: GridView, ListView 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  7. 重新想象 Windows 8&period;1 Store Apps 系列文章索引

    [源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  8. 重新想象 Windows 8&period;1 Store Apps &lpar;85&rpar; - 警报通知(闹钟)&comma; Tile 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Ap ...

  9. 重新想象 Windows 8&period;1 Store Apps &lpar;84&rpar; - 图像处理的新特性&comma; Share Contract 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...

随机推荐

  1. ECMAScript 6 扫盲

    ECMAScript 6 目前基本成为业界标准,它的普及速度比 ES5 要快很多,主要原因是现代浏览器对 ES6 的支持相当迅速,尤其是 Chrome 和 Firefox 浏览器,已经支持 ES6 中 ...

  2. windows查看占用端口的进程

    1方法 先找到进程号: netstat -aon|findstr 再根据进程号得到进程: tasklist |findstr " 2结果

  3. Angular&period;js入门教程

    简单介绍 AngularJS是为了克服HTML在构建应用上的不足而设计的.首先Html是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了. 通常,我们可以通过以下技术来解 ...

  4. VMware 虚拟机使用 NAT 方式联网

    选择要设置的虚拟主机: 点击右键,选择 “属性”,查看 “网络适配器”: 此时选择的连接方式是 “Host-only”,在 Host-only 模式中,所有的虚拟系统是可以相互通信的,但虚拟系统和真实 ...

  5. 设置Delphi XE4默认界面样式

    VCL BitMap Style   Proceject Options->Application->Appearance 选择几个样式 使用代码设置   uses Vcl.Themes; ...

  6. C&plus;&plus;语言十进制数,CDecimal(未完成)

    在C#和Java中都有存在decimal类似的十进制数字,C++中尚未发现,春节假期忙里抽闲写了一个玩玩,时间紧迫没有测试,只能保证编译通过.抛砖引玉,欢迎大家多提建议 当前缺陷: 1. 除法功能没有 ...

  7. mysql下的SELECT INTO语句

    在mysql下使用SELECT INTO语句会产生ERROR 1327 (42000): Undeclared variable:new_tablename 此时要使用: CREATE TABLE C ...

  8. MFC全局函数开局——AfxGetApp解剖

    MFC中有不少的全局函数,方便在不同对象中获取不同的内容或创建不同的对象.主要全局函数有: AfxWinInit() AfxBeginThread() AfxEndThread() AfxFormat ...

  9. Alpha阶段项目复审(冲鸭队)

    Alpha阶段项目复审(冲鸭队) 组名 优点 缺点 排名 天冷记得穿秋裤队 支持文件离线开源下载,没有限速 部分功能未实现 1 中午吃啥队 点餐系统用户需求较高,系统功能完善 界面可以再完善一下些 2 ...

  10. &lpar;ecj&rpar;Eclipse的Java编译器分析之一——ecj介绍

    Java是一个开放的平台,对于除发布编译器/解释器/基础类库之外,该语言的负责机构更多的是制定一系列标准,任何符合标准的厂商产品均可用于市场投放.甚至包括其编译器及解释器. (比如Hibernate提 ...