C# 表单在外观方法上受保护-如何更改布尔值?

C# 表单在外观方法上受保护-如何更改布尔值?,c#,xamarin.forms,C#,Xamarin.forms,在我的Xamarin.Forms应用程序中的一个页面中,我想检测第一次调用OnAppearing方法的时间 我试图通过添加bool值来实现这一点,当页面初始化时,该值被设置为true,但当调用OnAppearing时,该值被设置为false 它在代码中的外观: public partial class ListPage : ContentPage { bool FirstLoad = true; public ListPage() { Initial

在我的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方法是受保护的,但是有什么方法可以处理这个问题吗

@迭戈,谢谢你的承诺

主页是我的主页

我在标签和图像中创建了一个网格,其中包含TapGestureRecognitors处理程序:

                <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>
此外,请在t手势识别器f.e-车辆页面中使用此代码:

        private void LblVehiclesPageTapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
        //Detail = new NavigationPage(new VehiclesPage());

        Detail = ((Page)Activator.CreateInstance(typeof(VehiclesPage)));
        IsPresented = false;
    }
我希望,我的所有页面都会在启动时上传。而且,当我通过母版页导航到此页面时,它将在内存中,然后我可以正确地使用OnAppearing方法

此外,我还尝试从类f.e初始化页面

    public class MasterPageItem
    {
         public string Title { get; set; }
         public string Icon { get; set; }
         public Type TargetType { get; set; }
    }

但是,我不知道如何正确地使用此属性。

如果您希望获得总体首次加载,可以将属性设置为
静态

private static bool firstLoad = true;

一旦它第一次被设置,在所有新创建的实例中,它将保持
false

你说的“激活”页面是什么意思?如果将此页面推到NavigationStack上,然后将其从NavigationStack中弹出,则该页面将与所有状态一起销毁(需要了解导致上述输出的导航类型)。@gannaway Ok。。。这可能是重点。我有MasterDetail页,Master是我的菜单页,Details页是f.e.列表页。要导航,我使用以下代码:Detail=newnavigationpage(newlistpage());IsPresented=假;好的,如果你每次都要创建一个新的列表页,那就可以解释了。@gannaway是对的。每次调用都会创建一个包含初始属性状态的新页面。也许您必须控制“首次加载”的点是在菜单项单击上,例如,为每个[page type]-[first load state]对保留一些键值对列表…@Diegrafaelsouza您想到了什么示例?我被困在这一点上了。我创建了MasterPageItem类,并在MainPage的构造函数中实例化它,如下所示:var page1=new MasterPageItem(){Title=“Item 1”,Icon=“itemIcon1.png”,TargetType=typeof(ListPage)};但当点击任何标签并导航到特定页面时,我不知道如何使用它。在我的情况下,它不起作用。在OnPearing方法中,我总是有假值。对不起,我切换了值。我现在更新了我的答案。该字段开头必须为
true
,您将在第一个runGreat:-)之后将其设置为
false
。如果解决了问题,请考虑将答案标记为解决方案:
private static bool firstLoad = true;