c# - Xamarin.Forms 受保护的 OnAppearing 方法 - 如何更改布尔值?
问题描述
在我想要检测的 Xamarin.Forms 应用程序的一个页面中,第一次调用 OnAppearing 方法时。
我尝试通过添加布尔值来做到这一点,当页面初始化时将其设置为 true,但是当调用 OnAppearing 时,该值设置为 false。
它在代码中的外观:
public partial class ListPage : ContentPage
{
bool FirstLoad = true;
public ListPage()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
base.OnAppearing();
Debug.WriteLine("***** OnAppearing ListPage");
if (FirstLoad)
{
//Do something
Debug.WriteLine("***** FirstLoad = true ");
}
else
{
//Do something else
Debug.WriteLine("***** FirstLoad = false ");
}
FirstLoad = false;
}
但它不起作用。总是,当我激活页面时,它会将布尔值返回为 true。
[0:] ***** OnAppearing ListPage
[0:] ***** FirstLoad = true
[0:] ***** OnAppearing ListPage
[0:] ***** FirstLoad = true
[0:] ***** OnAppearing ListPage
[0:] ***** FirstLoad = true
[0:] ***** OnAppearing ListPage
[0:] ***** FirstLoad = true
我认为问题在于, OnAppearing 方法受到保护,但是有什么办法可以处理这个问题吗?
@Diego,感谢您的承诺。
MainPage 是我的 MasterDetailPage。
我在具有 TapGestureRecognizers 处理程序的标签和图像中创建了一个网格:
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalOptions="Center" VerticalOptions="Center" Text="Menu" FontSize="Large" TextColor="White"/>
<!--<Frame BackgroundColor="Transparent" BorderColor="Black" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />-->
<Image Source="vehicles.png" Grid.Row="1" Grid.Column="0" HorizontalOptions="Center" WidthRequest="40" IsOpaque="True">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ImgVehiclesTapGestureRecognizer_Tapped"/>
</Image.GestureRecognizers>
</Image>
<Label x:Name="LblVehiclesPage" Grid.Row="1" Grid.Column="1" Text="Pojazdy" VerticalOptions="Center" FontSize="Medium" TextColor="White" BackgroundColor="Transparent">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="LblVehiclesPageTapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
<Image Source="list.png" Grid.Row="2" Grid.Column="0" HorizontalOptions="Center" WidthRequest="45" IsOpaque="True">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ImgListTapGestureRecognizer_Tapped"/>
</Image.GestureRecognizers>
</Image>
<Label x:Name="LblListPage" Grid.Row="2" Grid.Column="1" Text="Lista" VerticalOptions="Center" FontSize="Medium" TextColor="White" BackgroundColor="Transparent">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="LblListPageTapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
<Image Source="scheduler.png" Grid.Row="3" Grid.Column="0" HorizontalOptions="Center" WidthRequest="50" IsOpaque="True">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ImgSchedulerTapGestureRecognizer_Tapped"/>
</Image.GestureRecognizers>
</Image>
<Label x:Name="LblSchedulerPage" Grid.Row="3" Grid.Column="1" Text="Kalendarz" VerticalOptions="Center" FontSize="Medium" TextColor="White" BackgroundColor="Transparent">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="LblSchedulerPageTapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
<Image Source="gmaps.png" Grid.Row="4" Grid.Column="0" HorizontalOptions="Center" WidthRequest="40" IsOpaque="True">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ImgMapTapGestureRecognizer_Tapped"/>
</Image.GestureRecognizers>
</Image>
<Label x:Name="LblMapPage" Grid.Row="4" Grid.Column="1" Text="Mapa" VerticalOptions="Center" FontSize="Medium" TextColor="White" BackgroundColor="Transparent">
<Label.GestureRecognizers>
<TapGestureRecognizer
Tapped="LblMapPageTapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>
</Grid>
(我不想使用 ListView,因为启动时性能会更差)。
在 MainPage ctor 中,我将 ListPage 初始化为 Detail:
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(ListPage)));
此外,在 TapGestureRecognizers fe - VehiclesPage 中使用此代码:
private void LblVehiclesPageTapGestureRecognizer_Tapped(object sender, EventArgs e)
{
//Detail = new NavigationPage(new VehiclesPage());
Detail = ((Page)Activator.CreateInstance(typeof(VehiclesPage)));
IsPresented = false;
}
我希望,我所有的页面都会在启动时上传。而且,当我通过 MasterPage 导航到此页面时,它将在内存中,然后我可以正确使用 OnAppearing 方法。
另外,我试图从类初始化页面,fe
public class MasterPageItem
{
public string Title { get; set; }
public string Icon { get; set; }
public Type TargetType { get; set; }
}
但是,不知道如何正确使用它。
解决方案
如果要进行整体首次加载,可以将属性设置为static
:
private static bool firstLoad = true;
首次设置后,它将保留false
在所有新创建的实例中。
推荐阅读
- python - 当新的 Tkinter 窗口打开时,如何关闭现有的 Tkinter 窗口?
- excel - 在 Excel 中分析很慢
- python-3.x - 无法使用 boto3 在 Scaleway 上列出存储桶对象
- python - ValueError:无效的 API 调用。请参阅 Alpha_vantage 文档了解 TIME_SERIES_INTRADAY
- heap - AVL 树中“空”节点数量的 O 复杂度是多少?
- python - 芹菜工人 - 分离模式无法正确加载应用程序
- java - 如何使用 FirebaseUser 设置自定义声明?
- r - 无法将数据帧绑定到空数据帧
- node.js - nodejs,postgres,sequelize - 而不是获取文件名,而是尝试获取完整路径
- bash - youtube-dl 将所有播放列表视为空