ios 当控件在.Net Maui中的显示器之外时,它们不会启用

q9yhzks0  于 2023-07-01  发布在  iOS
关注(0)|答案(1)|浏览(119)

我有一个页面,它有一个来自另一个页面的stacklayout控件作为子控件。第1页:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Page1"
             xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
             ios:Page.UseSafeArea="True"
             Title="{Binding }">

    <ContentPage.Content>
        <Grid RowDefinitions="50,*,55" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="Orange">
            <StackLayout Grid.Row="0" Orientation="Horizontal" BackgroundColor="GhostWhite" Padding="5" HeightRequest="50" IsVisible="True">
                <Button x:Name="" HorizontalOptions="FillAndExpand" Text="" Clicked="" BackgroundColor="LightBlue"/>
            </StackLayout>

            <StackLayout Grid.Row="1"  >
                <ScrollView VerticalOptions="FillAndExpand" Padding="10" IsVisible="True">
                    <StackLayout x:Name="stackLayoutCast" Padding="0,15,0,15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Transparent"/>
                </ScrollView>
            </StackLayout>
           

            <Button HeightRequest="40" BackgroundColor="Red" IsVisible="False"/>
            <Grid Grid.Row="2"  ColumnDefinitions="*,*,*" BackgroundColor="GhostWhite" Padding="5" HeightRequest="55" VerticalOptions="End">
                <Button Grid.Column="0" x:Name="" HorizontalOptions="FillAndExpand" Text="" Clicked="" BackgroundColor="Green" Margin="8,0,8,0"/>
                <Button Grid.Column="1" x:Name="" HorizontalOptions="FillAndExpand" Text="" Clicked="" BackgroundColor="Gray" Margin="8,0,8,0"/>
                <Button Grid.Column="2" x:Name="" HorizontalOptions="FillAndExpand" Text="" Clicked="" BackgroundColor="Red" Margin="8,0,8,0"/>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>

第二页:

<StackLayout xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="page 2"
             x:Name="me">

    <StackLayout.Resources>
        <ResourceDictionary>
            <Style TargetType="Grid">
                <Setter Property="BackgroundColor" Value="White"/>
                <Setter Property="Padding" Value="10"/>
                <Setter Property="RowSpacing" Value="14"/>
            </Style>
        </ResourceDictionary>
    </StackLayout.Resources>

    <Frame  x:Name="" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  Padding="1" BorderColor="Black" IsVisible="false">
        <VerticalStackLayout BackgroundColor="LightGray" Spacing="10">
            <Label x:Name="" Text="{Binding }" IsVisible="False"/>
            <CheckBox x:Name="" IsChecked="{Binding }" IsVisible="False"/>
            <CheckBox x:Name="" IsVisible="False"/>

            <Grid RowDefinitions="45, 45, 45" ColumnDefinitions="55, *" RowSpacing="7">

                <ImageButton  Grid.Row="0" Grid.Column="0" x:Name="" Clicked="" Source=""  Margin="0,0,10,0"/>
                <Frame Grid.Row="0"  Grid.Column="1"  Padding="5,0,5,0" HorizontalOptions="FillAndExpand">
                    <Entry x:Name="" Placeholder="" HorizontalOptions="FillAndExpand" IsReadOnly="True" TextChanged="">
                        <Entry.Text>
                            <MultiBinding StringFormat="{}{0}-{1}">
                                <Binding Path="" />
                                <Binding Path="" />
                            </MultiBinding>
                        </Entry.Text>
                    </Entry>
                </Frame>

                <ImageButton Grid.Row="1" Grid.Column="0"   x:Name="" Clicked="" Source="" Margin="0,0,10,0"/>
                <Frame Grid.Row="1" Grid.Column="1"  Padding="5,0,5,0" HorizontalOptions="Fill">
                    <Entry x:Name=""  Placeholder="" VerticalTextAlignment="Center" TextChanged="" Text="{Binding }" HorizontalOptions="FillAndExpand"/>
                </Frame>

              <Frame Grid.Row="2" Grid.ColumnSpan="2" Padding="5,0,5,0" HorizontalOptions="FillAndExpand">
                    <Entry x:Name="" Placeholder="" Text="{Binding }"/>
                </Frame>

            </Grid>

            <Grid  RowDefinitions="45,40,50"  ColumnDefinitions="45,*,*">

                <Label Grid.Row="0" Grid.ColumnSpan="2" Text="" HorizontalOptions="Start" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center"/>
                <Picker Grid.Row="0" Grid.Column="2" x:Name="" SelectedItem="{Binding }" HorizontalTextAlignment="Center" HorizontalOptions="EndAndExpand" Title="" VerticalTextAlignment="Center"/>

                <Label Grid.Row="1" Grid.ColumnSpan="2" Text="" VerticalTextAlignment="Center" FontSize=""/>
                <DatePicker Grid.Row="1" Grid.Column="2"  x:Name="" Date="{Binding }" HorizontalOptions="EndAndExpand"/>

                <ImageButton Grid.Row="2"  Grid.Column="0" x:Name="" Source=""  HorizontalOptions="End" Clicked=""/>
                <Frame Grid.Row="2" Grid.Column="1"  Padding="5,0,5,0" HorizontalOptions="FillAndExpand" >
                    <Entry x:Name="" Keyboard="Numeric"  Placeholder="" Text="{Binding }" HorizontalOptions="FillAndExpand"/>
                </Frame>
                <Frame Grid.Row="3" Grid.Column="2"  Padding="5,0,5,0" HorizontalOptions="FillAndExpand">
                    <Entry x:Name="" Keyboard="Numeric" Placeholder="" Text="{Binding }" HorizontalOptions="FillAndExpand"/>
                </Frame>

            </Grid>

            <Grid RowDefinitions="30, 30, 40, 40, 30, 40, 40,  25, *" ColumnDefinitions="*,*" Padding="5">

                <Label  Grid.Row="0" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch  Grid.Row="0" Grid.Column="1"   x:Name="" IsToggled="{Binding }" Toggled="Switch_Toggled"/>

                <Label Grid.Row="1" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch Grid.Row="1" Grid.Column="1"  x:Name="" IsToggled="{Binding }" Toggled="Switch_Toggled"/>

                <Label Grid.Row="2" Grid.Column="0" Text="" VerticalTextAlignment="Center" HorizontalTextAlignment="Start"/>
                <Entry Grid.Row="2" Grid.Column="1" x:Name="" Text="{Binding }" WidthRequest="80" Keyboard="Numeric" Placeholder="0.00" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="Center"/>

               <Label Grid.Row="3" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Picker Grid.Row="3" Grid.Column="1" x:Name="" SelectedItem="{Binding }"  WidthRequest="140" Title="" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="Center"/>

                <Label Grid.Row="4" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch Grid.Row="4" Grid.Column="1" x:Name="" IsToggled="{Binding }" Toggled="Switch_Toggled"/>

               <Label Grid.Row="5" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Picker Grid.Row="5" Grid.Column="1" x:Name="" SelectedItem="{Binding }" WidthRequest="140" Title="" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="Center"/>

                <Label Grid.Row="6" Grid.Column="0" Text="" VerticalTextAlignment="Center" HorizontalOptions="Start"/>
                <Entry Grid.Row="6" Grid.Column="1" x:Name=""  Text="{Binding }" WidthRequest="100" Placeholder="0.00" HorizontalTextAlignment="Center" Keyboard="Numeric" HorizontalOptions="EndAndExpand"/>

                <Label Grid.Row="7" Text="" VerticalTextAlignment="Center" HorizontalOptions="Start"/>

                <StackLayout Grid.Row="8"  Grid.Column="0" Grid.ColumnSpan="2" RadioButtonGroup.GroupName="">
                    <StackLayout Orientation="Horizontal"  Spacing="50">
                        <RadioButton x:Name="" GroupName="" Content="" FontSize="Body"  VerticalOptions="CenterAndExpand" IsChecked="True"/>
                        <RadioButton x:Name="" GroupName="" Content="" FontSize="Body"  VerticalOptions="CenterAndExpand"/>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal"  Spacing="61">
                        <RadioButton x:Name="" GroupName="" Content="" FontSize="Body"  VerticalOptions="CenterAndExpand"/>
                        <RadioButton x:Name="" GroupName="" Content="" FontSize="Body"  VerticalOptions="CenterAndExpand"/>
                    </StackLayout>

                </StackLayout>
                
            </Grid>

            <Grid RowDefinitions="40,40,40, 25, auto" ColumnDefinitions="*,60">

                <Label Grid.Row="0" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Entry Grid.Row="0" Grid.Column="1" x:Name="" Text="{Binding }" Placeholder="0" Keyboard="Numeric" WidthRequest="45" HorizontalTextAlignment="Center" HorizontalOptions="End"/>

                <!--<Line Grid.Row="1"/>-->

                <Label Grid.Row="1" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Entry Grid.Row="1" Grid.Column="1" x:Name="" Text="{Binding }" Placeholder="0" Keyboard="Numeric" WidthRequest="45"  HorizontalTextAlignment="Center" HorizontalOptions="EndAndExpand"/>

                <!--<Line Grid.Row="3"/>-->

                <Label Grid.Row="2" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Entry Grid.Row="2" Grid.Column="1" x:Name="" Text="{Binding }" Placeholder="0" Keyboard="Numeric" WidthRequest="45" HorizontalTextAlignment="Center" HorizontalOptions="EndAndExpand"/>

                <!--<Line Grid.Row="5"/>-->

                <Label Grid.Row="3"  Grid.Column="0" Text="" VerticalTextAlignment="Center" />
                <Switch  Grid.Row="3" Grid.Column="1"  x:Name="" IsToggled="{Binding }" HorizontalOptions="EndAndExpand"/>

                <CollectionView  Grid.Row="4" Grid.ColumnSpan="2" Grid.Column="0" x:Name="" IsVisible="{Binding Source={x:Reference }, Path=IsToggled}"  BackgroundColor="GhostWhite" Margin="0,10,0,0">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid Padding="5" Margin="10" RowDefinitions="40,40" ColumnDefinitions="*,*, 60"  BackgroundColor="White">
                                <Picker Grid.Row="0" Grid.Column="0" x:Name="" Title="Prodotto" ItemsSource="{Binding }" SelectedItem="{Binding }" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand"/>
                                <Picker Grid.Row="0" Grid.Column="1" x:Name="" Title="Frequenza" ItemsSource="{Binding }" SelectedItem="{Binding }" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand"/>

                                <Picker Grid.Row="1" Grid.Column="0" x:Name="" Title="" ItemsSource="{Binding }" SelectedItem="{Binding }" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand"/>
                                <Entry Grid.Row="1" Grid.Column="1" x:Name="" Text="{Binding }" HorizontalTextAlignment="Center" Placeholder="400" Keyboard="Numeric"/>
                                <Label Grid.Row="1" Grid.Column="2" Text="" TextColor="Black" VerticalTextAlignment="Center"/>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

                <Entry  x:Name="" IsVisible="false"/>

            </Grid>

            <StackLayout Padding="5" VerticalOptions="FillAndExpand">
                <Label Text=""/>
                <Frame BorderColor="Gray" VerticalOptions="FillAndExpand" Padding="8">
                    <Editor x:Name="" Text="{Binding }" VerticalOptions="FillAndExpand" Placeholder=""/>
                </Frame>
            </StackLayout>

            <Grid RowDefinitions= "25,25,25" ColumnDefinitions="*,*">

                <Label  Grid.Row="0" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch Grid.Row="0" Grid.Column="1" x:Name=""  IsToggled="{Binding }"  Toggled="Switch_Toggled" HorizontalOptions="EndAndExpand"/>

                <Label  Grid.Row="1" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch  Grid.Row="1" Grid.Column="1" x:Name=""  IsToggled="{Binding }" Toggled="Switch_Toggled"  HorizontalOptions="EndAndExpand"/>

                <Label Grid.Row="2" Grid.Column="0" Text="" VerticalTextAlignment="Center"/>
                <Switch Grid.Row="2" Grid.Column="1" x:Name=""  IsToggled="{Binding }" Toggled="Switch_Toggled"  HorizontalOptions="EndAndExpand"/>

            </Grid>

        </VerticalStackLayout>
    </Frame>

</StackLayout>

当用户进入Page1时,应用程序将整个Page2放入堆栈LayoutCast(Page1)中。

public Page2(object dClass, SQLiteAsyncConnection database)
{
            InitializeComponent();

            LoadComponents(dClass, database);
}

private async void LoadComponents(object dClass, SQLiteAsyncConnection database)
{
            try
            {
                
                stackLayoutCast.IsVisible = false;

                // Some work..

                stackLayoutCast.Children.Clear();
                stackLayoutCast.Children.Add(page2);
                
                stackLayoutCast.IsVisible = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
}

在Android中,它工作得很好,但在iOS中发生了一些奇怪的事情。当我加载页面时,屏幕外的任何组件都不会启用。如果我在加载页面时单击显示屏中的任何控件,它们都可以工作,但如果我向下滚动并尝试与其他组件交互,它们就不工作。
10次尝试中有8次失败。
有人知道为什么吗?
我知道我在里面放了很多控件,但我需要所有这些控件。

ttisahbt

ttisahbt1#

您必须将Page2类转换为可重用控件。这将保存更多的时间,并将在未来解决许多其他问题。
将整个 StackLayout 放在 ContentView 中。Page2的代码隐藏应该继承自 ContentView(而不是 ContentPage)。
有关控件的更多信息,请参见official documentation

相关问题