简体   繁体   English

如何将 Label 绑定到 function 结果为 Xamarin.Z645024253191292AE688A8A

[英]How can you bind a Label to a function result in Xamarin.Forms

I'm trying to bind a Label to the result of the GetPlayCount() function call.我正在尝试将Label绑定到GetPlayCount() function 调用的结果。 The other bindings, for Name and Category , are working as expected, but there is no output for the third label NameCategory的其他绑定按预期工作,但第三个 label 没有 output

XAML: XAML:

<ListView ItemsSource="{Binding Games}"
                      HasUnevenRows="true" 
                      HeightRequest="200" 
                      SeparatorVisibility="Default">
        <ListView.ItemTemplate>
               <DataTemplate>
                   <ViewCell>
                       <ViewCell.View>
                           <Grid Margin="0" Padding="0" RowSpacing="0">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                               </Grid.ColumnDefinitions>
                               <Label Grid.Column="0" Margin="0" Text="{Binding Name}"/>
                               <Label Grid.Column="1" Margin="0" Text="{Binding Category}"/>
                               <!--This following Label is the one not binding -->
                               <Label Grid.Column="2" Margin="0" Text="{Binding GetPlayCount}" />
                           </Grid>
                       </ViewCell.View>
                 </ViewCell>
           </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

Code Behind:代码背后:

 public partial class CollectionPage : ContentPage
    {
        CollectionViewModel collectionView = new CollectionViewModel();

        public CollectionPage()
        {
            InitializeComponent();
            BindingContext = collectionView;
        }
    }

ViewModel:视图模型:

public class CollectionViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Game> games;
        public ObservableCollection<Game> Games
        {
            get { return games; }
            set
            {
                games = value;
                OnPropertyChanged("Games");
            }
        }

        public CollectionViewModel()
        {
            GetGames();
        }
            

        public async void GetGames()
        {
            var restService = new RestService();
            Games = new ObservableCollection<Game>(await restService.GetGamesAsync());
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Model: Model:

public class Game 
    {
        public string Name { get; set; }

        public string Category { get; set; }

        public async Task<int> GetPlayCount()
        {
            return (await new RestService().GetGamesAsync()).Where(result => result.Name == this.Name).Count();
        }
    }

You can bind only to the property.您只能绑定到该属性。 You may call that function from the property getter.您可以从属性获取器中调用 function。 However it is not possible to bind to the function.但是,无法绑定到 function。 The app doesn't know when your function is updated, so binding wouldn't make much sense.该应用程序不知道您的 function 何时更新,因此绑定没有多大意义。 For the property you can call PropertyChanged to signal that the property has a new value.对于属性,您可以调用PropertyChanged来表示该属性具有新值。

I will talk with code:我会用代码说话:

[NotMapped]
public decimal TotalPrice { get =>GetTotalPrice(); }

private decimal GetTotalPrice()
{
  decimal result = 0;
  foreach(var dpo in DetailPurchaseOrder)
  {
    result = result + dpo.GetTotalPurchasePrice();
  }
  return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM