【问题标题】:Refreshing ListView in Xamarin.Forms for UWP为 UWP 刷新 Xamarin.Forms 中的 ListView
【发布时间】:2017-01-12 16:01:37
【问题描述】:

我的系统的详细信息是 操作系统:Windows 10 Pro N 视觉工作室企业 2015 Xamarin.Forms 2.3.1..114

我创建了一个选项卡式视图,在该视图中我使用 Navigation.PushModalAsync 方法导航到新页面。在视图中,我有一个带有自定义数据模板的列表视图。数据模板是 ViewCell 的,它包含两个图像和一个标签。

What I am trying to do is when ever a cell is selected, I am showing the Image for checked row and when other row is selected then hiding the other row images and showing the currently selected image.

当第一次加载视图时,我将第一行设置为选中并且一切正常,但是当我选择任何其他行时,ListView 不会刷新。 Image IsVisible 属性设置正确,但未反映在列表中。

参考下面的代码

ListView 的代码

var listView = new ListView();
listView.ItemsSource = StaticData.ListData;
listView.ItemTemplate = new DataTemplate(typeof(CustomDataCell));
listView.VerticalOptions = LayoutOptions.FillAndExpand;
listView.BackgroundColor = Color.White;            
listView.SeparatorVisibility = SeparatorVisibility.Default;
listView.RowHeight = 30;
listView.SeparatorColor = Color.White;
listView.ItemTapped += (sender, e) =>
                {
                if (e == null) return;
                selectedValue = (e.Item as ValiditySchema).Value;
                SelectValidityItem(listView,selectedValue); // In this method I am setting the IsSelected property to true  and other rows IsSelected property to false.                
                };

CustomDataCell 的代码

public class CustomDataCell : ViewCell
{
    public Label CellText { get; set; }
    public BoxView ImageDetail { get; set; }
    public Image CheckedImage { get; set; }
    public CustomDataCell()
    {
        CellText = new Label();
        CellText.FontAttributes = FontAttributes.Bold;
        CellText.SetBinding(Label.TextProperty, "Text");
        CellText.VerticalOptions = LayoutOptions.Center;
        CellText.HorizontalOptions = LayoutOptions.Start;
        CellText.TextColor = Color.Black;
        ImageDetail = new BoxView();
        ImageDetail.WidthRequest = 20;
        ImageDetail.HeightRequest = 10;
        ImageDetail.SetBinding(BoxView.BackgroundColorProperty, "ColorName");
        //declaring image to show the row is selected
        CheckedImage = new Image();
        CheckedImage.Source = "Images/checked.png";
        CheckedImage.HorizontalOptions = LayoutOptions.CenterAndExpand;
        CheckedImage.VerticalOptions = LayoutOptions.Center;
        CheckedImage.SetBinding(Image.IsVisibleProperty, "IsSelected");

        var ContentCell = new StackLayout();
        ContentCell.Children.Add(ImageDetail);
        ContentCell.Children.Add(CellText);
        ContentCell.Children.Add(CheckedImage);
        ContentCell.Spacing = 5;
        ContentCell.Orientation = StackOrientation.Horizontal;
        var maiCell = new StackLayout();
        maiCell.Orientation = StackOrientation.Vertical;
        maiCell.Children.Add(ContentCell);      
        View = maiCell;

    }
}

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    为了让 ListView 知道您的 ItemsSource 中的项目已更改,您需要在该特定项目上引发 INotifyPropertyChanged 事件。

    通常不是将数据直接绑定到 ListView,而是按照 MVVM 模式为每个项目创建一个 ViewModel 表示:

    视图 ViewModel 模型

    所以你需要做的是为StaticData.ListData中的项目创建一个ViewModel:

    public class ListItemViewModel : INotifyPropertyChanged
    {
        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set {
                _isSelected = value;
               OnPropertyChanged();
            }
        }
    
        // more properties here...
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    然后您可以将IsSelected 属性绑定到图像的可见性属性。

    这样,当您在 ViewModel 中更改 IsSelected 时,会触发正确的事件,并且 ListView 现在知道发生了某些更改并且需要刷新视图。

    【讨论】:

    • 感谢@Cheesebaron 的快速回复。我会实施它并让你知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2021-11-29
    • 2019-10-16
    相关资源
    最近更新 更多