Templates Xamarin MVVM显示来自另一个模型/表/对象的数据

Templates Xamarin MVVM显示来自另一个模型/表/对象的数据,templates,xamarin,mvvm,carousel,observablecollection,Templates,Xamarin,Mvvm,Carousel,Observablecollection,Xamarin的新成员。显示其他表/模型/对象所需数据的最佳方式是什么?还是根本不 我想试试System.Linq的Enumerable.Join,但这不是违背了可观察集合的目的吗?我想更改内容并插入记录。我一直在尝试使用另一个模型将信息分组在一起,但没有成功 尝试使用另一个组模型环绕信息的旋转木马视图。问题来自一个正在运行的API。谢谢大家 问题: 问题 名字 答复 潘斯沃 质询 价值观 评论 视图模型 IEnumerable<QuestionMo

Xamarin的新成员。显示其他表/模型/对象所需数据的最佳方式是什么?还是根本不

我想试试System.Linq的Enumerable.Join,但这不是违背了可观察集合的目的吗?我想更改内容并插入记录。我一直在尝试使用另一个模型将信息分组在一起,但没有成功

尝试使用另一个组模型环绕信息的旋转木马视图。问题来自一个正在运行的API。谢谢大家

问题:

  • 问题
  • 名字
答复

  • 潘斯沃
  • 质询
  • 价值观
  • 评论
视图模型

                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();
IEnumerable questions=wait-DataSource.GetQuestionsAsync(true);
问题列表;
int k=0;
foreach(q中的var i)
{
//模板问题
问题清单。添加(i);
var c=k++;
字符串s=(c+1).ToString();
var a=新的回答模型
{
pAnswer=s,
Posted=日期时间。现在,
fQuestion=i.pQuestion,
值=i.标准,
Comments=“Commnt here”
};
//每个问题的答案模板
答复收集。添加(a);
}
//模板
foreach(AnswerCollection中的var i)
{
var n=新的组列表
{
pGroup=i.pQuestion.ToString(),
Name=i.名称
};
n、 加(i);
GroupedAnswerCollection.Add(n);
}
//var g=AnswerCollection.Join(
//问题清单,
//foreign=>foreign.fQuestion,
//primary=>primary.pQuestion,
//(初级,外国)=>新
//    {
//Test=primary.pAnswer,
//Test2=foreign.Name,
//}).ToList();
Xaml


查看:

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

视图模型:

转盘视图的备份属性

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;
}
私有IEnumerable groupedCollection;
公共IEnumerable GroupedCollection
{
get=>groupedCollection;
设置
{
groupedCollection=值;
OnPropertyChanged(名称(GroupedCollection));
}
}
//可以从多个源提取信息并将其打包以用于视图。
私有void GetQuestionAnswers()
{
//从两个独立的源中提取数据
var questions=wait QuestionApi.GetQuestions();
var answers=等待应答sapi.GetAnswers();
//使用这些来构建名为questionAnswerList的问答列表
GroupedCollection=问题回答列表;
}

希望这能有所帮助。

您在哪里设置
GroupedCollection
DataTemplate
中的绑定将具有构成
GroupedCollection
的模型的绑定上下文。看起来您正试图绑定到回答中的属性,但也有疑问。那不行。是否有您没有在示例中加入的组合模型?我在循环中创建了组列表。这就是我所能做到的。我在构造函数上还有一个新的ObservableCollection。这就是你的意思吗?还有,如果它不起作用的话。我该怎么做呢?我正在学习,我仍然需要以正确的方式学习。从这个问题很难判断你到底想要达到什么目标。但简而言之。ViewModel的目的是以这样一种方式处理和格式化数据,即通过简单地绑定到properties.gotcha,可以轻松地从视图中使用数据。好的,我的问题是viewmodel是否与数据只有一对一的关系?或者可以是一对多?我希望在同一个视图中显示每个问题(问题模型)和答案(答案模型),因此,如果它是sql中的select语句,则它将是来自答案内部连接问题的pQuestion、Name、pAnswer、Value、注释的结果。fQuestion=question.pQuestionviewmodel与数据有一对多关系。它可以来自任何数量的来源。视图与viewmodel(其绑定上下文)有一对一的关系,但默认情况下DataTemplates具有不同的绑定上下文。啊,明白了。因此,创建一个模型将两者结合起来。这不会影响我以后想要更改的答案模型。我想得对吗?我觉得我要为所有东西和它们的妈妈建立一个模型。通常,你会为从数据库接收到的对象建立一个模型。这可用于sto
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;
}