Xamarin MVVM Отображение данных из другой модели / таблицы / объекта - PullRequest
0 голосов
/ 11 февраля 2020

Новый для Xamarin. Каков наилучший способ отображения нужных мне данных из другой таблицы / модели / объекта? Или совсем нет?

Я хочу попробовать Enumerable.Join из System.Linq, но разве это не противоречит цели наблюдаемой коллекции? Я хочу изменить вещи и вставить записи. Я пытался использовать другую модель для группировки информации, но безуспешно.

Пытался использовать представление карусели с другой моделью группы, обернутой вокруг информации. Вопросы приходят из рабочего API. Спасибо всем.

Вопрос

  • qВопрос
  • имя

Ответ

  • pОтвет
  • fQuestion
  • Значение
  • Комментарии

ViewModel

                IEnumerable<QuestionModel> questions = await DataSource.GetQuestionsAsync(true);

            QuestionList.Clear();

            int k = 0;

            foreach (var i in q)
            {
                // questions for template
                QuestionList.Add(i);

                var c = k++;

                string s = (c + 1).ToString();

                var a = new AnswerModel
                {
                    pAnswer = s,
                    Posted = DateTime.Now,
                    fQuestion = i.pQuestion,
                    Value = i.Standard,
                    Comments = "Commnt here"
                };

                // template of answers for each question
                AnswerCollection.Add(a);
            }

            // templates
            foreach (var i in AnswerCollection)
            {
                var n = new GroupList<QuestionModel>
                {
                    pGroup = i.pQuestion.ToString(),
                    Name = i.Name
                };

                n.Add(i);

                GroupedAnswerCollection.Add(n);
            }

            //var g = AnswerCollection.Join(
            //    QuestionList,
            //    foreign => foreign.fQuestion,
            //    primary => primary.pQuestion,
            //    (primary, foreign) => new
            //    {
            //        Test = primary.pAnswer,
            //        Test2 = foreign.Name,

            //    }).ToList();

Xaml

<CarouselView ItemsSource="{Binding GroupedCollection}"
                      HorizontalOptions="FillAndExpand"
                      VerticalOptions="FillAndExpand">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Frame BorderColor="DarkGray"
                               Margin="20"
                               WidthRequest="200"
                               HorizontalOptions="Center"
                            VerticalOptions="CenterAndExpand">
                            <StackLayout>

                                <StackLayout>
                                    <Label Text="{Binding pGroup}"></Label>
                                    <Label Text="{Binding Name}"></Label>
                                    <Label Text="{Binding Other}"></Label>
                                </StackLayout>


                                <StackLayout>
                                    <Label Text="{Binding pAnswer}"></Label>
                                    <Label Text="{Binding fQuestion}" ></Label>
                                    <Label Text="{Binding Value}" ></Label>
                                    <Label Text="{Binding Comments}" ></Label>
                                </StackLayout>

                            </StackLayout>

                        </Frame>
                    </StackLayout>

                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>

1 Ответ

0 голосов
/ 12 февраля 2020

Представление:

<CarouselView ItemsSource="{Binding GroupedCollection}"
                      HorizontalOptions="FillAndExpand"
                      VerticalOptions="FillAndExpand">
            <CarouselView.ItemTemplate>
                <!--All properties inside this data template need to exist in the QuestionAnswer object -->
                <DataTemplate>
                    <StackLayout>
                        <Frame BorderColor="DarkGray"
                               Margin="20"
                               WidthRequest="200"
                               HorizontalOptions="Center"
                            VerticalOptions="CenterAndExpand">
                            <StackLayout>

                                <StackLayout>
                                    <Label Text="{Binding pGroup}"></Label>
                                    <Label Text="{Binding Name}"></Label>
                                    <Label Text="{Binding Other}"></Label>
                                </StackLayout>


                                <StackLayout>
                                    <Label Text="{Binding pAnswer}"></Label>
                                    <Label Text="{Binding fQuestion}" ></Label>
                                    <Label Text="{Binding Value}" ></Label>
                                    <Label Text="{Binding Comments}" ></Label>
                                </StackLayout>

                            </StackLayout>

                        </Frame>
                    </StackLayout>

                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>

ViewModel:

Свойство поддержки для представления карусели.

private IEnumerable<QuestionAnswer> groupedCollection;
public IEnumerable<QuestionAnswer> GroupedCollection
{
    get => groupedCollection;
    set
    {
        groupedCollection = value;
        OnPropertyChanged(nameof(GroupedCollection));
    }
}

//Can pull information from multiple sources and package it for the view.
private void GetQuestionAnswers()
{
    //pulling data from 2 separate sources
    var questions = await QuestionApi.GetQuestions();
    var answers = await AnswersApi.GetAnswers();

    //use these to build QuestionAnswer list called questionAnswerList

    GroupedCollection = questionAnswerList;
}

Надеюсь, это поможет.

...