数察啜 发表于 2025-7-31 07:34:26

.NET 10 中的新增功能系列文章3—— .NET MAUI 中的新增功能


[*].NET 10 预览版 6 中的 .NET MAUI
[*].NET 10预览版 5中的.NET MAUI
[*].NET 10 预览版 4 中的 .NET MAUI
[*].NET 10 预览版 3 中的 .NET MAUI
[*].NET 10 预览版 2 中的 .NET MAUI
[*].NET 10 预览版 1 中的 .NET MAUI
一、MediaPicker 增强功能(预览版6)

.NET 10 预览版6 对 MediaPicker 进行了显著增强,新增了支持多文件选择和图像压缩的功能。开发者现在可以通过简单的 API 调用实现这些功能:
var result = await MediaPicker.PickMultipleAsync(new MediaPickerOptions
{
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>MaximumWidth = 1024,
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>MaximumHeight = 768
});这个改进使得处理媒体文件变得更加高效,特别是对于需要处理多张图片或控制图像尺寸的应用场景。
二、XAML 命名空间简化(预览版5)

.NET 10 预览版5 引入了一套全新的 XAML 命名空间处理机制,显著简化了 XAML 文件的头部声明。主要改进包括:

[*]项目范围的"全局"命名空间:http://schemas.microsoft.com/dotnet/maui/global 可以聚合多个 xmlns
[*]隐式默认命名空间(选择加入):编译器自动注入默认命名空间
对比示例:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>变化:

[*]无需声明 xmlns:models 或 xmlns:controls 因为它们在 GlobalXmlns.cs 文件中全局声明
[*]不需要前缀 TagView 或 Tag
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>要启用这一功能,开发者需要在项目中添加 GlobalXmlns.cs 文件并配置相关属性。
三、Web 请求拦截功能(预览版5和6)

.NET 10 为 BlazorWebView 和 HybridWebView 新增了 Web 请求拦截能力,为开发者提供了更大的灵活性:
webView.WebResourceRequested += (s, e) =>{<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>if (e.Uri.ToString().Contains("api/secure"))<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>e.Handled = true;<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>e.SetResponse(200, "OK", "application/json", GetCustomStream());<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>}};此功能支持多种场景,包括修改请求头、重定向请求或提供本地响应等。
四、.NET Aspire 集成

.NET 10 为 .NET MAUI 新增了与 .NET Aspire 的深度集成模板,简化了遥测和服务发现的配置:
builder.AddServiceDefaults();该方法自动完成:

[*]OpenTelemetry 指标和追踪配置
[*]服务发现功能添加
[*]HttpClient 与服务发现的集成配置
这为构建现代云原生应用提供了更便捷的路径。
五、动画 API 改进

.NET 10 对动画 API 进行了现代化改造,将原有的同步方法替换为异步版本:
旧方法新方法FadeToFadeToAsyncRotateToRotateToAsyncScaleToScaleToAsync这种改变不仅更符合现代异步编程模式,还能避免潜在的 UI 线程阻塞问题。
详细信息:基础动画。
六、控件增强和弃用

CollectionView 和 CarouselView

.NET 9 中的 .NET MAUI 在 iOS 和 Mac Catalyst 上包含了两个可选的处理程序,为 CollectionView 和 CarouselView带来了性能和稳定性改进。 在 .NET 10 中,这些是 CollectionView 和 CarouselView的默认处理程序。
编辑和输入 (Android)

在安卓平台上,Editor 和 Entry 视图将它们的原生视图从 AppCompatEditText 更改为 MauiAppCompatEditText,这增加了对 SelectionChanged 事件的支持。
HybridWebView

HybridWebView 获得一个 InvokeJavaScriptAsync 重载,该重载调用指定的 JavaScript 方法,但不包括返回类型的任何信息。 有关详细信息,请参阅 调用不返回值的 JavaScript 方法。
默认情况下,由您的JavaScript代码抛出的任何异常都会发送到.NET,然后在.NET中重新抛出为.NET异常。
列表视图

ListView 已被弃用,和 EntryCell、ImageCell、SwitchCell、TextCell、以及 ViewCell 一样。 相反,应使用 CollectionView。
备注
Cell 尚未弃用,因为它目前用于源代码生成。 然而,它应该被视为已弃用。
搜索栏

SearchBar 获得了一个 SearchIconColor 可绑定属性,用于设置搜索图标的颜色:
SearchBar 还获得一个可绑定的 ReturnType 属性,类型为 ReturnType,用于指定返回按钮的外观。 此属性的默认值为 Search。
有关详细信息,请参阅 SearchBar。
开关

Switch 拥有一个 OffColor 可绑定属性,用于设置开关在关闭状态时的颜色。
有关详细信息,请参阅 开关。
表格视图

TableView 已弃用。 相反,应使用 CollectionView。
Android上的WebView

当视频托管在 Android 的WebView中时,现在可以通过在allowfullscreen中包含iframe来全屏播放。
七、平台特定功能增强


[*]iOS/Mac Catalyst 模态页面:现在可以显示为弹出式页面
适用于 .NET 10 的 .NET MAUI 添加了一个特定于平台的功能,可以在 iOS 和 Mac Catalyst 上将模态页面显示为弹出式页面。 通过将Page.ModalPopoverSourceView可绑定属性设置为定义模态来源的View,将Page.ModalPopoverRect可绑定属性设置为定义弹出框将从其发起的视图内矩形的Rectangle,并将Page.ModalPresentationStyle可绑定属性设置为Popover,来使用它。
using Microsoft.Maui.Controls.PlatformConfiguration;using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;public partial class PopoverPage : ContentPage{<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>public PopoverPage(View modal, Rectangle rectangle)<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>{<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>InitializeComponent();<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>On().SetModalPopoverView(modal);<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>On().SetModalPopoverRect(rectangle);<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:models="clr-namespace:MyApp.Models"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>xmlns:controls="clr-namespace:MyApp.Controls"
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><controls:TagView x:DataType="models:Tag" />
</ContentPage>


<ContentPage x:>
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage><TagView x:DataType="Tag" />
</ContentPage>On().SetModalPresentationStyle(UIModalPresentationStyle.Popover);<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>}}使用 Navigation.PushModalAsync 方法导航到模式页
Page modalPage = new PopoverPage(originButton, Rectangle.Empty);
await Navigation.PushModalAsync(modalPage);
[*]地理位置:新增 IsEnabled 属性检查位置服务状态
[*]文本转语音:SpeechOptions 新增 Rate 属性控制语速
[*]Web 认证:AuthenticateAsync 支持 CancellationToken
这些平台特定的增强使得应用能够更好地利用各平台的原生能力^参考内容:"平台功能"^。
八、Android 开发改进

.NET 10 为 Android 开发带来多项重要改进:

[*]API 36 和 JDK 21 支持:默认使用 Android 16 (API 36)
[*]dotnet run 支持:简化调试流程
// Run on the only attached Android physical device
dotnet run -p:AdbTarget=-d

// Run on the only running Android emulator
dotnet run -p:AdbTarget=-e

// Run on the specified Android physical device or emulator
dotnet run -p:AdbTarget="-s emulator-5554"
[*]编组方法默认启用:
在 .NET 9 中,引入了一种新的方式来创建 Java 代码调用 C# 代码所需的封送方法,提高了启动性能。 但是,在 .NET 9 中,它们默认处于关闭状态。
在 .NET 10 中,默认启用它们。 这些封送方法的问题通常表现为启动时挂起。 如果您在 .NET 10 预览版中遇到启动时的卡顿问题,而在 .NET 9 上没有,请尝试在项目文件中将 $(AndroidEnableMarshalMethods) MSBuild 属性设置为 false 来禁用封送方法。
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>false
[*]构建优化:使用 System.IO.Compression 创建 APK,缩短构建时间
这些改进显著提升了 Android 开发的体验和效率。
九、iOS 开发改进

iOS 开发在 .NET 10 中也获得多项增强:

[*]修剪器增强:在更多配置中启用,默认显示警告
[*]原始资源处理:优化资源嵌入机制
[*]Windows 绑定构建:不再需要远程 Mac
[*]NSUrlSessionHandler 改进:更灵活的 TLS 配置
<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>true<ContentPage x:>
    <TagView x:DataType="Tag" />
</ContentPage>false系列文章

.NET 10 中的新增功能系列文章1——运行时中的新增功能
.NET 10 中的新增功能系列文章2——ASP.NET Core 中的新增功能

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: .NET 10 中的新增功能系列文章3—— .NET MAUI 中的新增功能