为什么在使用 xamarin Essentials 文件选择器时应用程序关闭?

问题描述

当我尝试使用 Xamarin.Essentials 的 FilePicker 类时,我的应用程序关闭时不会引发任何异常。

我做了什么:

补充: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

和代码:

async Task<FileResult> PickAndShow(PickOptions options)
{
    try
    {
        var result = await FilePicker.PickAsync();
        if (result != null)
        {
            Text = $"File Name: {result.FileName}";
            if (result.FileName.EndsWith("jpg",StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png",StringComparison.OrdinalIgnoreCase))
            {
                var stream = await result.OpenReadAsync();
                Image = ImageSource.FromStream(() => stream);
            }
        }
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }
}

我在 MacOS 上使用 VS。

关闭应用的视频: https://drive.google.com/file/d/1H1488amryi_p7mkg7JdmhcGAE569KON4/view?usp=sharing

请帮我看看我遗漏了什么?

编辑:

发生错误的行 var result = await FilePicker.PickAsync();

例外:

不抛出异常;

应用退出:

[Mono] 请求加载 Xamarin.Essentials.dll 的第 5 个(共 8 个)参考 [Mono] Loading reference 5 of Xamarin.Essentials.dll asmctx DEFAULT,寻找 System.Core,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e [Mono] 组装参考地址 Xamarin.Essentials[0x7ddc93de80] -> System.Core[0x7d850f7880]: 10 【编舞】跳过711帧!应用程序可能在其主线程上做了太多工作。 [OpenGLRenderer] 戴维!持续时间=12935ms;标志= 0,IntendedVsync = 24578200518089,垂直同步= 24591127790687,OldestInputEvent = 9223372036854775807,NewestInputEvent = 0,HandleInputStart = 24591131789208,AnimationStart = 24591131799939,PerformTraversalsStart = 24591132369131,DrawStart = 24591132532323,SyncQueued = 24591132572054,SyncStart = 24591133134246,IssueDrawCommandsStart = 24591133217554,SwapBuffers = 24591134886554,FrameCompleted=24591136679054,DequeueBufferDuration=219000,QueueBufferDuration=280000, [mali_winsys] EGLint new_window_surface(egl_winsys_display *,void *,EGLSurface,EGLConfig,egl_winsys_surface **,EGLBoolean) 返回 0x3000 [libEGL] EGLNativeWindowType 0x7de100a210 断开连接失败 [Mono] 请求 Xamarin.Forms.Platform.Android.dll 的加载参考 12(共 22 个) [Mono] Loading reference 12 of Xamarin.Forms.Platform.Android.dll asmctx DEFAULT,寻找 System.Runtime.Serialization,PublicKeyToken=7cec85d7bea7798e [Mono] 程序集引用地址 Xamarin.Forms.Platform.Android[0x7d852aa080] -> System.Runtime.Serialization[0x7d77336a00]: 3 [Mono] 请求 Xamarin.Forms.Platform.Android.dll 的加载参考 21(共 22 个) [Mono] Loading reference 21 of Xamarin.Forms.Platform.Android.dll asmctx DEFAULT,寻找 System.Xml,PublicKeyToken=7cec85d7bea7798e [Mono] 装配参考地址 Xamarin.Forms.Platform.Android[0x7d852aa080] -> System.Xml[0x7d7769b100]: 6 [libEGL] EGLNativeWindowType 0x7d5d049310 断开连接失败

一个设备日志:

Image Log

解决方法

感谢大家,并为给您带来的不便表示歉意。罪魁祸首是我的不良行为。没有例外,因为真的没有。问题是当您打开文件时应用程序在后台启动,在我的代码中我有:

    public static void Background()
    {
        activity.MoveTaskToBack(true);
    } 

我忘记了。在另一种情况下我仍然需要这个,所以我是这样解决的:

    public static void Background()
    {
        if (App.Current.importClicked) App.Current.importClicked = false;
        else activity.MoveTaskToBack(true);
    } 

再次道歉。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...