我怎么能通过一个idialoginterfaceon遣散listener没有我的应用程序崩溃



我的Xamarin有问题。Android应用程序关于IDialogInterfaceOnDismissListener .

My repro sample:

public enum ConfirmationResult
{
    Cancel,
    Yes,
    No
}
public interface IDialogService
{
    Task<ConfirmationResult> ConfirmAsync(string title, string message, string acceptText, string declineText);
}
public class DialogService : IDialogService
{
    class DismissDelegateListener : IDialogInterfaceOnDismissListener, IDialogInterfaceOnCancelListener
    {
        private Action _callback;
        public DismissDelegateListener()
        {
        }
        public DismissDelegateListener(Action callback)
        {
            _callback = callback;
        }
        public void Dispose()
        {
            _callback = null;
        }
        public IntPtr Handle { get; }
        public void OnCancel(IDialogInterface dialog)
        {
            _callback?.Invoke();
        }
        public void OnDismiss(IDialogInterface dialog)
        {
            _callback?.Invoke();
        }
    }
    private readonly WeakReference<Context> _activity;
    public DialogService(Context activity)
    {
        _activity = new WeakReference<Context>(activity);
    }
    public async Task<ConfirmationResult> ConfirmAsync(string title, string message, string acceptText, string declineText)
    {
        Context target;
        if (!_activity.TryGetTarget(out target))
            return ConfirmationResult.Cancel;
        TaskCompletionSource<ConfirmationResult> tcs = new TaskCompletionSource<ConfirmationResult>();
        var builder = new AlertDialog.Builder(target);
        var d = builder
            .SetTitle(title)
            .SetMessage(message)
            .SetPositiveButton(acceptText, (sender, args) =>
            {
                tcs.SetResult(ConfirmationResult.Yes);
                (sender as AlertDialog)?.Dispose();
            })
            .SetNegativeButton(declineText, (sender, args) =>
            {
                tcs.SetResult(ConfirmationResult.No);
                (sender as AlertDialog)?.Dispose();
            })
            // if this line is uncommented my application crashes once the dialog is opened
//              .SetOnCancelListener(new DismissDelegateListener(() => { tcs.SetResult(ConfirmationResult.Cancel); }))
            .Create();
        d.Show();
        return await tcs.Task;
    }
}

一旦我调用SetOnCancelListener我的应用程序将崩溃与错误信息System.InvalidCastException: Specified cast is not valid.

对我来说,一切似乎都是正确的-我正在传递所请求接口的实现,我的实现也不是那么困难。

有人对这个问题有印象吗?

从Java.Lang.Object派生是解决方案。

        class DismissDelegateListener : Java.Lang.Object, IDialogInterfaceOnDismissListener, IDialogInterfaceOnCancelListener
        //class DismissDelegateListener : IDialogInterfaceOnDismissListener, IDialogInterfaceOnCancelListener
        {
            private Action _callback;
            public DismissDelegateListener()
            {
            }
            public DismissDelegateListener(Action callback)
            {
                _callback = callback;
            }
            protected override void Dispose(bool disposing)
            {
                _callback = null;
                base.Dispose(disposing);
            }
            public void OnCancel(IDialogInterface dialog)
            {
                _callback?.Invoke();
            }
            public void OnDismiss(IDialogInterface dialog)
            {
                _callback?.Invoke();
            }
        }

相关内容

最新更新