Xamarin与MAUI中弹窗上层显示Prompt的问题排查
问题:CommunityToolkit.Maui环境下,地图Pin信息窗口点击后调用DisplayPromptAsync无弹窗
问题场景
在地图页面中,实现了地图点击添加Pin的功能:
void mapClicked(object sender, MapClickedEventArgs e) { var pin = new Pin { Label = "Here's where it is", Location = e.Location }; map.Pins.Add(pin); }
为Pin的信息窗口点击事件绑定了编辑标签的逻辑,调用DisplayPromptAsync让用户输入新标签:
pin.InfoWindowClicked += async (s, args) => { string pinName = ((Pin)s).Label; await DisplayPromptAsync("Enter new label", "enter new label"); };
实际运行时,点击Pin信息窗口后没有弹出输入提示框,尝试切换至主线程执行也无效果。
解决方法
问题出在调用DisplayPromptAsync的上下文上。直接调用时,当前上下文可能不是活跃的页面实例,导致弹窗无法正常触发。需要明确指定使用当前应用的主页面来调用弹窗方法:
修改后的代码如下:
pin.InfoWindowClicked += async (s, args) => { string pinName = ((Pin)s).Label; // 使用Application.Current.MainPage来调用DisplayPromptAsync var result = await Application.Current.MainPage.DisplayPromptAsync("Enter new label", "Enter new label", initialValue: pinName); if (!string.IsNullOrEmpty(result)) { ((Pin)s).Label = result; } };
这样就能正常弹出输入提示框,完成Pin标签的编辑操作。
内容的提问来源于stack exchange,提问作者Alex Kay
相关产品推荐
相关产品推荐

