簡體   English   中英

如何將 Label 綁定到 function 結果為 Xamarin.Z645024253191292AE688A8A

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

我正在嘗試將Label綁定到GetPlayCount() function 調用的結果。 NameCategory的其他綁定按預期工作,但第三個 label 沒有 output

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>

代碼背后:

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

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

視圖模型:

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

您只能綁定到該屬性。 您可以從屬性獲取器中調用 function。 但是,無法綁定到 function。 該應用程序不知道您的 function 何時更新,因此綁定沒有多大意義。 對於屬性,您可以調用PropertyChanged來表示該屬性具有新值。

我會用代碼說話:

[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