C# XAML ListView:GroupHeaderTemplate绑定在发布时不起作用
我有以下课程:C# XAML ListView:GroupHeaderTemplate绑定在发布时不起作用,c#,xaml,listview,mvvm,xamarin.forms,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; publi
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); }
IList私有产权;
public IList TitleProperties{get=>\u TitleProperties;set{u TitleProperties=value;OnPropertyChanged();OnPropertyChanged(nameof(GrouppedTitleProperties));OnPropertyChanged(nameof(GrouppedFilteredTitleProperties));}
公共对象GrouppedTitleProperties{get=>this.TitleProperties?.GroupBy(g=>g.GroupingParameter);}
公共对象GrouppedFilteredTitleProperties{get=>this.TitleProperties?.Where(w=>w.IsChecked | | w.IsRequired).GroupBy(g=>g.GroupingParameter);}
我在TitleProperty中添加数据,并将其绑定到我的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>
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()
};
}
这在调试模式下工作,但当我设置发布模式时,它不会在GroupHeaderTemplate中同时显示iOS和Android。但当我在标签上设置静态文本时,它就起作用了
这是调试模式:
这是释放模式:
好的,我找到了解决办法。当我像这样使用lambda表达式时:
<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>
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()
};
}
它不起作用。但当我用如下查询表达式替换它时:
<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>
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的bug报告。其中一项评论指出:
链接器可能正在剥离System.Linq或其他SDK程序集
这打破了lambda表达式的使用
所以,我的问题解决了。视图模型中的“键”属性到底在哪里?我在您提供的代码中没有看到它?