简体   繁体   中英

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. The other bindings, for Name and Category , are working as expected, but there is no output for the third label

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:

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. However it is not possible to bind to the function. The app doesn't know when your function is updated, so binding wouldn't make much sense. For the property you can call PropertyChanged to signal that the property has a new value.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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