【问题标题】:How can you bind a Label to a function result in Xamarin.Forms如何将标签绑定到 Xamarin.Forms 中的函数结果
【发布时间】:2021-06-29 12:52:42
【问题描述】:

我正在尝试将Label 绑定到GetPlayCount() 函数调用的结果。 NameCategory 的其他绑定按预期工作,但第三个标签没有输出

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

型号:

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

【问题讨论】:

    标签: c# xaml xamarin.forms data-binding


    【解决方案1】:

    您只能绑定到属性。您可以从属性 getter 调用该函数。但是,无法绑定到该函数。该应用程序不知道您的功能何时更新,因此绑定没有多大意义。对于该属性,您可以调用PropertyChanged 来表示该属性具有新值。

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 1970-01-01
      • 2020-11-23
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多