displayinfochanged 事件在.net maui 中仅随机触发

问题描述 投票:0回答:1
public MainPage()
{
    InitializeComponent();

    BindingContext = new CircleColorsViewModel();
    DeviceDisplay.Current.MainDisplayInfoChanged += Current_MainDisplayInfoChanged;
}
public void Current_MainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
    Shell.Current.DisplayAlert("title", $"Orient: {DeviceDisplay.Current.MainDisplayInfo.Orientation}", "ok");
}

我改变了方向,但上面的处理程序在某些情况下只是随机触发的。感觉很简单,但很多时候却什么也没做。我错过了什么?

谢谢你。

.net event-handling listener maui screen-orientation
1个回答
0
投票

可能是模拟器上的计时问题。在实际设备上进行测试。或者尝试:

Dispatcher.Dispatch( async () =>
{
    await Task.Delay(100);
    Shell.Current.DisplayAlert(...
}
  • Dispatch
    延迟动作直至旋转完成后。
  • Delay
    应确保在 DisplayAlert 之前以新方向重新绘制页面。

底线:我怀疑 DisplayAlert 尝试运行,但 Maui 的页面渲染器在旋转后尚未处于良好状态。也许警报会因为无法完成而被忽略(内部错误已被悄悄抑制)。

© www.soinside.com 2019 - 2024. All rights reserved.