Xamarin MVVM 显示来自另一个 Model/Table/Object 的数据
Xamarin MVVM Display Data From Another Model/Table/Object
Xamarin 新手。显示我需要的来自另一个 table/model/object 的数据的最佳方式是什么?或者根本不?
我想尝试 System.Linq 的 Enumerable.Join 但这是否违背了可观察集合的目的?我想更改内容并插入记录。我一直在尝试使用另一种模型将信息组合在一起,但没有成功。
正在尝试将轮播视图与围绕信息的另一个组模型一起使用。问题来自工作 API。谢谢大家
问题
- q问题
- 名字
回答
- pAnswer
- f问题
- 价值
- 评论
视图模型
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>
视图模型:
支持 属性 轮播视图。
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;
}
希望这对您有所帮助。
Xamarin 新手。显示我需要的来自另一个 table/model/object 的数据的最佳方式是什么?或者根本不?
我想尝试 System.Linq 的 Enumerable.Join 但这是否违背了可观察集合的目的?我想更改内容并插入记录。我一直在尝试使用另一种模型将信息组合在一起,但没有成功。
正在尝试将轮播视图与围绕信息的另一个组模型一起使用。问题来自工作 API。谢谢大家
问题
- q问题
- 名字
回答
- pAnswer
- f问题
- 价值
- 评论
视图模型
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>
视图模型:
支持 属性 轮播视图。
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;
}
希望这对您有所帮助。