繁体   English   中英

如何在没有 visual studio 的情况下在 windows 上运行 .NET MAUI?

[英]how to run .NET MAUI on windows without visual studio?

是否可以在命令提示符下运行 windows 上的 .net Maui 项目? 我尝试了简单的“do.net run”,但它说:

C:\Program Files\do.net\packs\Microsoft.Android.Sdk.Windows\32.0.476\tools\Xamarin.Android.Tooling.targets(69,5): error XA5300: The Android SDK directory could not be found. 检查 Android SDK Visual Studio 中的管理器是否显示有效安装。 要为命令行构建使用自定义 SDK 路径,请将“AndroidSdkDirectory”MSBuild 属性设置为自定义路径。 [C:\Users\Windows\Documents\csharp-scripts\MauiTest1\MauiTest1.csproj]

我不想在 android 上测试应用程序,而是在 windows 上测试

在我的测试中,我使用以下步骤在没有 Visual Studio 的情况下在 Windows 上使用 .NET MAUI,它工作正常。

步骤1。 https://do.net.microsoft.com/en-us/download 安装 .NET SDK

第2步。 使用 do.net CLI 安装 .NET MAUI 工作负载。 启动命令提示符并输入以下内容:

dotnet workload install maui

第三步。 使用 maui-check 命令行实用程序验证并安装缺少的组件。

dotnet tool install -g redth.net.MAUI.check
maui-check

第4步。 创建一个新文件夹和一个新的 MAUI 应用程序。

第五步。 启动您的 Android 模拟器。

第六步。 在 Android 模拟器中运行 MAUI 应用程序。

dotnet build -t:Run -f net6.0-android

根据使用 do.net build 而不是 msbuild.exe 的 Build Windows 目标,似乎使用 do.net 方式为 Windows 构建 .NET MAUI 应用程序不适用于 .NET MAUI。

对于 Windows,我发现编译和运行该应用程序的最佳方法是分 3 个步骤进行:

  1. msbuild编译
  2. 使用 Add-AppxPackage 安装应用程序
  3. 运行 package

我为pwsh 7 编写了以下脚本:

# Locate msbuild
$msbuild = "$((gci -Path (gci "$env:ProgramFiles\Microsoft Visual Studio\2*" )[-1])[-1].FullName)\MSBuild\Current\Bin\msbuild.exe"
new-alias msbuild $msbuild -Force

# Find sln
$sln = gci *.sln -Recurse -Depth 3 | Select -First 1
$baseDir = (gci -Path $sln.Directory *.csproj -Depth 1 | select -First 1).Directory

# 1. Build
msbuild $sln -r -p:Configuration=Debug -p:RestorePackages=false -p:TargetFramework=net6.0-windows10.0.19041

# 2. Find AppxManifest and Install it
$appManifest = Get-ChildItem -Path "$baseDir/bin" AppxManifest.xml -Depth 3 | Select-Object -First 1
import-module appx -usewindowspowershell
Add-AppxPackage $appManifest.FullName -Register

# 3. Get app guid to launch it
$guid = (gci *.csproj -Path $sln.Directory -Depth 1 | gc | Select-String -Pattern 'Guid>([^<]*)<').Matches.Groups[1].Value
$app = Get-AppxPackage -Name $guid;
explorer "shell:appsFolder\$($app.PackageFamilyName)!App"

编辑

如果你想包含 Images 和 Fonts 等资源,它们不会被自动复制,所以:

# Copy raw assets
gci "$baseDir/Resources/Raw" | cp -Destination $appManifest.Directory

# Copy built assets
gci -Path "$baseDir/obj" -Include resizetizer -Depth 3 | 
    gci -Exclude m | gci -Recurse -File | cp -Destination $appManifest.Directory

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM