代码之家  ›  专栏  ›  技术社区  ›  Tuğçe Arar

XAML ListView:GroupHeaderTemplate绑定在发布时不起作用

  •  0
  • Tuğçe Arar  · 技术社区  · 6 年前

    我下课了:

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

    我在视图模型中的属性是:

     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>
    

    这在调试模式下工作,但当我设置发布模式时,它不会在GroupHeaderTemplate中同时显示密钥iOS和Android。但当我在标签上设置静态文本时,它就工作了。

    这是调试模式: debug

    这是释放模式: release

    1 回复  |  直到 6 年前
        1
  •  0
  •   Tuğçe Arar    6 年前

    好吧,我找到了解决办法。当我使用这样的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()
                   };
        }
    

    它起作用了。我读过这个( https://bugzilla.xamarin.com/show_bug.cgi?id=56250 )关于Bugzilla的bug报告。其中一条评论指出:

    链接器可能正在剥离System.Linq或其他SDK程序集 它正在破坏lambda表达式的使用。

    所以,我的问题解决了。