xamarin 如何在不使用集合列表的情况下填充选取器?

kzmpq1sx  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(150)

我一直在尝试填充我的选择器,但我不知道如何。我看了一些在线教程,但他们是一种混乱,他们都创建了一个项目列表,我还没有这样做。

namespace FYP.ViewModels
{
    public class NewGames
    {
        public int Id { get; set; }
        public string GameTitle { get; set; }
        public double Rating { get; set; }
        public string ImageSource { set; get; }
    }
}

●使用系统.组件模型;使用系统文本;
命名空间FYP. ViewModels {

public class NewReleasesViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<NewGames> NewGames;

    public ObservableCollection<NewGames> Games
    {
        get { return NewGames; }
        set { NewGames = value;

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Games"));
        }
    }

    public NewReleasesViewModel() 
    {
        Games = new ObservableCollection<NewGames>();
        AddData();
    }
    private void AddData()
    {
        Games.Add(new NewGames
        {
            Id = 0,
            GameTitle = "The Elder Scrolls Online",
            Rating = 4.9,
            ImageSource= "https://assets-prd.ignimgs.com/2022/01/05/elderscrollsonline-                             1641413357689.jpg"
        });
        Games.Add(new NewGames
        {
            Id = 1,
            GameTitle = "World Of Warcraft",
            Rating = 4.9,
            ImageSource = "https://assets-prd.ignimgs.com/2021/12/10/wow-1639126324635.jpg"
        });
        Games.Add(new NewGames
        {
            Id = 2,
            GameTitle = "Star Wars: The Old Republic",
            Rating = 4.9,
            ImageSource = "https://assets-prd.ignimgs.com/2022/01/27/swotor-sq1-1643302998212.jpg"
        });

    }
}

}'

<ContentPage.Content>
    <StackLayout>
      <Label Text="Select Game" TextColor="Black" FontSize="24" FontAttributes="Bold" Margin="15" />
      <Picker>
      </Picker>
    </StackLayout>
</ContentPage.Content>

我正在尝试这样做,用户选择一个标题,然后在屏幕上加载其他内容(https://i.stack.imgur.com/0nMuR.png

tsm1rwdh

tsm1rwdh1#

你需要指定一个ItemSource

<Picker ItemsSource="{Binding Games}" ... />

你需要告诉它要显示哪个属性

ItemDisplayBinding="{Binding GameTitle}" ...
62lalag4

62lalag42#

结果我漏掉了绑定上下文

<ContentPage.BindingContext>
        <viewmodels:NewReleasesViewModel/>
    </ContentPage.BindingContext>
    <StackLayout>
        <Label Text="Select Game" TextColor="Black" FontSize="24" FontAttributes="Bold" Margin="15" />
        <Picker ItemsSource="{Binding Games}" ItemDisplayBinding="{Binding GameTitle}"/>
    </StackLayout>
</ContentPage>

相关问题