我想知道如何保存复选框的状态?该复选框位于listview
个鸟类中。如果单击该框,则意味着您已经看到了那只鸟并保存了状态。我尝试过使用偏好设置,但是不知道我是否走对了。
更新
这是BirdListZA的xaml
<Frame x:Name="frame" BackgroundColor="#f3f0e9" BorderColor="#f3f0e9" CornerRadius="20"
AbsoluteLayout.LayoutBounds="0.5,0.95,0.9,0.9" AbsoluteLayout.LayoutFlags="All">
<ListView x:Name="blistview" BackgroundColor ="#f3f0e9" HasUnevenRows="True" ItemSelected="blistview_ItemSelected" RowHeight="80"
AbsoluteLayout.LayoutBounds="0.5,0.95,0.9,0.9" AbsoluteLayout.LayoutFlags="All" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding BirdNames}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" HeightRequest="40" TextColor="Black" Padding="20,20,0,0" BackgroundColor="#f3f0e9" FontFamily="appfontM"/>
<CheckBox x:Name="checkbox" IsChecked="{Binding isChecked}" CheckedChanged="CheckBox_CheckedChanged"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Frame>
这是BirdListAZ页面背后的代码
public partial class BirdListAZ : ContentPage
{
private ObservableCollection<birdlistmodel> listBirds;
IEnumerable<birdlistmodel> GetBirds(string searchText = null)
{
var birds = new List<birdlistmodel>
{
new birdlistmodel (){Id = 1, BirdNames = "Apalis, Bar-throated", isChecked = false },
new birdlistmodel (){Id = 2, BirdNames = "Apalis, Yellow-breasted", isChecked = false},
new birdlistmodel (){Id = 3, BirdNames = "Barbet, Acacia Pied", isChecked = false },
new birdlistmodel (){Id = 4, BirdNames = "Barbet, Black-collared", isChecked = false},
new birdlistmodel (){Id = 5, BirdNames = "Batis, Cape", isChecked = false },
new birdlistmodel (){Id = 6, BirdNames = "Batis, Chinspot", isChecked = false},
new birdlistmodel (){Id = 7, BirdNames = "Bee-eater, European", isChecked = false },
new birdlistmodel (){Id = 8, BirdNames = "Bee-eater, White-fronted", isChecked = false},
new birdlistmodel (){Id = 9, BirdNames = "Bishop,Southern Red", isChecked = false },
new birdlistmodel (){Id = 10, BirdNames = "Bokmakierie", isChecked = false},
new birdlistmodel (){Id = 11, BirdNames = "Boubou, Southern", isChecked = false },
new birdlistmodel (){Id = 12, BirdNames = "Brownbul, Terrestrial", isChecked = false},
new birdlistmodel (){Id = 13, BirdNames = "Bulbul, Dark-capped",isChecked = false },
new birdlistmodel (){Id = 14, BirdNames = "Bunting, Cape", isChecked = false},
new birdlistmodel (){Id = 15, BirdNames = "Bunting, Golden-breasted", isChecked = false },
new birdlistmodel (){Id = 16, BirdNames = "Bushshrike, Gorgeous", isChecked = false},
new birdlistmodel (){Id = 17, BirdNames = "Bushshrike, Grey-headed", isChecked = false },
new birdlistmodel (){Id = 18, BirdNames = "Bushshrike, Olive", isChecked = false},
new birdlistmodel (){Id = 19, BirdNames = "Bushshrike, Orange-breasted", isChecked = false },
};
if (String.IsNullOrWhiteSpace(searchText))
return birds;
var lowerBirds = searchText.ToLower();
return birds.Where(c => c.BirdNames.ToLower().StartsWith(lowerBirds));
}
public BirdListAZ()
{
InitializeComponent();
}
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
blistview.ItemsSource = GetBirds(e.NewTextValue);
}
private void blistview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (((ListView)sender).SelectedItem == null)
return;
var birds = e.SelectedItem as birdlistmodel;
Navigation.PushAsync(new BirdPages(birds.BirdNames, birds.BirdSelect));
((ListView)sender).SelectedItem = null;
}
async private void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
async private void Button_Clicked_1(object sender, EventArgs e)
{
await Navigation.PushAsync(new myBirdList());
}
protected override void OnAppearing()
{
base.OnAppearing();
if (string.IsNullOrWhiteSpace(Preferences.Get("listbirds", "")))
{
var voels = GetBirds();
listBirds = new ObservableCollection<birdlistmodel>(voels);
blistview.ItemsSource = listBirds;
}
else
{
string dataString = Xamarin.Essentials.Preferences.Get("listbirds", "");
ObservableCollection<birdlistmodel> listBirds = JsonConvert.DeserializeObject<ObservableCollection<birdlistmodel>>(dataString);
blistview.ItemsSource = listBirds;
}
}
private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var checkbox = (CheckBox)sender;
var selectBird = checkbox.BindingContext as birdlistmodel;
selectBird.isChecked = e.Value;
string json = JsonConvert.SerializeObject(listBirds);
Preferences.Set("listbirds", json);
}
}
}
birdlistmodel页面
class birdlistmodel
{
public int Id { get; set; }
public string Voellist { get; set; }
public string BirdNames { get; set; }
public Button BirdSelect { get; set; }
public bool isChecked { get; set; }
}
答案 0 :(得分:1)
您将有多个复选框,就像您有多只鸟一样。 使用SQL Lite创建一个简单的数据库来存储每只鸟的复选框状态。
https://docs.microsoft.com/pt-br/xamarin/get-started/quickstarts/database?pivots=windows
答案 1 :(得分:1)
您只能通过执行此操作来保存复选框的状态,并且它不会告诉您选中了哪个项目,如果您有多个选择,它甚至会更加混乱。
如果您想在重新进入页面时显示选定的状态,则可以在模型中添加isChecked
属性。
例如:
birdlistmodel
类:
class birdlistmodel
{
public int Id { get; set; }
public string Voellist { get; set; }
public string BirdNames { get; set; }
public Button BirdSelect { get; set; }
public bool isChecked { get; set; }
}
然后在您的xaml中
<ListView x:Name="blistview" BackgroundColor ="#f3f0e9" HasUnevenRows="True" ItemSelected="blistview_ItemSelected" RowHeight="80"
AbsoluteLayout.LayoutBounds="0.5,0.95,0.9,0.9" AbsoluteLayout.LayoutFlags="All" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding BirdNames}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" HeightRequest="40" TextColor="Black" Padding="20,20,0,0" BackgroundColor="#f3f0e9" FontFamily="appfontM"/>
<CheckBox x:Name="checkbox" IsChecked="{Binding isCheckd}" CheckedChanged="CheckBox_CheckedChanged"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在您的page.cs中:
private void checkbox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var checkbox = (CheckBox)sender;
var selectBird = checkbox.BindingContext as birdlistmodel;
selectBird.isCheckd = e.Value;
//if you want save the data and checkbox state,you could save the data as a json string
string json = JsonConvert.SerializeObject(xxx);//xxx is the List<birdlistmodel> which you binding to the listview
Preferences.Set("listbirds", json);
}
,当您重新进入页面时,您可以从“偏好设置”中获取数据。
string dataString = Xamarin.Essentials.Preferences.Get("listbirds","");
ObservableCollection<birdlistmodel> listBirds = JsonConvert.DeserializeObject<ObservableCollection<birdlistmodel>>(dataString);
第一次进入页面时,应将所有isChecked
属性设置为false
(因为您别无选择)。
var birds = new List<birdlistmodel>
{
new birdlistmodel (){Id = 1, BirdNames = "Apalis, Bar-throated", isChecked = false},
new birdlistmodel (){Id = 2, BirdNames = "Apalis, Yellow-breasted", isChecked = false},
new birdlistmodel (){Id = 3, BirdNames = "Barbet, Acacia Pied", isChecked = false },
new birdlistmodel (){Id = 4, BirdNames = "Barbet, Black-collared", isChecked = false},
new birdlistmodel (){Id = 5, BirdNames = "Batis, Cape", isChecked = false },
new birdlistmodel (){Id = 6, BirdNames = "Batis, Chinspot", isChecked = false},
new birdlistmodel (){Id = 7, BirdNames = "Bee-eater, European", isChecked = false },
new birdlistmodel (){Id = 8, BirdNames = "Bee-eater, White-fronted", isChecked = false},
new birdlistmodel (){Id = 9, BirdNames = "Bishop,Southern Red", isChecked = false },
new birdlistmodel (){Id = 10, BirdNames = "Bokmakierie", isChecked = false},
new birdlistmodel (){Id = 11, BirdNames = "Boubou, Southern", isChecked = false },
new birdlistmodel (){Id = 12, BirdNames = "Brownbul, Terrestrial", isChecked = false}
}
当您选中复选框时,checkbox_CheckedChanged
将被处理并更改状态
更新:
private ObservableCollection<birdlistmodel> listBirds; //global variable
protected override void OnAppearing()
{
base.OnAppearing();
if (string.IsNullOrEmpty(Preferences.Get("listbirds", "")))
{
var voels = GetBirds();
listBirds = new ObservableCollection<birdlistmodel>(voels);
blistview.ItemsSource = listBirds;
}
else
{
string dataString = Xamarin.Essentials.Preferences.Get("listbirds", "");
listBirds = JsonConvert.DeserializeObject<ObservableCollection<birdlistmodel>>(dataString);
blistview.ItemsSource = listBirds;
}
}
然后在您的
private void CheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var checkbox = (CheckBox)sender;
var selectBird = checkbox.BindingContext as birdlistmodel;
selectBird.isChecked = e.Value;
string json = JsonConvert.SerializeObject(listBirds);
Preferences.Set("listbirds", json);
}