Xamarin的新手。显示来自另一个表/模型/对象的数据的最佳方法是什么?还是根本不?
我想尝试System.Linq的Enumerable.Join,但这不会破坏可观察集合的目的吗?我想改变事物并插入记录。我一直在尝试使用其他模型将信息分组在一起,但是没有运气。
[尝试将轮播视图与其他围绕该信息的组模型一起使用。问题来自有效的API。谢谢大家。
问题
答案
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>
查看:
<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;
}
希望这会有所帮助。