我有ListView(在MainPage上)列出练习,而ExercisePage用于编辑现有或添加新项目。有用。问题是如何使用选择器控件在ExercisePage(而不是muscleGroupEntry)上选择MuscleGroup并在Exercise实例中保存更改?
我尝试使用这里描述的ItemsSource和ItemDisplayBinding属性Bindable picker,但它不起作用。
感谢您的帮助!这是我的代码。
练习课:
public class Exercise : INotifyPropertyChanged
{
public enum muscleGroups
{
Back,
Chest
}
public string title { get; set; }
public muscleGroups muscleGroup { get; set; }
public string Title
{
get { return title; }
set
{
if (title != value)
{
title = value;
OnPropertyChanged("Title");
}
}
}
public muscleGroups MuscleGroup
{
get { return muscleGroup; }
set
{
if (muscleGroup != value)
{
muscleGroup = value;
OnPropertyChanged("MuscleGroup");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
ExercisePage.xaml
<StackLayout>
<Entry x:Name="titleEntry" Text="{Binding Path=Title}" />
<Entry x:Name="muscleGroupEntry" Text="{Binding Path=MuscleGroup}" />
<Button Text="Save" Clicked="SaveExercise" />
</StackLayout>
ExercisePage.xaml.cs
public partial class ExercisePage : ContentPage
{
bool edited = true;
public Exercise Exercise { get; set; }
public ExercisePage (Exercise exercise)
{
InitializeComponent ();
Exercise = exercise;
if (exercise == null)
{
Exercise = new Exercise();
edited = false;
}
this.BindingContext = Exercise;
}
async void SaveExercise(object sender, EventArgs e)
{
await Navigation.PopAsync();
if (edited == false)
{
NavigationPage navPage = (NavigationPage)Application.Current.MainPage;
IReadOnlyList<Page> navStack = navPage.Navigation.NavigationStack;
MainPage homePage = navStack[navPage.Navigation.NavigationStack.Count - 1] as MainPage;
if (homePage != null)
{
homePage.AddExercise(Exercise);
}
}
}
MainPage.xaml中
<StackLayout>
<Button Text="Add" Clicked="AddButton_Click" />
<ListView x:Name="exercisesList" ItemsSource="{Binding}" ItemSelected="OnListViewItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Title}" FontSize="Medium" Grid.Column="0" />
<Label Text="{Binding MuscleGroup}" FontSize="Medium" Grid.Column="1" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
MainPage.xaml.cs中
public partial class MainPage : ContentPage
{
protected internal ObservableCollection<Exercise> Exercises { get; set; }
public MainPage()
{
InitializeComponent();
Exercises = new ObservableCollection<Exercise>
{
new Exercise {Title="Exercise 1", MuscleGroup = Exercise.muscleGroups.Chest},
new Exercise {Title="Exercise 2", MuscleGroup = Exercise.muscleGroups.Chest},
new Exercise {Title="Exercise 3", MuscleGroup = Exercise.muscleGroups.Back},
new Exercise {Title="Exercise 4", MuscleGroup = Exercise.muscleGroups.Back}
};
exercisesList.BindingContext = Exercises;
}
private async void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs args)
{
Exercise selectedExercise = args.SelectedItem as Exercise;
if (selectedExercise != null)
{
exercisesList.SelectedItem = null;
await Navigation.PushAsync(new ExercisePage(selectedExercise));
}
}
private async void AddButton_Click(object sender, EventArgs e)
{
await Navigation.PushAsync(new ExercisePage(null));
}
protected internal void AddExercise(Exercise exercise)
{
Exercises.Add(exercise);
}
}
提前致谢。