首页 > 解决方案 > Xamarin 将 ListView 绑定到静态资源可观察集合

问题描述

如何将 Itemsource 绑定到静态 Observable 集合?

Itemsource 将静态资源绑定到可观察集合

代码背后


namespace uitestmain
{
    class FoodListViewModel
    {

        public static ObservableCollection <string> Foods { get; set; }
        
        public string FoodName { get; set; }
        public string FoodFat { get; set; }
        public string FoodCarbs { get; set; }
        public string FoodProtein { get; set; }


        public FoodListViewModel()
        {
            Foods = new ObservableCollection<string>();
        
        }

Xaml 代码

<StackLayout Spacing="20" VerticalOptions="Start" Margin="10,160,0,0">
                <ListView x:Name="FoodItems" ItemsSource="{Binding Source=Foods}"  CachingStrategy="RecycleElement" HeightRequest="350">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Grid.Row="1" Orientation="Horizontal" Spacing="4">
                                    <Label Text="{Binding .}" TextColor="White"  FontSize="12" FontFamily="NexaLight.otf#NexaLight"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
        </Grid>

标签: listviewxamarinobservablecollection

解决方案


推荐阅读