Xamarin MVVM从另一个模型/表/对象显示数据

问题描述 投票:0回答:1

Xamarin的新手。显示来自另一个表/模型/对象的数据的最佳方法是什么?还是根本不?

我想尝试System.Linq的Enumerable.Join,但这不会破坏可观察集合的目的吗?我想改变事物并插入记录。我一直在尝试使用其他模型将信息分组在一起,但是没有运气。

[尝试将轮播视图与其他围绕该信息的组模型一起使用。问题来自有效的API。谢谢大家。

问题

  • qQuestion
  • 名称

答案

  • 答案
  • f问题
  • 评论

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>
templates xamarin mvvm carousel observablecollection
1个回答
0
投票

查看:

<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;
}

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.