如何使用 xamarin.forms 中选择的选项设置主详细信息页面?
How to set master details page with option selected in xamarin.forms?
我正在使用主详细信息页面创建一个应用程序。在那,我错过了一件事,当我打开一个应用程序时,第一个项目没有被选中。
我尝试使用不同的解决方案,例如 make "Custom View Cell" 并制作一个渲染器来解决这个问题,但也出现了同样的问题。
我也提到下面的图片。
有什么解决办法吗?
有点复杂,您完全可以在 Forms 项目中完成,无需自定义渲染器。
我列出了实现你想要的步骤。
给Model属性表示选择哪个,实现`INotifyPropertyChanged.
public class MasterPageItem : INotifyPropertyChanged
{
private bool isSelected;
public bool IsSelected {
get {
return isSelected;
}
set {
if (value != this.isSelected)
{
this.isSelected = value;
NotifyPropertyChanged();
}
}
}
}
在ViewCell中绑定父视图的背景色,在Converter中将bool值转换为颜色
<ViewCell>
<Grid Padding="5,10" BackgroundColor="{Binding IsSelected , Converter={StaticResource BooltoColor}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding IconSource}" />
<Label Grid.Column="1" Text="{Binding Title}" />
</Grid>
</ViewCell>
public class BooltoColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Color color ;
if(((bool)value) == true)
{
color = Color.Gray;
}
else
{
color = Color.Transparent;
}
return color;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return true;
}
}
当您点击列表视图项目时,将项目设置为选中状态并取消选中其他项目。
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
foreach (MasterPageItem i in list)
{
i.IsSelected = false;
}
MasterPageItem item = e.Item as MasterPageItem;
if (item != null)
{
item.IsSelected = true;
list.RemoveAt(e.ItemIndex);
list.Insert(e.ItemIndex, item);
}
}
检查下面我的测试图像和示例 link
https://github.com/ColeXm/MasterDetailedSample/blob/master/Xamarin_Forms___MasterDetailPage.zip
我正在使用主详细信息页面创建一个应用程序。在那,我错过了一件事,当我打开一个应用程序时,第一个项目没有被选中。 我尝试使用不同的解决方案,例如 make "Custom View Cell" 并制作一个渲染器来解决这个问题,但也出现了同样的问题。
我也提到下面的图片。
有点复杂,您完全可以在 Forms 项目中完成,无需自定义渲染器。
我列出了实现你想要的步骤。
给Model属性表示选择哪个,实现`INotifyPropertyChanged.
public class MasterPageItem : INotifyPropertyChanged { private bool isSelected; public bool IsSelected { get { return isSelected; } set { if (value != this.isSelected) { this.isSelected = value; NotifyPropertyChanged(); } } } }
在ViewCell中绑定父视图的背景色,在Converter中将bool值转换为颜色
<ViewCell> <Grid Padding="5,10" BackgroundColor="{Binding IsSelected , Converter={StaticResource BooltoColor}}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="30"/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{Binding IconSource}" /> <Label Grid.Column="1" Text="{Binding Title}" /> </Grid> </ViewCell> public class BooltoColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Color color ; if(((bool)value) == true) { color = Color.Gray; } else { color = Color.Transparent; } return color; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return true; } }
当您点击列表视图项目时,将项目设置为选中状态并取消选中其他项目。
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e) { foreach (MasterPageItem i in list) { i.IsSelected = false; } MasterPageItem item = e.Item as MasterPageItem; if (item != null) { item.IsSelected = true; list.RemoveAt(e.ItemIndex); list.Insert(e.ItemIndex, item); } }
检查下面我的测试图像和示例 link
https://github.com/ColeXm/MasterDetailedSample/blob/master/Xamarin_Forms___MasterDetailPage.zip