【问题标题】:Listview is scrolling on iOS but not AndroidListview 在 iOS 上滚动,但不是在 Android 上滚动
【发布时间】:2020-03-19 09:31:40
【问题描述】:

在 iOS 上运行良好,但在 Android 上无法滚动。你能检查一下我做错了什么或任何可能导致这种情况的想法,或者检查什么? 我尝试添加滚动视图,添加 heightRequest,并将verticalOptions 更改为FillAndExpand,但似乎没有任何效果

<ContentView.Content>
        <AbsoluteLayout Margin="0,0,30,0">
            <Entry
                x:Name="searchBar"
                AbsoluteLayout.LayoutBounds="0,0,1,AutoSize"
                AbsoluteLayout.LayoutFlags="WidthProportional"
                BackgroundColor="White"
                HeightRequest="40"
                Placeholder="Enter sensor"
                TextChanged="SearchBar_OnTextChanged"
                TextColor="{DynamicResource RelogixDarkGray}"
                VerticalOptions="Center" />
            <ListView
                x:Name="dataListView"
                AbsoluteLayout.LayoutBounds="5,40,.98,.4"
                AbsoluteLayout.LayoutFlags="SizeProportional"
                BackgroundColor="White"
                CachingStrategy="RecycleElement"
                HasUnevenRows="True"
                IsVisible="False"
                ItemTapped="ListView_OnItemTapped">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <StackLayout Margin="5" Padding="0">
                                <Label
                                    BackgroundColor="White"
                                    FontFamily="{StaticResource NormalFont}"
                                    FontSize="16"
                                    Text="{Binding .}"
                                    TextColor="#FF464859"
                                    VerticalOptions="Center"
                                    VerticalTextAlignment="Center" />
                            </StackLayout>

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </AbsoluteLayout>

【问题讨论】:

    标签: xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    首先,我不知道TextColor="{DynamicResource RelogixDarkGray}"FontFamily="{StaticResource NormalFont}"是什么你用的,所以我删除它进行测试。

    那你在你的ListView中设置IsVisible="False",如果你设置为false,就看不到了,运行的时候我设置为True。

    而且您没有为您的listview 设置ItemsSource="{Binding myModels}"。你应该设置它,这是我的布局代码。

    <AbsoluteLayout Margin="0,0,30,0">
                    <Entry
                    x:Name="searchBar"
                    AbsoluteLayout.LayoutBounds="0,0,1,AutoSize"
                    AbsoluteLayout.LayoutFlags="WidthProportional"
                    BackgroundColor="White"
                    HeightRequest="40"
                    Placeholder="Enter sensor"
                    TextChanged="searchBar_TextChanged"
    
                    VerticalOptions="Center" />
                    <ListView
                        ItemsSource="{Binding myModels}"
                    x:Name="dataListView"
                    AbsoluteLayout.LayoutBounds="5,40,.98,.4"
                    AbsoluteLayout.LayoutFlags="SizeProportional"
                    BackgroundColor="White"
                    CachingStrategy="RecycleElement"
                    HasUnevenRows="True"
                    IsVisible="True"
                    ItemTapped="dataListView_ItemTapped">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
    
                                    <StackLayout Margin="5" Padding="0">
                                        <Label
                                        BackgroundColor="White"
    
                                        FontSize="16"
                                        Text="{Binding .}"
                                        TextColor="#FF464859"
                                        VerticalOptions="Center"
                                        VerticalTextAlignment="Center" />
                                    </StackLayout>
    
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </AbsoluteLayout>
    

    我注意到你使用了 MVVM,你应该像这样简单地在布局背景代码中添加BindingContext = new MyModelView();

      public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
    
                BindingContext = new MyModelView();
            } 
    }
    

    这里是MyModelView.cs

        public class MyModelView
        {
            public ObservableCollection<MyModel> myModels { get; set; }
            public MyModelView()
            {
    
                myModels = new ObservableCollection<MyModel>();
                myModels.Add(new MyModel() { Prop0="xxx",Prop1="xxx1",Prop2="xxxx2",Prop3=true,Prop4="xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
                myModels.Add(new MyModel() { Prop0 = "xxx", Prop1 = "xxx1", Prop2 = "xxxx2", Prop3 = true, Prop4 = "xxxx4" });
            }
        }
    

    这里是MyModel.cs

        public class MyModel
        {
            public string Prop0 { get; set; }
            public string Prop1 { get; set; }
            public string Prop2 { get; set; }
            public bool Prop3 { get; set; }
            public string Prop4 { get; set; }
        }
    

    这里正在运行 GIF。

    如果我为Label 设置绑定值

    <Label
                                        BackgroundColor="White"
    
                                        FontSize="16"
                                        Text="{Binding Prop0}"
                                        TextColor="#FF464859"
                                        VerticalOptions="Center"
                                        VerticalTextAlignment="Center" />
    

    这里正在运行 GIF。

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多