c# - 所选项目在选择器中不起作用

标签 c# xamarin mvvm xamarin.forms

在我们的编辑页面中,我们在将值填充到选择器中的选定项目时遇到了问题,并且由于某种原因它也不会从 LoadCourses() 或 LoadRoundCategories() 中选择。

有什么想法吗?

代码如下:

View 模型

public class EditGolfRoundViewModel : INotifyPropertyChanged
{
    ApiServices _apiServices = new ApiServices();

    private string _message;
    private ObservableCollection<GolfCourse> _courses;
    private ObservableCollection<GolfRoundCategory> _roundCategories;
    private object_selectedGolfCourse;
    private GolfRoundCategory _selectedGolfRoundCategory;
    private GolfRound _golfRound;

    public EditGolfRoundViewModel()
    {
        _selectedGolfCourse = new GolfCourse();
        _selectedGolfRoundCategory = new GolfRoundCategory();
        LoadCourses();
        LoadRoundCategories();
    }

    public GolfRound GolfRound
    {
        get { return _golfRound; }
        set
        {
            _golfRound = value;
            OnPropertyChanged();
        }
    }


    public string Message
    {
        get { return _message; }

        set
        {
            _message = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<GolfCourse> GolfCourses
    {
        get { return _courses; }
        set
        {
            if (_courses != value)
            {
                _courses = value;
                OnPropertyChanged();
            }

        }
    }

    public ObservableCollection<GolfRoundCategory> GolfRoundCategories
    {
        get { return _roundCategories; }
        set
        {
            _roundCategories = value;
            OnPropertyChanged();
        }
    }

    public object SelectedGolfCourse
    {
        get { return _selectedGolfCourse; }
        set
        {
            _selectedGolfCourse = value;
            var golfCourse = _selectedGolfCourse as GolfCourse;
            Guid tempGolfCourseID = golfCourse.GolfCourseID;
            OnPropertyChanged("SelectedGolfCourse");
        }
    }


    public GolfRoundCategory SelectedGolfRoundCategory
    {
        get { return _selectedGolfRoundCategory; }
        set
        {
            _selectedGolfRoundCategory = value;
            OnPropertyChanged();
        }
    }



    public ICommand EditCommand 
    { 
        get
        {
            return new Command(async() =>
            {
                GolfRound.GolfCourseID = SelectedGolfCourse.GolfCourseID;
                GolfRound.GolfCourse = SelectedGolfCourse;
                GolfRound.GolfRoundCategoryID = SelectedGolfRoundCategory.GolfRoundCategoryID;
                GolfRound.GolfRoundCategory = SelectedGolfRoundCategory;
                GolfRound.LastModifiedUTC = System.DateTime.Now;

                await _apiServices.PutGolfRoundAsync(GolfRound, Settings.AccessToken);
            });
        }

    }

    public ICommand DeleteCommand
    {
        get
        {
            return new Command(async () =>
            {
                await _apiServices.DeleteGolfRoundAsync(GolfRound.GolfRoundID, Settings.AccessToken);
            });
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private async void LoadCourses()
    {
        GolfCourses = new ObservableCollection<GolfCourse>(await _apiServices.GetGolfCoursesAsync(Settings.AccessToken));
    }

    private async void LoadRoundCategories()
    {
        GolfRoundCategories = new ObservableCollection<GolfRoundCategory>(await _apiServices.GetGolfRoundCategoriesAsync(Settings.AccessToken));
    }

}

View - XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:viewModels="clr-namespace:AthlosifyMobile.ViewModels.Golf"
         x:Class="AthlosifyMobile.Views.EditGolfRoundPage">

<StackLayout Orientation="Vertical" VerticalOptions="Center" Spacing="30" Padding="30">
    <Entry Text="{Binding GolfRound.Name}" Placeholder="name" FontSize="Default" />
    <Entry Text="{Binding GolfRound.Notes}" Placeholder="notes" FontSize="Default" />
    <Entry Text="{Binding GolfRound.DailyHandicap}" Placeholder="daily handicap" FontSize="Default" />
    <Label Text="Date" />
    <DatePicker Date="{Binding GolfRound.TeeOffUTC}" 
                Format="D"
                Margin="30, 0, 0, 30"  />
    <Picker x:Name="pCourse" Title="Course" ItemsSource="{Binding GolfCourses}" 
            SelectedItem="{Binding SelectedGolfCourse, Mode=TwoWay}" 
            ItemDisplayBinding="{Binding Name}"></Picker>
    <Entry Text="{Binding GolfRound.GolfCourse.Name}" Placeholder="selected golf course" FontSize="Default" />
    <Picker x:Name="pCategory" Title="Category" ItemsSource="{Binding GolfRoundCategories}" 
            SelectedItem="{Binding SelectedGolfRoundCategory, Mode=TwoWay}"
            ItemDisplayBinding="{Binding Name}"></Picker>
    <Entry Text="{Binding SelectedGolfRoundCategory.Name}" Placeholder="selected round category" FontSize="Default" />
    <Button Command="{Binding EditCommand}" Text="Edit Round" />
    <Button Command="{Binding DeleteCommand}" Text="Delete Round" />

    <Label Text="{Binding Message}" ></Label>
</StackLayout>

查看 - 代码隐藏

public partial class EditGolfRoundPage : ContentPage
{
    public EditGolfRoundPage (GolfRound round)
    {
        var editGolfRoundViewModel = new EditGolfRoundViewModel();
        editGolfRoundViewModel.GolfRound = round;

        BindingContext = editGolfRoundViewModel;

        InitializeComponent ();

        //var editGolfRoundViewModel = new EditGolfRoundViewModel();
        //editGolfRoundViewModel.GolfRound = round;
        //editGolfRoundViewModel.SelectedGolfCourse = round.GolfCourse;

        //BindingContext = editGolfRoundViewModel;

    }
}

最佳答案

实现IEquatable SelectedItem 中使用的属性类:

public class GolfCourse : IEquatable<GolfCourse>
{
    ...
    public bool Equals(GolfCourse other)
    {
        if (other == null) return false;
        return (this.Name.Equals(other.Name));
    }
}

用法,假设 ItemsSource 包含一个值为 Name 的对象,如下所示:

SelectedGolfCourse = new GolfCourse { Name = "Course 2" };

关于c# - 所选项目在选择器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51691550/

相关文章:

c# - 动态添加上下文菜单项

c# - 为什么我的 ListView 看起来是空的?

c# - 如何在 WPF View 中获取无效值

c# - 检查文本框中输入的值是否为 WPF 中的双数

Android 使用数据绑定(bind)库动态包含布局

c# - ASP.NET MVC2 数据访问层

c# - 为什么协变隐式转换会忽略通用约束?

c# - 如何获取xamarin.android中的音乐文件夹路径?

android - Xamarin Android ListView 的垂直滚动

c# - 如何在图像上写XAML控件?