MVVM新手在这里。当我尝试在MVVM中绑定数组ChoiceText[]时,初始值显示在我的视图中,但更新后的值不显示。
public partial class MainViewModel : ObservableObject
{
private int _questionNumber = 0;
private int _numberOfQuestions = 30;
MainPage.quizQuestion _question = new MainPage.quizQuestion();
[ObservableProperty]
string _answerText = "no answer yet...";
[ObservableProperty]
string _questionText = "what's this a picture of?";
[ObservableProperty]
string _imagePath = "pic1_72dpi.jpg";
[ObservableProperty]
string[] _choiceText = {"1", "2", "3", "4"};
private void LoadQuestion()
{
_question = LoadHardCodedQuestion(_questionNumber);
QuestionText = _question.QText;
AnswerText = string.Empty;
ImagePath = _question.imagePath;
for (int i = 0; i < _question.choices.Length; i++) { ChoiceText[i] = _question.choices[i]; }
}
}
在我的.xaml视图代码隐藏中:
<Image Grid.Row="0" Grid.ColumnSpan="2"
Source="{Binding ImagePath}"
BackgroundColor="Black"/>
<Label x:Name ="lblQuestion"
Grid.Row="1"
Text="{Binding QuestionText}"
FontSize="18"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Button Text="{Binding ChoiceText[0]}"
Grid.Row="2" Grid.ColumnSpan="2"
Style="{StaticResource defaultButtonStyle}"
Command="{Binding SubmitAnswerCommand}" CommandParameter="1"/>
<Button Text="{Binding ChoiceText[1]}"
Grid.Row="3" Grid.ColumnSpan="2"
Style="{StaticResource defaultButtonStyle}"
Command="{Binding SubmitAnswerCommand}" CommandParameter="2"/>
<Button Text="{Binding ChoiceText[2]}"
Grid.Row="4" Grid.ColumnSpan="2"
Style="{StaticResource defaultButtonStyle}"
Command="{Binding SubmitAnswerCommand}" CommandParameter="3"/>
<Button Text="{Binding ChoiceText[3]}"
Grid.Row="5" Grid.ColumnSpan="2"
Style="{StaticResource defaultButtonStyle}"
Command="{Binding SubmitAnswerCommand}" CommandParameter="4"/>
<Label x:Name ="lblAnswer" IsVisible="True"
Grid.Row="6"
Text="{Binding AnswerText}"
FontSize="18"
VerticalOptions="Start"
HorizontalOptions="Start"/>
其他可观察属性在视图中更新,没有问题。
我可以将ChoiceText[]定义为4个唯一变量,ChoiceText 1,ChoiceText 2等,但我希望能够绑定这个数组和可能更大的数组。我没绑好吗?
谢谢!abedaun
1条答案
按热度按时间fnx2tebb1#
感谢您发送编修。
我在ObservableArray的社区工具包中找不到任何东西,但ObservableCollection看起来确实可以工作,尽管有点不同。
Rufo爵士的回答对我来说非常有效--如果我赋值ChoiceText = _question.choices;_choiceText observable属性被更新,* 每个数组元素 * 也被更新。太好了,谢谢你,鲁弗爵士!
阿贝道恩