Xamarin Forms 的 MasterDetail 页面导航在安卓上导致崩溃 [Fatal signal 6 (SIGABRT), code -6],但在 iOS 和 UWP 上工作正常。

10

我有一个类似下面的主从关系:

public partial class LeaguesMDPage : MasterDetailPage
{
    public LeaguesMDPage()
    {
        InitializeComponent();
        Master = new LeaguesPage();
        Detail = new NavigationPage(new DivisionsPage(new League()));
    }
}

联赛页面(大师版)的设计具有以下列表视图:

    <ListView
    ItemsSource="{Binding Leagues}"
    SelectedItem="{Binding SelectedLeague, Mode=TwoWay}"
    IsPullToRefreshEnabled="True"
    RefreshCommand="{Binding UpdateLeagues}"
    IsRefreshing="{Binding IsBusy}"
    >

并且背后的代码是

public partial class LeaguesPage : ContentPage
{
    LeaguesViewModel vm;

    public LeaguesPage()
    {
        InitializeComponent();
        BindingContext = vm = new LeaguesViewModel(this);
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        vm.UpdateLeagues.Execute(false);   
    }
}
在LeaguesViewModel中,我通过设置SelectedLeague属性更新Detail页面并隐藏Master页面,代码如下:
League _SelectedLeague;

public League SelectedLeague
{
    get { return _SelectedLeague; }
    set
    {
        _SelectedLeague = value;
        OnPropertyChanged();
        if (_SelectedLeague != null)
        {
            Debug.WriteLine($"Navigating to DivisionsPage with LeagueID {_SelectedLeague.ID}");
            var mdp = (MasterDetailPage)App.Current.MainPage;
            Device.OnPlatform(                     
                    Android: () => { mdp.IsPresented = false; },
                    iOS: () => { mdp.IsPresented = false; },
                    WinPhone: () => { },
                    Default: () => { mdp.IsPresented = false; }
                );
            mdp.Detail = new NavigationPage(new DivisionsPage(_SelectedLeague));
            _SelectedLeague = null;
        }
    }
}

有时候这似乎能够按照我所希望的工作。它将导航到新的DivisionsPage并隐藏主页面。在iOS和UWP上似乎运行良好,但在Android上出现崩溃,具体情况如下:

[0:] Server Returned 34 divisions
[0:] Navigating to DivisionsPage with LeagueID 12
[0:] UpdatePoolRankings: Called GetDivisionsAsync
03-23 02:40:52.151 W/Mono    ( 6249): The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
03-23 02:40:52.151 D/Mono    ( 6249): Unloading image System.Core.dll [0x99db7700].
03-23 02:40:52.151 D/Mono    ( 6249): Image addref System.Core[0xaec169a0] -> System.Core.dll[0x9d166d00]: 11
03-23 02:40:52.151 D/Mono    ( 6249): Config attempting to parse: 'System.Core.dll.config'.
03-23 02:40:52.151 D/Mono    ( 6249): Config attempting to parse: '/Users/builder/data/lanes/4009/f3074d2c/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Core/System.Core.config'.
03-23 02:40:52.152 W/Mono    ( 6249): The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
03-23 02:40:52.152 D/Mono    ( 6249): Unloading image System.Core.dll [0x99db7700].
03-23 02:40:52.152 D/Mono    ( 6249): Image addref System.Core[0xaec169a0] -> System.Core.dll[0x9d166d00]: 12
03-23 02:40:52.152 D/Mono    ( 6249): Config attempting to parse: 'System.Core.dll.config'.
03-23 02:40:52.152 D/Mono    ( 6249): Config attempting to parse: '/Users/builder/data/lanes/4009/f3074d2c/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/System.Core/System.Core.config'.
03-23 02:40:52.383 D/Mono    ( 6249): [0x9b5bf930] hill climbing, change max number of threads 4
[0:] Server Returned 3 divisions
03-23 02:40:52.421 F/        ( 6249): * Assertion at /Users/builder/data/lanes/4009/f3074d2c/source/mono/mono/metadata/sgen-tarjan-bridge.c:1139, condition `xref_count == xref_index' not met
03-23 02:40:52.421 F/libc    ( 6249): Fatal signal 6 (SIGABRT), code -6 in tid 6249 (ClubApp.Droid)
InspectorDebugSession(21): Disposed
InspectorDebugSession(21): HandleTargetEvent: TargetExited

如果需要更多细节,请告诉我,谢谢!


我发现另一个人也遇到了这个问题 https://bugzilla.xamarin.com/show_bug.cgi?id=54120 我认为这是来自mono内部的 https://github.com/mono/mono/blob/master/mono/metadata/sgen-tarjan-bridge.c#L1140 - schnabs
我遇到完全相同的问题! - CCamilo
在我的情况下,我也使用了主从关系。在我的详情页面中,我有一个ListView,它导航到另一个具有ListView的页面。当我返回时,应用程序有时会崩溃并显示完全相同的消息。我正在使用Xamarin形式。 - CCamilo
@schnabs,你找到解决方案了吗? - Stephane Mathis
你们尝试设置菜单列表页面的标题了吗? - Ravindra Kumar
显示剩余2条评论
2个回答

0

我已将您的示例更新到最新版本2.3.4,并且在Android上运行时没有任何问题。我建议您删除MasterDetailPage中的代码并将其更新为以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ClubApp.Views;assembly=ClubApp"
             xmlns:model="clr-namespace:ClubApp.Models;assembly=ClubApp"
             x:Class="ClubApp.Views.MainMasterDetailPage" 
             Title="MD Page"
             IsPresented="True">
    <MasterDetailPage.Master>
        <local:LeaguesPage />
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <local:DivisionsPage>
                    <x:Arguments>
                        <model:League />
                    </x:Arguments>
                </local:DivisionsPage>
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

你可以使用Xamarin Forms来解决这个bug(我没有测试过你的解决方案)。这个bug也存在于一个(native) Xamarin Android应用程序中。你有针对这种情况的解决方案吗? - Stephane Mathis
您没有发布非Forms的可重现问题。如果您的问题与Forms应用程序有关,则根据您的示例,您只需要更新到最新的稳定版本即可。 - Dan Siegel
1
我不是原帖作者(但我开始了悬赏)。我只是有一个非Forms应用程序的相同问题,并且我认为您可能知道可以转换到我的情况下的潜在问题。 - Stephane Mathis
如果您发布一个可重现的示例,我可以查看一下。 - Dan Siegel
这是一个示例应用程序:https://drive.google.com/open?id=0B7De5DfDEi--QlJEN29xNjhDZGc。在来回操作17次后,它会崩溃(对于我的LG G4c是17次,但对于Galaxy S6是30次,但总数始终相同)。 - Stephane Mathis
显示剩余3条评论

0

我曾经遇到过类似的问题,后来发现是由于 NavigationPage 的 Android 渲染器中存在一个 bug 导致的。 可以查看一下这个 Xamarin 论坛上的帖子,里面有一个相关的 bug 报告和解决方法。该问题应该已经在当前的 XamForms 版本中得到了修复。

你可以尝试不使用 NavigationPage(直接将 DivisionsPage 设置为 Detail),以确定是否是 NavigationPage 导致了崩溃。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接