【问题标题】:xamarin.forms listview Items only update when scrolling even after iNotifyPropertychanged implementationxamarin.forms listview 项目仅在滚动时更新,即使在 iNotifyPropertychanged 实施之后也是如此
【发布时间】:2020-05-12 18:12:38
【问题描述】:

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

我的 Listview 项目来源设置

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>

【问题讨论】:

  • 如何获取位置数据?使用 Firebase 数据库?尝试在 if 语句之后设置 ListView Itemsource。根据我在 firebase 数据库中获取数据并最终设置 ItemSource 的测试,它对我来说效果很好。
  • @WendyZang-MSFT 兄弟。谢谢您的回复。我每次都可以从firebase获取数据。没问题。实际上,我从firebase获得了纬度和经度,我尝试将其转换为地址并将其绑定到“Item.Location”。问题是第一次数据会正确显示,如果我们重新加载列表,位置参数将不会显示。它只会在我们滚动时显示。当我放置调试器时,它每次都会显示数据。但不在列表视图上绘画
  • 知道了。我会对此进行测试,并尽快反馈。
  • @WendyZang-MSFT 谢谢兄弟...
  • NotifyPropertyChanged("Selected"); 应该是NotifyPropertyChanged("Location");

标签: firebase xamarin xamarin.forms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多