我想开始使用 .NET MAUI 进行编码,因此我将 VS2022 更新为“Version 17.1.0 Preview 6.0”。
然后我按照https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation中的建议使用“Visual Studio Installer”安装了这些功能
不幸的是,当我创建一个基本的 MAUI 应用程序时,我遇到了一些如下错误:
一个或多个目标框架不存在错误 NU1012 平台版本,即使它们已指定平台:net6.0-android
在开发人员命令行上,我尝试运行dotnet workload list
并得到
android
ios
maui-android
maui-maccatalyst
如果我尝试安装其他一些工作负载(例如 maui 或 maui-windows),我会收到一条错误消息
无法下载清单 microsoft.net.workload.emscripten:在 NuGet 源https://api.nuget.org/v3/index.json上未找到 microsoft.net.workload.emscripten.manifest-6.0.200 ,C: \Program Files (x86)\Microsoft SDKs\NuGetPackages"
我也尝试过完全删除并重新安装 VS2022,但没有任何运气。
然后我尝试运行,maui-check
但当它尝试下载建议:下载 .NET SDK (6.0.200)时,exe 无效并且任务失败。
最糟糕的部分:我尝试在另一台电脑上安装新的 VS2022 预览版,一切都完美运行...但我需要它在我的开发电脑上运行!
。
我使用这些资源来学习如何发布 MAUI 应用程序:
\nhttps://learn.microsoft.com/en-us/dotnet/maui/ios/deployment/overview
\nhttps://github.com/dotnet/maui/issues/4397
\nhttps://github.com/dotnet/sdk/issues/21877
\n基于这些,我尝试了不同的变体来发布,但它们都不起作用:
\ndotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 \n
Run Code Online (Sandbox Code Playgroud)\n\n\n错误:运行时标识符“ios-arm64”无效。
\n
dotnet publish -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 --no-restore\n
Run Code Online (Sandbox Code Playgroud)\n\n\n错误 NETSDK1032:RuntimeIdentifier 平台“ios-arm64”和\nPlatformTarget“x64”必须兼容。
\n
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true\n
Run Code Online (Sandbox Code Playgroud)\n\n\n错误:运行时标识符“ios-arm64”无效。
\n
dotnet build -f:net6.0-ios -c:Release /p:RuntimeIdentifier=ios-arm64 /p:BuildIpa=true --no-restore\n
Run Code Online (Sandbox Code Playgroud)\n\n\n错误 NETSDK1032:RuntimeIdentifier 平台“ios-arm64”和\nPlatformTarget“x64”必须兼容。
\n
更新
\n环境:
\nWindows 10 家庭版 - 21H2
\nVS 2022 版本 17.3.0 预览版 1.1
更新2
\n根据答案,我现在有了这个 csproj 文件:
我已在 .Net Maui 中声明了全局样式并尝试从其中一个页面访问它,但它抛出异常
Microsoft.Maui.Controls.Xaml.XamlParseException:位置 10:37。类型转换器失败:调用目标已引发异常。Microsoft.Maui.Controls.Xaml.XamlParseException:位置 8:34。未找到主键的 StaticResource。
应用程序.xaml代码
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp"
x:Class="MyApp.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style x:Key="redLabelStyle"
TargetType="Label">
<Setter Property="TextColor"
Value="Red"/>
<Setter Property="FontSize"
Value="Small"/>
<Setter Property="FontAttributes"
Value="Bold"/>
</Style>
<Style TargetType="Label">
<Setter Property="TextColor"
Value="Green"/>
<Setter Property="FontSize"
Value="Small"/>
<Setter Property="FontAttributes"
Value="Bold"/>
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
MainPage.xaml代码
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
NavigationPage.HasNavigationBar="False"
x:Class="MyApp.MainPage">
<VerticalStackLayout HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
<Label Style="{StaticResource redLabelStyle}"
Text="Global Style Red Label"/>
<Label …
Run Code Online (Sandbox Code Playgroud) 我刚刚在 Mac 上启动了一个全新的模板 .NET MAUI 项目,并且能够毫无问题地构建和运行启动项目。当我将任何图像添加到“Resources/Images”文件夹,然后尝试构建项目时,我收到错误:
错误描述: 名称“资源”已被保留,无法使用。
错误路径: Resources/Images/icon_notes.png
规格 Visual Studio for Mac 17.4 预览版(17.4 内部版本 2326)
我尝试过清理和重建项目,但这没有帮助。
重现步骤:
请帮助我了解这里出了什么问题以及如何解决它。
这是我的 csproj 文件
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>Notes</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Display name -->
<ApplicationTitle>Notes</ApplicationTitle>
<!-- App …
Run Code Online (Sandbox Code Playgroud) 我想做一个将图片保存到文件夹的下载器应用程序。该应用程序应该可以在 Windows 和 Macos 上运行,稍后可能会在 Android 和 iOS 上运行。
我还没有找到选择目标文件夹的方法。关于如何使用 blazor 或 xaml .NET MAUI 应用程序实现它有什么想法吗?
我在这里下载了最新的 MAUI 示例: https: //github.com/microsoft/dotnet-podcasts并复制了他们发出请求的确切方式,但我无法通过“意外的流结束错误”:
System.Net.WebException: unexpected end of stream on com.android.okhttp.Address@4c288121
---> Java.IO.IOException: unexpected end of stream on com.android.okhttp.Address@4c288121
Run Code Online (Sandbox Code Playgroud)
我HttpClient
通过工厂方法在 MauiProgram.cs 中初始化AddHttpClient
,如下所示(注意 Andorid 的不同 IP 地址):
public static string Base = DeviceInfo.Platform == DevicePlatform.Android ? "http://10.0.2.2" : "https://localhost";
public static string APIUrl = $"{Base}:7096/api/";
builder.Services.AddHttpClient<DishClient>(client => { client.BaseAddress = new Uri(APIUrl); });
Run Code Online (Sandbox Code Playgroud)
android:networkSecurityConfig
我已在 AndroidManifest.xml 中包含以下内容(允许 http 流量):
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
Xamarin 中的解决方案似乎是使用AndroidClientHandler
,但这是在一个已弃用的库中,我无法在 MAUI 中使用它,并且在有效的链接解决方案中没有引用它。 …
最近我更新到了最新版本的 .net MAUI,我有点惊讶地发现 MAUI 设备类现在已被标记为“过时”
我有多个用例,其中我将设备类用于某些用途。
例如,现在主线程方法已过时:
Device.BeginInvokeOnMainThread(() =>{});
Run Code Online (Sandbox Code Playgroud)
现在是否有一个新的助手类将执行这些活动,或者我错过了什么?
对于 .NET 7,System.Common.Drawing 不再在非 Windows 平台上使用。我喜欢将我的应用程序迁移到 Microsoft.Maui.Graphics(正如 Microsoft 在 .NET 6/7 的重大更改页面上建议的那样)。\n它们支持 Windows 上的 GDI+,这非常好,因为它可以最大限度地减少迁移的副作用。在 Linux 或 MacOS 上,我可以使用 SkiaImage 实现。
\n然而它\xe2\x80\x99s不清楚如何实现这一点。目前的文档非常有限。从源代码中我找到了IImageLoaderService
IImage 和几个实现。
根据平台,我会注册权利IImageLoaderService
,一切都应该正常。
if (RuntimeInformation.IsOsPlatform(OsPlatform.Windows) \n{ \n services.AddSingleton<IImageLoaderService, GDIImageLoaderService>()\n} \nelse \n{\nservices.AddSingleton<IImageLoaderService, SkiaImageLoaderService>() \n}\n
Run Code Online (Sandbox Code Playgroud)\n理论上到目前为止。但是,在引用Microsoft.Maui.Graphics
Nuget 后,仅IImageLoaderService
可用。
如何GDIImageLoaderService
在 Windows 上注册(或如何调用服务以使用 gdi+)?
我试图根据设置为浅色或深色主题的设备来更改 StatusBar 的颜色/样式,但运气不佳。
应用程序加载后,以下 XAML 即可运行,但在启动屏幕上不起作用:
<ContentPage.Behaviors>
<toolkit:StatusBarBehavior StatusBarColor="#F8F9FB" StatusBarStyle="DarkContent" />
</ContentPage.Behaviors>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,上面的 XAML 似乎不会在闪屏期间影响 StatusBar,因为它仍然显示默认的紫色和白色文本。知道如何在应用程序显示启动屏幕时更改状态栏吗?
最终目标是根据设置为浅色或深色主题的设备相应地设置状态栏颜色和图标颜色。例如,如果设备设置为使用深色主题,则显示启动画面和应用程序外壳时的状态栏应为带有浅色文本/图标的深色背景。当设备设置为使用浅色主题时,显示启动画面和应用程序外壳的状态栏应该是带有深色文本/图标的浅色背景。
我已经检查了许多与此相关的现有问题,但它们似乎都已过时,因为我正在使用 API 33。
这是我的 .xaml
<Picker x:Name="Title" SelectedItem="{Binding Title, Mode=TwoWay}" ItemsSource="{Binding Titles}" ItemDisplayBinding="{Binding Text}" Title="Title" />
<Entry x:Name="Name" Text="{Binding Name}" Placeholder="Name" />
Run Code Online (Sandbox Code Playgroud)
绑定到视图模型 .cs,如下所示
public class Person
{
public string Name { get; set; }
public string Title { get; set; }
List<SelectListItem> Titles = new(){
new SelectListItem { Text = "Mister", Value="Mr" }
new SelectListItem { Text = "Doctor", Value="Dr" }
...
}
}
Run Code Online (Sandbox Code Playgroud)
包含此数据
Person person = new() { Name = "Bill Jones", Title = "Mr" };
Run Code Online (Sandbox Code Playgroud)
所以选择器可以很好地显示列表。但我有两个问题。
Mr