C# WPF WebView2 ブラウザ App.xaml2022年08月24日 09:20

<Application x:Class="WpfWeb.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfWeb"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>



C# WPF WebView2 ブラウザ MainWindow.xaml2022年08月24日 09:22



<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfWeb"
        xmlns:Wpf="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" x:Class="WpfWeb.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="514*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Button x:Name="btnYahoo" Content="Yahoo" HorizontalAlignment="Left" Height="24" VerticalAlignment="Center" Width="45" Click="btnYahoo_Click" Margin="2,0,0,0"/>
        <Wpf:WebView2 x:Name="WViewMain" Grid.Row="1" Margin="0,0,0,2" NavigationCompleted="WViewMain_NavigationCompleted"/>
        <Button x:Name="btnGoogle" Content="Google" HorizontalAlignment="Left" Margin="52,0,0,0" VerticalAlignment="Center" Height="24" Width="50" Click="btnGoogle_Click"/>
        <Button x:Name="btnStop" Content="Stop" HorizontalAlignment="Left" Margin="106,0,0,0" VerticalAlignment="Center" Height="24" Width="34" Click="btnStop_Click"/>
        <Button x:Name="btnReload" Content="Reload" HorizontalAlignment="Left" Margin="144,0,0,0" VerticalAlignment="Center" Width="47" Height="24" Click="btnReload_Click"/>
        <TextBox x:Name="tbxURI" Margin="30,0,0,0" Grid.Row="2" TextWrapping="Wrap" Text="URI" VerticalAlignment="Center" Height="24"/>
        <Button x:Name="btnPrev" Content="Prev" HorizontalAlignment="Left" Height="24" Margin="195,0,0,0" VerticalAlignment="Center" Width="35" Click="btnPrev_Click"/>
        <Button x:Name="btnNext" Content="Next" HorizontalAlignment="Left" Height="24" Margin="235,0,0,0" VerticalAlignment="Center" Width="35" Click="btnNext_Click"/>
        <Button x:Name="btnGoURI" Content="Go" HorizontalAlignment="Left" Height="24" Margin="2,0,0,0" Grid.Row="2" VerticalAlignment="Center" Width="25" Click="btnGoURI_Click"/>
    </Grid>
</Window>



C# WPF WebView2 ブラウザ MainWindow.xaml.cs2022年08月24日 09:23

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfWeb
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnYahoo_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.Source = new Uri("https://www.yahoo.co.jp");
        }

        private void btnGoogle_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.Source = new Uri("https://www.google.co.jp");
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.Stop();
        }

        private void btnReload_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.Reload();
        }

        private void WViewMain_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
            this.Title =WViewMain.CoreWebView2.DocumentTitle;
            tbxURI.Text = WViewMain.CoreWebView2.Source;
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.GoBack();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.GoForward();
        }

        private void btnGoURI_Click(object sender, RoutedEventArgs e)
        {
            WViewMain.Source = new Uri(tbxURI.Text);
        }
    }
}



<< 2022/08
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

このブログについて

ネットで見つけたいろいろ雑記

バックナンバー

RSS