XAML ListView:GroupHeaderTemplate绑定不适用于发行版

时间:2018-07-17 15:42:52

标签: c# xaml listview mvvm xamarin.forms

我有以下课程:

 public class TitlePropertyViewModel : BaseViewModel
    {
        private int _propertyId;
        private string _name;
        private bool _isRequired;
        private bool _isChecked;
        private bool _answerIsChecked;

        public string GroupingParameter { get; set; }
        public int PropertyId { get => _propertyId; set { _propertyId = value; OnPropertyChanged(); } }
        public string Name { get => _name; set { _name = value; OnPropertyChanged(); } }
        public bool IsChecked { get => _isChecked; set { _isChecked = value; OnPropertyChanged(); } }
        public bool AnswerIsChecked { get => _answerIsChecked; set { _answerIsChecked = value; OnPropertyChanged(); SelectedAnswerChanged?.Invoke(this, new EventArgs()); } }
        public bool IsRequired { get => _isRequired; set { _isRequired = value; OnPropertyChanged(); } }

        public override string ToString() => $"{this.Name}: {(this.IsChecked ? "Checked" : "-")} | {(this.IsRequired ? "Required" : "-")}";
    }

和我在View模型中的属性是:

 private IList<TitlePropertyViewModel> _titleProperties;

    public IList<TitlePropertyViewModel> TitleProperties { get => _titleProperties; set { _titleProperties = value; OnPropertyChanged(); OnPropertyChanged(nameof(GrouppedTitleProperties)); OnPropertyChanged(nameof(GrouppedFilteredTitleProperties)); } }
    public object GrouppedTitleProperties { get => this.TitleProperties?.GroupBy(g => g.GroupingParameter); }
    public object GrouppedFilteredTitleProperties { get => this.TitleProperties?.Where(w => w.IsChecked || w.IsRequired).GroupBy(g => g.GroupingParameter); }

我在TitleProperties中添加数据,并将其绑定到我的ListView中,如下所示:

 <ListView
                ItemsSource="{Binding GrouppedFilteredTitleProperties}"
                IsPullToRefreshEnabled="False"
                IsRefreshing="{Binding IsBusy}"
                IsGroupingEnabled="True"
                HasUnevenRows="True"
                SeparatorVisibility="None"
                CachingStrategy="RecycleElement"
                BackgroundColor="Transparent"
                ItemSelected="Item_Selected"
                >
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Padding="25,15" BackgroundColor="{DynamicResource PrimaryColor}">
                                <Label Text="{Binding Key}" FontAttributes="Bold" FontSize="Large" 
                                       HorizontalOptions="Start" 
                                       VerticalOptions="Center"
                                       TextColor="{DynamicResource TextColorLight}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal" Margin="25,5">
                                <renderer:IconView WidthRequest="25" Source="{Binding IsChecked,Converter={StaticResource PositiveIconConverter}}" FillColor="{Binding IsChecked,Converter={StaticResource PositiveColorConverter}}" VerticalOptions="CenterAndExpand" />
                                <Label Text="{Binding Name}" TextColor="{DynamicResource TextColorLight}"/>
                                <Image Source="exclamation_sign.png" IsVisible="{Binding IsRequired}" VerticalOptions="CenterAndExpand" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

这在调试模式下有效,但是当我设置发布模式时,它在iOS和Android上均未在GroupHeaderTemplate中显示Key。但是当我在标签上设置静态文本时,它就可以工作。

这是调试模式: debug

这是发布模式: release

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。当我使用lambda表达式时:

    public object GrouppedTitleProperties { get => this.TitleProperties?.GroupBy(g => g.GroupingParameter); }

它不起作用。但是当我用这样的查询表达式替换它时:

public object GrouppedTitleProperties
    {
        get => from tp in this.TitleProperties
               group tp by tp.GroupingParameter into GrouppedTitleProperties
               select new
               {
                   Key = GrouppedTitleProperties.Key,
                   Items = GrouppedTitleProperties.ToList()
               };
    }

有效。我在bugzilla上阅读了此(https://bugzilla.xamarin.com/show_bug.cgi?id=56250)错误报告。其中一条评论指出:

  

链接器可能正在剥离System.Linq或另一个SDK程序集   这打破了lambda表达式的使用。

所以,我的问题解决了。