Listview 如何在Xamarin.Forms列表视图中显示嵌套集合
伙计们,我是xamarin.forms的新手,尝试在我的xamarin.forms中以嵌套列表视图的形式显示测验,其中将显示问题及其相关选项,但我无法在列表视图中正确显示此列表,所以如果有人知道我如何做,请帮助我…这将是很大的帮助 这是我的密码Listview 如何在Xamarin.Forms列表视图中显示嵌套集合,listview,user-interface,xamarin,xamarin.forms,Listview,User Interface,Xamarin,Xamarin.forms,伙计们,我是xamarin.forms的新手,尝试在我的xamarin.forms中以嵌套列表视图的形式显示测验,其中将显示问题及其相关选项,但我无法在列表视图中正确显示此列表,所以如果有人知道我如何做,请帮助我…这将是很大的帮助 这是我的密码 public QuizViewModel(int testId) { var quizModel = Load(testId); QuizList = new ObservableCollection<
public QuizViewModel(int testId)
{
var quizModel = Load(testId);
QuizList = new ObservableCollection<QuizModel>(quizModel);
}
public List<QuizModel> Load(int testId)
{
List<Suggestions> suggestionlist;
List<QuizModel> quizList = new List<QuizModel>();
var responseString = CommonFunctions.GetQuestionItems(testId);
var responseData = JArray.Parse(responseString.ToString());
var Questions = new List<QuizModel>();
int i = 0;
foreach (var quiz in responseData)
{
i++;
suggestionlist = new List<Suggestions>();
var ques = new QuizModel
{
QuestionNumber = i,
Id = !string.IsNullOrEmpty(quiz["Id"].ToString()) ? (int)quiz["Id"] : 0,
TestId = testId,
Question = !string.IsNullOrEmpty(quiz["Title"].ToString()) ? quiz["Title"].ToString() : string.Empty,
Answer = !string.IsNullOrEmpty(quiz["Answer"].ToString()) ? quiz["Answer"].ToString() : string.Empty,
UserSelectedAnswer = string.Empty,
};
suggestionlist.Add(new Model.Suggestions { Option = !string.IsNullOrEmpty(quiz["OptionA"].ToString()) ? quiz["OptionA"].ToString() : string.Empty });
suggestionlist.Add(new Model.Suggestions { Option = !string.IsNullOrEmpty(quiz["OptionB"].ToString()) ? quiz["OptionB"].ToString() : string.Empty });
suggestionlist.Add(new Model.Suggestions { Option = !string.IsNullOrEmpty(quiz["OptionC"].ToString()) ? quiz["OptionC"].ToString() : string.Empty });
suggestionlist.Add(new Model.Suggestions { Option = !string.IsNullOrEmpty(quiz["OptionD"].ToString()) ? quiz["OptionD"].ToString() : string.Empty });
ques.Suggestions = suggestionlist;
Questions.Add(ques);
}
return Questions;
}
public QuizViewModel(int testId)
{
var quizModel=荷载(testId);
QuizList=新的可观测集合(quizModel);
}
公共列表加载(int testId)
{
列表建议列表;
List quizList=新列表();
var responseString=CommonFunctions.GetQuestionItems(testId);
var responseData=JArray.Parse(responseString.ToString());
var Questions=新列表();
int i=0;
foreach(响应数据中的var测验)
{
i++;
suggestionlist=新列表();
var ques=新的QuizModel
{
问题编号=i,
Id=!string.IsNullOrEmpty(测验[“Id”].ToString())?(int)测验[“Id”]:0,
TestId=TestId,
问题=!string.IsNullOrEmpty(测验[“Title”].ToString())?测验[“Title”].ToString():string.Empty,
答案=!string.IsNullOrEmpty(测验[“答案”].ToString())?测验[“答案”].ToString():string.Empty,
UserSelectedAnswer=string.Empty,
};
suggestionlist.Add(新模型.建议{Option=!string.IsNullOrEmpty(测验[“OptionA”].ToString())?测验[“OptionA”].ToString():string.Empty});
suggestionlist.Add(新模型.建议{Option=!string.IsNullOrEmpty(测验[“OptionB”].ToString())?测验[“OptionB”].ToString():string.Empty});
suggestionlist.Add(新模型.建议{Option=!string.IsNullOrEmpty(测验[“OptionC”].ToString())?测验[“OptionC”].ToString():string.Empty});
suggestionlist.Add(新模型.建议{Option=!string.IsNullOrEmpty(测验[“OptionD”].ToString())?测验[“OptionD”].ToString():string.Empty});
问题建议=建议列表;
问题。添加(问题);
}
回答问题;
}
测验模式:
public class QuizModel
{
public int Id { get; set; }
public int TestId { get; set; }
public int QuestionNumber { get; set; }
public string Question { get; set; }
public List<Suggestions> Suggestions { get; set; }
public string Answer { get; set; }
public string UserSelectedAnswer { get; set; }
}
公共类QuizModel
{
公共int Id{get;set;}
公共int TestId{get;set;}
公共整数问题编号{get;set;}
公共字符串问题{get;set;}
公共列表建议{get;set;}
公共字符串答案{get;set;}
公共字符串UserSelectedAnswer{get;set;}
}
以下是我的xaml代码:
<ScrollView >
<ListView ItemsSource="{Binding QuizList}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout VerticalOptions="CenterAndExpand" Orientation="Horizontal">
<Label HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Text="{Binding Question}" TextColor="White"/>
</StackLayout>
<ListView ItemsSource="{Binding Suggestions}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="10">
<Frame>
<StackLayout HorizontalOptions="FillAndExpand">
<Label Text="{Binding Option}"/>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollView>
使用Dictionary以键值格式创建问答列表,然后将该列表绑定到xaml页面中(键作为问题变量的名称,值作为选项)。
<ListView ItemsSource="{Binding QuizList}"
HasUnevenRows="True"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}" >
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Text="{Binding Key}"
TextColor="Red"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Padding="10">
<StackLayout>
<Label Text="{Binding Option}"/>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>