Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

WebView2 in Windows Apps 2021

20.01.2021 (👁16944)


 

Das neue Browser Control für Windows Apps heißt WebView2 und basiert auf der Google Chrome ( Chromium, in Microsoft als Canary ) Browser API.

Das Control löst das alte WebBrowser in WinForms und WebView in WPF und UWP ab und kommt schon als Preview in 2020 zum Einsatz und in 2021 als vollwertige Version.

 

Einsatz:

Mit dem WebView2 kann man in einer Windows App HTML und Javascript basierte HTML Webseiten lokal oder via Web URL anzeigen und sozusagen in eine App einbinden.

Dadurch kann man Web-Anwendung automatisch testen und auch Webseiten automatisiert auswerten und Daten sammeln wie Webscraping.

 

 

 

Betrifft:

UWP WinUI3 WPF WinForms

 

Vorraussetzung:

Für die Verwendung unter Windows 10 Visual Studion SDK 2019 muss über Microsoft Edge Insider die neue Chromium basierte Browser Schnittstelle installiert werden.

 

 

Installation Download:

 

Microsoft Edge Insider Channels

https://www.microsoftedgeinsider.com/en-us/download

Beta Channel

Major update every 6 weeks

 

The Beta channel is the most stable Microsoft Edge preview experience. With major updates every 6 weeks, each release incorporates learnings and improvements from our Dev and Canary builds.

 

 

 

Beispiel Anwendung in Universal Windows App (gleich wie WPF mit WinUI3)

 

Beispiel Code / UWP Universal Windows

MainPage.xaml

 

<Page

    x:Class="Uwp_webControl.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:Uwp_webControl"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*" />

            <ColumnDefinition Width="Auto" />

        </Grid.ColumnDefinitions>

        <TextBox Name="addressBar" Grid.Column="0"/>

        <Button x:Name="myButton" Grid.Column="1" Click="myButton_Click">Go</Button>

       

        <WebView2 x:Name="MyWebView"  Grid.Row="1" Grid.ColumnSpan="2"

        Source="https://Google.com" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />

    </Grid>

</Page>

 

 

 

MainPage.xaml.cs Code Behind in C#

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Microsoft.UI.Xaml;

using Microsoft.UI.Xaml.Controls;

using Microsoft.Web.WebView2.Core;

namespace Uwp_webControl

{

    /// <summary>

    /// An empty page that can be used on its own or navigated to within a Frame.

    /// </summary>

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        #region ====< Buttons >=====

        private void myButton_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                Uri targetUri = new Uri(addressBar.Text);

                MyWebView.Source = targetUri;

            }

            catch (FormatException ex)

            {

                // Incorrect address entered.

            }

        }

        #endregion ====</ Buttons >=====

    }

}