[英]How to close the MauiCommunityToolkit Popup from Viewmodel
I want close a CommunityToolkit Popup in my Viewmodel.我想在我的 Viewmodel 中关闭一个 CommunityToolkit Popup。 I tried using a
WeakReferenceMessenger
to receive a message like this:我尝试使用
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(); });
}
});
}
And somewhere else I use this to send a message在其他地方我用它来发送消息
WeakReferenceMessenger.Default.Send<string, string>("close", "popup");
The 1st call works.第一次通话有效。 And the SECOND time it will raise a
System.NullReferenceException
in MauiPopup.windows.cs Function void CleanUp() Target.ContextFlyout = null;
第二次它将在 MauiPopup.windows.cs 中引发
System.NullReferenceException
Function void CleanUp() Target.ContextFlyout = null;
I also tried like this in message receive:我也在消息接收中这样尝试过:
MainThread.BeginInvokeOnMainThread(() => { this.Close(); });
the same happens.同样的事情发生了。 I wonder if there is a solution or a better way to close popup from somewhere else without transferring the handle of popup.
我想知道是否有解决方案或更好的方法可以在不转移弹出窗口句柄的情况下从其他地方关闭弹出窗口。
I did this in my code我在我的代码中这样做了
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.