簡體   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