xamarin开发UWP元素的初始化设置顺序

时间:2023-03-09 21:45:18
xamarin开发UWP元素的初始化设置顺序

在开发xamarin的UWP平台不可避免的遇到一下坑,现记录下来,希望对后面踩坑有帮助。

一、listview的分组问题:当我们使用listview的IsGroupingEnabled=true时,如果你采用如下代码写分组的头代码的话,那这个DataTemplate将不起作用。

list = new ListView(ListViewCachingStrategy.RecycleElement)
{
IsGroupingEnabled = true,
GroupDisplayBinding = new Binding("Key", BindingMode.OneWay, new HanderTextConverter()),
//注意这里设置的模板将不起作用,
GroupHeaderTemplate = new DataTemplate(typeof(BorwseHanderCell))
//GroupShortNameBinding = new Binding("Key"),
SeparatorColor = Color.White,
BackgroundColor = Color.White,
VerticalOptions = LayoutOptions.FillAndExpand,
//HasUnevenRows = true,
};

需要在初始化后在设置GroupHeaderTemplate的模板

 list = new ListView(ListViewCachingStrategy.RecycleElement)
{
IsGroupingEnabled = true,
GroupDisplayBinding = new Binding("Key", BindingMode.OneWay, new HanderTextConverter()),
//GroupShortNameBinding = new Binding("Key"),
SeparatorColor = Color.White,
BackgroundColor = Color.White,
VerticalOptions = LayoutOptions.FillAndExpand,
//HasUnevenRows = true,
}; list.GroupHeaderTemplate = new DataTemplate(typeof(BorwseHanderCell));

同时那个GroupDisplayBinding也是没有效果的。

二、MasterDetailPage的Detail页的Title问题。

如果你使用了this.Detail.BindingContext = lst[0]这样的方式绑定Mastpage和Detailpage的内容交互的话,如果你想更改Detail页的Title那需要如下设置,注意代码的顺序,搞错顺序将无效:

 listView.ItemsSource = mailFolederList;
mailFolederList[].IsSelected = true;
this.Detail.BindingContext = lst[];
this.Detail.Title = lst[].FolderName;
this.Title = “我是更改的Title”;
this.IsPresented = false;