.NET MAUI应用仅Release模式崩溃(含SyncFusion组件)
.NET MAUI应用Release模式下点击SfScheduler跳转页面崩溃
问题核心
- 跨Android、iOS的.NET MAUI应用仅在Release模式下崩溃,模拟器与真机均出现该问题
- 崩溃触发场景:点击SyncFusion SfScheduler的日程或单元格,跳转至
AppointmentView页面(新建/编辑日程)时 - 此前Release模式该功能正常运行,Debug模式下无任何异常,仅存在无关警告
- 已尝试新建项目迁移代码、重置Mac等操作,问题未解决
相关代码片段
Scheduler页面触发跳转的XAML
<syncfusion:SfScheduler x:Name="Scheduler" AppointmentTapped="OnAppointmentTapped" CellTapped="OnCellTapped"> <!-- 其他Scheduler配置 --> </syncfusion:SfScheduler>
Scheduler页面后台跳转逻辑
private async void OnAppointmentTapped(object sender, AppointmentTappedEventArgs e) { var targetAppointment = e.Appointment as CustomAppointment; await Navigation.PushAsync(new AppointmentView(targetAppointment)); } private async void OnCellTapped(object sender, CellTappedEventArgs e) { await Navigation.PushAsync(new AppointmentView(e.Date)); }
AppointmentView页面QueryProperty定义
[QueryProperty(nameof(SelectedAppointment), "Appointment")] [QueryProperty(nameof(SelectedDate), "Date")] public partial class AppointmentView : ContentPage { public CustomAppointment SelectedAppointment { get => GetValue(SelectedAppointmentProperty) as CustomAppointment; set => SetValue(SelectedAppointmentProperty, value); } public static readonly BindableProperty SelectedAppointmentProperty = BindableProperty.Create(nameof(SelectedAppointment), typeof(CustomAppointment), typeof(AppointmentView)); public DateTime SelectedDate { get => (DateTime)GetValue(SelectedDateProperty); set => SetValue(SelectedDateProperty, value); } public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create(nameof(SelectedDate), typeof(DateTime), typeof(AppointmentView)); public AppointmentView() { InitializeComponent(); } public AppointmentView(CustomAppointment appointment) : this() { SelectedAppointment = appointment; } public AppointmentView(DateTime date) : this() { SelectedDate = date; } }
AppointmentView页面XAML
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="YourAppNamespace.AppointmentView"> <StackLayout Padding="20"> <Entry Placeholder="日程标题" Text="{Binding SelectedAppointment.Title}" /> <DatePicker Date="{Binding SelectedDate}" /> <!-- 其他日程编辑控件 --> </StackLayout> </ContentPage>
排查与解决建议
Release模式链接器优化问题
检查是否因Release模式下的代码收缩导致必要类型被移除,在项目.csproj中添加保留规则:<PropertyGroup Condition="'$(Configuration)' == 'Release'"> <MauiLinkerBehavior>LinkSdkOnly</MauiLinkerBehavior> <AssemblyToPreserve>Syncfusion.SfScheduler.Maui</AssemblyToPreserve> <AssemblyToPreserve>YourAppNamespace</AssemblyToPreserve> </PropertyGroup>QueryProperty与绑定优化问题
Release模式下绑定可能因优化失效,给相关类和属性添加[Preserve]特性:[Preserve(AllMembers = true)] [QueryProperty(nameof(SelectedAppointment), "Appointment")] [QueryProperty(nameof(SelectedDate), "Date")] public partial class AppointmentView : ContentPage { [Preserve] public CustomAppointment SelectedAppointment { get; set; } [Preserve] public DateTime SelectedDate { get; set; } }SyncFusion版本兼容性
若近期更新过SyncFusion NuGet包,尝试回退到之前能正常运行的版本,排查版本适配问题。收集崩溃日志定位根源
- Android:通过Logcat抓取Release模式下的崩溃堆栈
- iOS:通过Xcode的Organizer导出设备崩溃日志,查看具体异常类型和代码行
构造函数重载优化问题
Release模式下构造函数可能被优化,尝试统一使用无参构造函数,完全依赖QueryProperty传递参数,避免重载导致的实例化异常。
内容的提问来源于stack exchange,提问作者thejoesy
相关产品推荐
相关产品推荐

