【问题标题】:Binding property "fromICAO" not found on "ViewModel" in collectionview在 collectionview 的“ViewModel”上找不到绑定属性“fromICAO”
【发布时间】:2021-12-17 05:13:26
【问题描述】:

我正在尝试使用 Xamarin 中的集合视图显示数据,但其中一个标签中的文本绑定无法在视图模型中找到该属性

我的看法:

<CollectionView x:Name="ItemsCollectionView"
                                 ItemsSource="{Binding Plans}"
                                 SelectionMode="None">
                     <CollectionView.ItemTemplate>
                         <DataTemplate>
                             <Frame HasShadow="True">
                                 <StackLayout>
                                     <Grid>
                                         <Grid.RowDefinitions>
                                             <RowDefinition Height="60"/>
                                         </Grid.RowDefinitions>
                                         <Grid.ColumnDefinitions>
                                             <ColumnDefinition/>
                                             <ColumnDefinition/>
                                         </Grid.ColumnDefinitions>
                                         <StackLayout Spacing="0">
                                             <Label Text="From:" 
                                                    FontSize="10"
                                                    TextColor="Black"></Label>
                                             <Label Text="{Binding fromICAO}" 
                                                    FontSize="24"
                                                    TextColor="Black"></Label>
                                             <Label Text="To:" 
                                                    FontSize="10"
                                                    TextColor="Black"></Label>
                                             <Label Text="{Binding toICAO}" 
                                                    FontSize="24"
                                                    TextColor="Black"></Label>
                                         </StackLayout>
                                         <StackLayout  Spacing="0"
                                                       Grid.Column="1">
                                             <Label Text="Distance:" 
                                                    FontSize="10"
                                                    TextColor="Black"></Label>
                                             <Label Text="{Binding distance}" 
                                                    FontSize="24"
                                                    TextColor="Black"></Label>
                                             <Label Text="Fuel used:" 
                                                    FontSize="10"
                                                    TextColor="Black"></Label>
                                             <Label Text="{Binding fuelUsed}" 
                                                    FontSize="24"
                                                    TextColor="Black"></Label>
                                         </StackLayout>
                                     </Grid>
                                 </StackLayout>
                             </Frame>
                         </DataTemplate>
                     </CollectionView.ItemTemplate>
                 </CollectionView>

我的视图模型:

public ObservableCollection<PlanToFirestoreModel> Plans { get; set; }
    
         public PlansViewModel() {
             firestoreService = DependencyService.Get<IFirestoreService>();
             auth = DependencyService.Get<IFirebaseAuthentication>();
             Plans = new ObservableCollection<PlanToFirestoreModel>();
    
             FillData();
         }
    
         public async void FillData()
         {
             var tempPlans = await firestoreService.GetFlightPlansByUid(auth.GetUserInfoAsync().Id);
             foreach (var plan in tempPlans)
             {
                 Plans.Add(plan);
             }
         }

还有我的模型本身:

public class PlanToFirestoreModel : BasePlanModel
     {
         public string fromICAO { get; set; }
         public string toICAO { get; set; }
         public string fromName { get; set; }
         public string toName { get; set; }
         public int id { get; set; }
         public string userId { get; set; }
         public string aircraft { get; set; }
         public string deptCountry { get; set; }
         public string arrCountry { get; set; }
         public double fuelUsed { get; set; }
         public string flightNumber { get; set; }
         public double distance { get; set; }
         public string altitude { get; set; }
         public string createdAt { get; set; }
         public double deptLon { get; set; }
         public double deptLat { get; set; }
         public double arrLon { get; set; }
         public double arrLat { get; set; }
     }

XAML 页面可以找到 Plans ObservableCollection 本身,但不能找到应该在列表中的属性,例如“fromICAO”和“toICAO” 数据模板中的标签也只能找到计划,但不能找到其中的属性。 我做错了什么?

【问题讨论】:

  • 所有更新都发生在与 UI 线程不同的线程上,并且 UI 线程不知道更新的数据。
  • 好的,有没有办法可以访问这个线程?或者打电话给这个帖子?
  • 在你的问题的顶部,你暗示只有一个标签/属性被破坏,但最后你暗示它们都不起作用。是哪个?
  • 我的 IDE(Visual Studio)在错误列表中给了我 1 个损坏的标签,但是在查看代码时,它们似乎都没有找到绑定,所以我认为它们都是。
  • 您是否验证了Plans 实际包含数据并且为每个对象填充了这些属性?

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

通过使用Essentials Mainthread 强制数据更新它的工作,我可以解决属性。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2021-01-13
  • 2021-05-21
  • 2011-03-01
  • 2013-06-25
  • 2011-02-01
  • 2011-08-26
  • 2012-07-20
  • 1970-01-01
  • 2011-01-19
相关资源
最近更新 更多