繁体   English   中英

如何从 Viewmodel 关闭 MauiCommunityToolkit 弹出窗口

[英]How to close the MauiCommunityToolkit Popup from Viewmodel

我想在我的 Viewmodel 中关闭一个 CommunityToolkit Popup。 我尝试使用WeakReferenceMessenger来接收这样的消息:

        public mypopup()
    {
        InitializeComponent();
        WeakReferenceMessenger.Default.Register<string, string>(this, "popup", (r, d) =>
        {
            Debug.WriteLine(message: "received message");
            if (d == "close")
            {
                WeakReferenceMessenger.Default.Unregister<string>(this);
                MainThread.BeginInvokeOnMainThread(() => { this.Close(); });
            }
        });
    }

在其他地方我用它来发送消息

WeakReferenceMessenger.Default.Send<string, string>("close", "popup");

第一次通话有效。 第二次它将在 MauiPopup.windows.cs 中引发System.NullReferenceException Function void CleanUp() Target.ContextFlyout = null;

我也在消息接收中这样尝试过:

MainThread.BeginInvokeOnMainThread(() => { this.Close(); });

同样的事情发生了。 我想知道是否有解决方案或更好的方法可以在不转移弹出窗口句柄的情况下从其他地方关闭弹出窗口。

我在我的代码中这样做了

        PopupPage p = new PopupPage();
        Application.Current.MainPage.ShowPopup(p);
        await new TaskFactory().StartNew(() => { Thread.Sleep(5000); });
        p.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM