吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1149|回复: 6
收起左侧

[其他原创] UWP 中 Webview2 支持请求 超过2M 的 html模板

  [复制链接]
pjy612 发表于 2023-5-22 12:01
本帖最后由 pjy612 于 2023-5-22 12:11 编辑

因为 微软要对 Xamarin 终止支持了。
https://dotnet.microsoft.com/zh-cn/platform/support/policy/xamarin?ocid=AID3042760

然后 老的 Win8.1 的 Win App 要升级。。。
唉,咱一个后端还要帮忙兼顾App前端的活儿。。。
先迁移到 UWP ,然后 老的 IE Webview 就可以升级到 Webview2 了!
接着就出了 坑爹的bug,加载大容量的Html 就提示 参数错误。。。
但需求 要在 wv2 上面显示,其中还有可能需要运行 js... 然后 存cookie 什么的...

加载 内存 html模板的 函数 为 NavigateToString
https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatetostring?view=webview2-dotnet-1.0.864.35#remarks

里面提到
  
The htmlContent parameter may not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size. The origin of the new page is about:blank.

这尼玛 坑了... url 是 about:blank 的话 就不能用 js 操作 cookie 和 localstorage 什么的。

然后 网上查的 解决方案 也没太详细的。
官方也有类似的反馈 https://github.com/MicrosoftEdge/WebView2Feedback/issues/1355

最后反正弄了一套解决方案出来。。。
[C#] 纯文本查看 复制代码
public static class WebView2LargeHtmlExtension
{
    /// <summary>
    /// htmlTemplate Cache
    /// </summary>
    static readonly Dictionary<string, string> TemplateCache = new Dictionary<string, string>();

    public static async void NavigateToHtml(this WebView2 browser, string template)
    {
        string key = Guid.NewGuid().ToString();
        TemplateCache[key] = template;
        browser.Source = new Uri($"https://template/?key={key}");
    }

    public static void RegLargeHtmlHandler(this CoreWebView2 cwv2)
    {
        cwv2.AddWebResourceRequestedFilter("https://template/*", CoreWebView2WebResourceContext.All);
        cwv2.WebResourceRequested += CoreWebView2_WebResourceRequested_LargeHtmlHandler;
    }
    private static async void CoreWebView2_WebResourceRequested_LargeHtmlHandler(CoreWebView2 sender, CoreWebView2WebResourceRequestedEventArgs args)
    {
        string requestUri = args.Request.Uri;
        if (requestUri.StartsWith("https://template/"))
        {
            Deferral def = args.GetDeferral();
            try
            {
                Uri uri = new Uri(requestUri);
                NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(uri.Query);
                string headers = $"Content-Type: text/html; charset=utf-8";
                if (TemplateCache.Remove(queryString["key"], out string html))
                {
                    InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
                    using (var dataWriter = new DataWriter(ms))
                    {
                        dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                        dataWriter.ByteOrder = ByteOrder.LittleEndian;
                        dataWriter.WriteString(html);
                        await dataWriter.StoreAsync();
                        await dataWriter.FlushAsync();
                        dataWriter.DetachStream();
                        ms.Seek(0);
                    }
                    args.Response = sender.Environment.CreateWebResourceResponse(ms, 200, "OK", headers);
                }
            }
            catch (Exception)
            {
                args.Response = sender.Environment.CreateWebResourceResponse(null, 404, "Not found", "");
            }
            finally
            {
                def.Complete();
            }
        }
    }
}


private async void Webview2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
{
    sender.CoreWebView2.RegLargeHtmlHandler();
    //other WebView2 CoreWebView2 init code
}

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
3yu3 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Pojie1999.0909 发表于 2023-5-22 14:27
Xamarin没了,.net怎么开发移动端App?
 楼主| pjy612 发表于 2023-5-22 15:33
Pojie1999.0909 发表于 2023-5-22 14:27
Xamarin没了,.net怎么开发移动端App?

说是让迁移到 移动端的 .Net SDK 而不用 Xamarin SDK 了

不过我这边 协助 win 上面的 App 迁移。 所以先到UWP
glionying 发表于 2023-5-29 12:39
pjy612 发表于 2023-5-22 15:33
说是让迁移到 移动端的 .Net SDK 而不用 Xamarin SDK 了

不过我这边 协助 win 上面的 App 迁移。 所以 ...

后面跨平台低端的app开发估计也会逐渐被大厂慢慢取代
微软的 MAUI
谷歌的 Flutter
这两年 Reactive Native也好也不如前几年了

BTW,  今天偶尔看到fiddle enhance那个仓库 。。。。
 楼主| pjy612 发表于 2023-5-29 13:04
本帖最后由 pjy612 于 2023-5-29 13:06 编辑
glionying 发表于 2023-5-29 12:39
后面跨平台低端的app开发估计也会逐渐被大厂慢慢取代
微软的 MAUI
谷歌的 Flutter

BTW 是啥?

本来是计划迁移到 MAUI 但是 MAUI 的 基础控件 支持太差了。。。 得再观望下。。。

UWP 至少算是 平滑迁移 改动比较下。。。 顺便熟悉下 Webview2 反正 最终还得用这。

PS. fildder 那个啊? fork 了 原作者的 然后 顺带多研究了下。(不过原作者似乎不更了
glionying 发表于 2023-5-29 15:16
pjy612 发表于 2023-5-29 13:04
BTW 是啥?

本来是计划迁移到 MAUI 但是 MAUI 的 基础控件 支持太差了。。。 得再观望下。。。

BTW = By the way
Net控件做的好的有Devexpress, Telerik, Syncfusion
它们都有net各平台的常用的控件,包括MAUI,可以关注下
 楼主| pjy612 发表于 2023-5-29 15:41
glionying 发表于 2023-5-29 15:16
BTW = By the way
Net控件做的好的有Devexpress, Telerik, Syncfusion
它们都有net各平台的常用的控件 ...

毕竟是公司项目,商用组件 不能随便用。
另外迁移还要保证风格统一
Xamarin 迁移到 UWP 多少还统一点。
直接迁移 MAUI 很多东西 没有基础支持 可能还得自己画控件。

所以 前端同事 觉得 短期内还是 迁移到 UWP 比较快。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 20:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表