首页 > 解决方案 > xamarin.forms listview 项目仅在滚动时更新,即使在 iNotifyPropertychanged 实施之后也是如此

问题描述

我有一个 xamarin.forms 应用程序,它有一个列表视图,显示从 firebase 获取的项目。我面临的问题是 SO 中提出的问题,即;我在列表视图中的一项(名为 location 的项目)仅在我们滚动列表视图时更新。我试图实施,iNotifyPropertychanged但我仍然面临同样的问题。我在这里做错了什么?任何帮助都会得到帮助。

我的 Listview Itemsource 设置

var person = await firebaseHelper.GetPerson(EmployeeID);// getting data from firebase
                    LocationData = person.userdata.Where(x => DateTime.Parse(x.DateTime).ToString("yyyy-MM-dd") == StartDate.Date.ToString("yyyy-MM-dd") && x.Longitude != "initial").ToList();
                    ObservableCollection<UserLocationData> dynamicLocation = new ObservableCollection<UserLocationData>(LocationData);
                    TrackingListView.ItemsSource = dynamicLocation;
                    if (dynamicLocation.Count != 0)
                    {
                        foreach (UserLocationData Item in dynamicLocation)
                        {
                            Item.DateTime = DateTime.Parse(Item.DateTime).ToString("MMMM dd,yyyy hh:mm tt");

                            if (!string.IsNullOrEmpty(Item.Latitude) && !string.IsNullOrEmpty(Item.Longitude))
                            {

                                var Locations = await Geocoding.GetPlacemarksAsync(Convert.ToDouble(Item.Latitude), Convert.ToDouble(Item.Longitude));
                                var placemark = Locations?.FirstOrDefault();
                                if (placemark.Thoroughfare.ToString() != null)
                                {
                                    var geocodeAddress =
                                    placemark.Thoroughfare + '\n' +
                                    placemark.SubAdminArea + '\n';
                                    Item.Location = geocodeAddress.ToString();                                  
                                }
                                else
                                {
                                    Item.Location = Item.Latitude + "," + Item.Longitude;

                                }
                            }

                        }

我的数据模型

 public class Person : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public string Name { get; set; }
        public string PersonId { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public string EmailID { get; set; }
        public string Password { get; set; }       
        public bool Status { get; set; }
        public bool IsAdmin { get; set; }
        public List<UserLocationData> userdata { get; set; }
    }



 public partial class UserLocationData : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string DateTime { get; set; }


        private string _location;
        public string Location
        {
            get
            {
                return _location;
            }

            set
            {
                if (value != null)
                {
                    _location = value;
                    NotifyPropertyChanged("Selected");
                }
            }
        }


        public bool ButtonOn { get; set; }
        public bool ButtonOff { get; set; }
    }

Firebase GetPerson 部分

   public async Task<Person> GetPerson(string personId)
        {
                var allPersons = await GetAllPersons();
                await firebase
                .Child("Persons")
                .OnceAsync<Person>();
                return allPersons.Where(a => a.PersonId == personId).FirstOrDefault(); 

        }

我的 XAML

 <ListView  x:Name="TrackingListView"  ItemsSource="{Binding} " 
                             HasUnevenRows="True" 
                             IsVisible="False"
                             CachingStrategy="RecycleElement"                                          
                             SeparatorVisibility="None"   
                             SelectionMode="None"
                             BackgroundColor="Transparent"  
                             Margin="10,10,10,10"
                             HorizontalOptions="FillAndExpand"    
                             >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>

                                        <Frame
                            Padding="5"
                            Margin="0,5,0,5"
                            Opacity="0.9"                           
                            BackgroundColor="White"
                            BorderColor="LightBlue"
                            HasShadow="True"                           
                            CornerRadius="10"                          
                            ClassId="{Binding DateTime}"
                            HorizontalOptions="FillAndExpand">

                                            <Frame.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
                                            </Frame.GestureRecognizers>
                                            <Grid Margin="0,10,0,0" >

                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto"/>
                                                    <RowDefinition Height="Auto"/>
                                                    <RowDefinition Height="Auto"/>
                                                </Grid.RowDefinitions>

                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="Auto"/>
                                                </Grid.ColumnDefinitions>

                                                <StackLayout HorizontalOptions="FillAndExpand"  Grid.Row="0" Grid.Column="0" Orientation="Horizontal">


                                                    <Image Source="clock.png" HorizontalOptions="Start" VerticalOptions="Start"
                                                       HeightRequest="20" Margin="10,0,5,0"                                                      
                                                       ></Image>

                                                    <StackLayout Orientation="Vertical" Margin="0" HorizontalOptions="FillAndExpand">

                                                        <Label Text="Location fetched time" FontSize="12"  HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                                                       TextColor="Blue" 

                                                       ></Label>

                                                        <Label Text="{Binding DateTime}"  HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                                                       TextColor="Black" FontSize="Small"

                                                       ></Label>

                                                    </StackLayout>

                                                </StackLayout>


                                                <StackLayout Grid.Column="1" Grid.Row="0" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand">
                                                    <Image Source="googlemap.png" HorizontalOptions="End" VerticalOptions="Center" HeightRequest="30" ></Image>

                                                </StackLayout>


                                                <StackLayout HorizontalOptions="FillAndExpand"  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">


                                                    <Image Source="currentlocation.png" HorizontalOptions="Start" VerticalOptions="Start"
                                                       HeightRequest="20" Margin="10,0,5,0"                                                      
                                                       ></Image>

                                                    <StackLayout Orientation="Vertical" Margin="0" HorizontalOptions="FillAndExpand">

                                                        <Label Text="Location " FontSize="12"  HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                                                       TextColor="Blue" 

                                                       ></Label>

                                                        <Label Text="{Binding Location}"  HorizontalOptions="StartAndExpand" VerticalOptions="Center"
                                                       TextColor="Black" FontSize="Small"

                                                       ></Label>

                                                    </StackLayout>

                                                </StackLayout>

                                                <StackLayout Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"  Margin="0" >
                                                    <Grid>
                                                        <StackLayout Orientation="Horizontal" Margin="0"   IsVisible="{Binding ButtonOn}">

                                                            <Image Source="start.png" HorizontalOptions="Start"
                                                                    HeightRequest="20" Margin="10,0,5,0"       
                                                                   VerticalOptions="Start">

                                                            </Image>

                                                            <Label Text="Location Tracking Started" VerticalOptions="Center" TextColor="Green" FontSize="12" ></Label>

                                                        </StackLayout>

                                                        <StackLayout Orientation="Horizontal" Margin="0"  IsVisible="{Binding ButtonOff}">

                                                            <Image Source="stop.png" HorizontalOptions="Start"
                                                                    HeightRequest="20" Margin="10,0,5,0"       
                                                                   VerticalOptions="Start">

                                                            </Image>

                                                            <Label Text="Location Tracking Stopped" VerticalOptions="Center" TextColor="Red" FontSize="12" ></Label>

                                                        </StackLayout>

                                                    </Grid>

                                                </StackLayout>


                                            </Grid>
                                        </Frame>

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

标签: firebasexamarinxamarin.forms

解决方案


NotifyPropertyChanged("Selected");它应该是NotifyPropertyChanged("Location");


推荐阅读