Microsoft Edge 浏览器开发控件WebView2 尝鲜_microsoft edge webview2

在这里插入图片描述

官方消息: WebView2 控件使用 Microsoft Edge (Chromium) 作为呈现引擎在应用中显示 Web 内容。 WebView2 作为 WINDOWS UI 库 3 (WinUI3) 的一部分提供。

利用WebView2,你可以方便的开发有关网页内容的混合界面。

在这里插入图片描述

2 优势和劣势

  • 基于web开发,有更好的生态,你懂得!
  • web开发允许你快速部署
  • 完整的本机功能支持
  • microsoft官方支持!!
  • 完美支持 windows7、8、10
  • 支持 WebDriver ,因此也支持爬虫、自动化测试等等各类场景。
  • 控件下载超快,相比Chrome,不需要梯子~~~
    劣势或不足
  • 目前暂不支持windows以外的平台

3. 开发平台

  • Win32 C/C++
  • WPF 【.Net FrameWork 4.5+】
  • WinForms 【.Net FrameWork 4.5+/.Net5】
  • WinUI3 【Net5】

开发完几乎windows全平台覆盖运行。

4. WebView2组件开心下载

用过Chrome开发的都知道,下载不易,且行且珍惜。
微软的WebView2,飞一般的体验。
在这里插入图片描述

5. WebUi 项目初体验

按照 说明,安装好webui环境。

  • Visual Studio 2019 版本 16.9 预览版 +
  • .NET 桌面开发 (安装程序还会安装 .NET 5)
  • 通用 Windows 平台开发

构建一个webui项目。
在这里插入图片描述

码上代码, 以下是界面配置:

<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d">
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="\*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="\*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <controls:WebView2 x:Name="MyWebView"  Grid.Row="1" Grid.ColumnSpan="2" 
        Source="https://passport.csdn.net/account/login" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <TextBox Name="addressBar" Grid.Column="0"/>
        <Button x:Name="myButton" Grid.Column="1" Click="myButton\_Click">Go</Button>
    </Grid>
</Window>


按钮事件:

private async void myButton\_Click(object sender, RoutedEventArgs e)
 {
       try
       {                
           Uri targetUri = new Uri(addressBar.Text);
           MyWebView.Source = targetUri;                
       }
       catch (FormatException ex)
       {
           // Incorrect address entered.
       }
}

6. 运行界面

由于下载插件分X86和X64,运行时需要匹配,如果有问题,可以切换下看看。

一个Edge内核的浏览器搞定了~~~
在这里插入图片描述

7 .执行Js

作为浏览器控件,必然需要支持js,这样可以方便干…事了。
嗯,以下是面向XXX编程必备小技巧,请妥善收藏。

document.querySelectorAll('.main-select > ul >li')[1].children[0].click()
document.querySelector('#all').value = '作者1'
document.querySelector('#password-number').value = '密码123'
document.querySelector('[data-type=account]').click()

把以上可以保存为js文件,然后我们执行它。

var dir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory;
 var file = Path.Combine(dir.ToString(), "test.js");
 try
 {
     var text = File.ReadAllText(file);
     //Uri targetUri = new Uri(addressBar.Text);
     //MyWebView.Source = targetUri;
    await MyWebView.CoreWebView2.ExecuteScriptAsync(text);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值