Xamarin Forms 关闭页面时不可见控件变为可见

Invisible Control becomes Visible when closing page Xamarin Forms

我有一个框架,IsVisible 绑定到我的模型视图中设置为 false 的 属性。当我更改 shell 中的弹出窗口时,框架会在 shell 弹出窗口关闭时出现。知道为什么会这样吗?

         // in my model view this is the property
         public bool ShowPanel{ get { return _showPanel; } set { _showPanel = 
         value; OnPropertyChange(); } }
                

         // in my view this is the panel

        <Frame IsVisible="{Binding ShowPanel}"  BackgroundColor="White" BorderColor="Purple"             AbsoluteLayout.LayoutFlags="PositionProportional" WidthRequest="100"  HeightRequest="200" AbsoluteLayout.LayoutBounds="0,0,200,250">
            <local:ExtendedListView  ItemTapped="ExtendedListView_ItemTapped"  ItemsSource="{Binding AllItems}" IsVisible="{Binding ShowPanel}" SelectionMode="None" HorizontalScrollBarVisibility="Always" HorizontalOptions="Fill" VerticalOptions="Fill"  BackgroundColor="Transparent">
                <local:ExtendedListView.ItemTemplate >
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="20" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10*"></ColumnDefinition>
                                    <ColumnDefinition Width="10*"></ColumnDefinition>
                                    <ColumnDefinition Width="20*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Image Grid.Column="0" Image.Source="{Binding ImageUrl}"  HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  WidthRequest="50" HeightRequest="50"  x:DataType="local1:ServiceLayers"></Image>
                                <CheckBox Grid.Column="1" IsChecked="{Binding IsVisible, Mode=TwoWay}" Color="Purple" VerticalOptions="Center" HorizontalOptions="Center" x:DataType="local1:ServiceLayers"></CheckBox>
                                <Label Padding="-5,0,0,0"  HorizontalOptions="StartAndExpand" FontAttributes="Bold" TextColor="Black" Grid.Row="0" Grid.Column="2"  HorizontalTextAlignment="Start" Text="{Binding Name}" x:DataType="local1:Service"></Label>


                                <Label></Label>
                            </Grid>

                        </ViewCell>

                    </DataTemplate>
                </local:ExtendedListView.ItemTemplate>
            </local:ExtendedListView>
        </Frame>


       // In the View Class 
       protected override bool OnBackButtonPressed() {

        await Shell.Current.GoToAsync($"//{nameof(MainPage)}");
        return true;
    }
              

如果当您离开页面时 BindingContext 为 removed/changed,则框架将显示为绑定无效并由绑定引擎更新。您有两个选择:

  1. 不要在导航生命周期中手动清除页面中的 BindingContext,GC 应该自行清理这些资源。

  2. 使用 binding fallbacks 定义无法解析绑定源(因为您已将 BindingContext 设为空)或绑定源已解析但绑定 属性 为空。