C#+WinUI+AOT发布开发第一个WebView2应用程序

xingyun86 3小时前 144

C#+WinUI+AOT发布开发第一个WebView2应用程序

1.新建C# WinUI应用程序

2.修改配置文件,支持exe不打包运行

  <PropertyGroup>

    <OutputType>WinExe</OutputType>

    <TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>

    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>

    <RootNamespace>DoubanCollect</RootNamespace>

    <ApplicationManifest>app.manifest</ApplicationManifest>

    <Platforms>x86;x64;ARM64</Platforms>

    <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>

    <PublishProfile>win-$(Platform).pubxml</PublishProfile>

    <UseWinUI>true</UseWinUI>

    <WinUISDKReferences>false</WinUISDKReferences>

    <EnableMsixTooling>true</EnableMsixTooling>

    <Nullable>enable</Nullable>

    <WindowsPackageType>None</WindowsPackageType>//新增此行很关键

  </PropertyGroup>

3.编写UI代码

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="DoubanCollect.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DoubanCollect"
    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"
    Title="DoubanCollect">
    <Window.SystemBackdrop>
        <MicaBackdrop />
    </Window.SystemBackdrop>
    <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>
        <controls:WebView2 x:Name="MyWebView" Grid.Row="1" Grid.ColumnSpan="2" Source="https://www.microsoft.com" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>
</Window>

4.编写逻辑代码

    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyWebView.NavigationStarting += EnsureHttps;
        }
        private void EnsureHttps(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
        {
            String uri = args.Uri;
            if (!uri.StartsWith("https://"))
            {
                MyWebView.ExecuteScriptAsync($"alert('{uri} is not safe, try an https link')");
                args.Cancel = true;
            }
            else
            {
                addressBar.Text = uri;
            }
        }
        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Uri targetUri = new Uri(addressBar.Text);
                MyWebView.Source = targetUri;
            }
            catch (FormatException ex)
            {
                // 处理格式错误的地址
            }
        }
    }
}

5.编译运行

6.发布AOT配置

7.右键发布

8.发布成功

完成!

×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回