xamarin - 将 DeviceDisplay.MainDisplayInfoChanged 事件添加到 XF 应用程序的 OnStart 是否有任何问题?
问题描述
我的应用程序有这个代码:
protected override void OnStart()
{
base.OnStart();
DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged
}
private void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)
{
SetDeviceInfo();
SetResourceDimensions();
}
我已经读过 Android 可以多次调用 OnStart。有人可以告诉我这是否会导致我在其中注册 OnMainDisplayInfoChanged 方法的代码出现问题。
解决方案
在您的情况下,因为该方法OnStart()
将被多次调用。该方法OnMainDisplayInfoChanged
可能会同时被多次调用。因为方法列表 MainDisplayInfoChanged
有重复的方法。
所以我们可以MainDisplayInfoChanged
在添加之前删除它。
bool IsFirstLoad = true;
protected override void OnStart()
{
base.OnStart();
if(!IsFirstLoad)
{
DeviceDisplay.MainDisplayInfoChanged -= OnMainDisplayInfoChanged
}
DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
IsFirstLoad = false ;
}
推荐阅读
- wordpress - 将 www 重定向到非 www 的所有页面
- javascript - 猫鼬 findByIdAndUpdate 更新对象并将其他对象推送到现有字段
- c# - c#向我的应用程序中的所有Windows添加新属性
- angular - 如何将 navCtrl 与从 LocalStorage 检索的类名一起使用
- django - Google-Oauth2 未在 Django 中注册新用户
- asp.net-web-api - 角 5; asp.net web api http.delete 不起作用
- java - 将base64屏幕截图添加到extendreport 3.1.5不成功
- php - 如何在php中将数据从一个数组附加到另一个数组
- r - 使用 R 聚合、排序和计算加权平均值
- java - 带有选项卡布局的 Android 键盘多图像类别