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.NavigateToString(sURL)

20.01.2021 (👁7982)


 

Frage:

Wohin navigiert der neue WebBrowser Control in UWP WinUI3, wenn man die Methode .NavigateToString(String_URL) einträgt?

 

Antwort:

Das Browser Control WebView2 gibt dann nur den String aus. Dadurch kann man plain HTML anzeigen

 

 

//*.NavigateToString()

String sURL = addressBar.Text;

webView2.NavigateToString(sURL);

 

 

 

 

 

 

 

 

C# Code, WebView2 Universal Windows with WebView2.NavigateToString

 

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;

using Windows.UI.Popups;

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 async void btnBrowse_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                //----< Navigate Browser >----

                //Uri targetUri = new Uri(addressBar.Text);

                //webView2.Source = targetUri;

                //*.NavigateToString()

                String sURL = addressBar.Text;

                webView2.NavigateToString(sURL);

                //----</ Navigate Browser >----

            }

            catch (FormatException ex)

            {

                // Incorrect address entered.

                await new MessageDialog("Incorrect address entered.", "UR Address Error").ShowAsync(); ;

            }

        }

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

    }

}

 

 

 

 

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="btnBrowse" Grid.Column="1" Click="btnBrowse_Click">Go</Button>

       

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

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

    </Grid>

</Page>