在我要检测的Xamarin.Forms应用程序的一个页面中,第一次调用OnAppearing方法时。
我尝试通过添加bool值来实现此目的,该值在页面初始化时设置为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。
我在Lables和Images中创建了具有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 f.e-VehiclesPage中使用此代码:
private void LblVehiclesPageTapGestureRecognizer_Tapped(object sender, EventArgs e)
{
//Detail = new NavigationPage(new VehiclesPage());
Detail = ((Page)Activator.CreateInstance(typeof(VehiclesPage)));
IsPresented = false;
}
我希望我的所有页面都将在启动时上传。而且,当我通过MasterPage导航到此页面时,它将在内存中,然后可以正确使用OnAppearing方法。
此外,我尝试从类f.e
初始化页面 public class MasterPageItem
{
public string Title { get; set; }
public string Icon { get; set; }
public Type TargetType { get; set; }
}
但是,我不知道如何正确使用它。
答案 0 :(得分:0)
如果要首先进行总体加载,可以将属性设置为static
:
private static bool firstLoad = true;
首次设置后,它将在所有新创建的实例中保留false
。