Xamarin问答转盘视图将答案设置为空

eivnm1vs  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(170)

我正在我的应用程序中创建一个页面,让用户回答一系列问题。我首先创建了一个5项问题用于测试目的。答案可以是多项选择或字符串。我已经将问题存储在CarouselView中,将Position绑定到qPos。通过单击Next按钮,用户将继续下一个问题,使用Prev按钮,反之亦然。我已经成功地为对象列表中的每个问题添加了答案。但是,当有3个项目的差距时,答案被设置为空。
例如,我已经回答了问题1 - 4,现在是问题5。但是当我回到问题1时,答案是空的。为了更好地理解情况,我在下面附上了一个gif。

由于我已经到达问题#5,问题#2和#1的答案将重置为空。
下面是我的代码:

    • 型号**
public class QuestionsModel
    {
        public int QuestionNumber { get; set; }
        public string Question { get; set; }
        public string AnswerableBy { get; set; } //Determine if the answer is from choices or not
        public string Answer { get; set; } //Stores answer that are in string format
        public AnswerModel ChoiceAnswer { get; set; } //Stores answer that are from choices
        public ObservableRangeCollection<AnswerModel> Choices { get; set; } //List of choices if available
    }
    public class AnswerModel
    {
        public string Choice { get; set; }
    }
}
    • 视图模型**
public ObservableRangeCollection<QuestionsModel> questionList { get; set; } // Question List
public SetUpOwnerViewModel()
{
    //Populate Questions
    questionList = new ObservableRangeCollection<QuestionsModel>() { 
        new QuestionsModel()
        { 
            QuestionNumber = 1, 
            Question = "What is your preferred pet to adopt?", 
            AnswerableBy = "Choices",
            Choices = new ObservableRangeCollection<AnswerModel>()
            { 
                new AnswerModel(){Choice = "Dog"},
                new AnswerModel(){Choice = "Cat"},
                new AnswerModel(){Choice = "Both"}
            } 
        },
        new QuestionsModel()
        {
            QuestionNumber = 2,
            Question = "Have you adopted from a pet shelter before?",
            AnswerableBy = "Choices",
            Choices = new ObservableRangeCollection<AnswerModel>()
            {
                new AnswerModel(){Choice = "Yes"},
                new AnswerModel(){Choice = "No"}
            }
        },
        new QuestionsModel()
        {
            QuestionNumber = 3,
            Question = "How many children below 18 are present in the house?",
            AnswerableBy = "Numeric",
            Answer = ""
        },
        new QuestionsModel()
        {
            QuestionNumber = 4,
            Question = "How many other pets are in your house right now?",
            AnswerableBy = "Numeric",
            Answer = ""
        },
        new QuestionsModel()
        {
            QuestionNumber = 5,
            Question = "Who else do you live with?",
            AnswerableBy = "Choices",
            Answer = "",
            Choices = new ObservableRangeCollection<AnswerModel>()
            {
                new AnswerModel(){Choice = "Spouse"},
                new AnswerModel(){Choice = "Parents"},
                new AnswerModel(){Choice = "Roomate"},
                new AnswerModel(){Choice = "None"}
            }
        }
    };
    nextStageComm = new AsyncCommand(nextStage);
    nextQComm = new AsyncCommand(nextQ);
    prevQComm = new AsyncCommand(prevQ);
}
public ICommand nextStageComm { get; }
public ICommand nextQComm { get; }
public ICommand prevQComm { get; }
public ICommand setAnswerComm { get; }
private int _stage = 1; //Stage 1: Welcome view, Stage 2: Questions View
public int stage
{
    get => _stage;
    set => SetProperty(ref _stage, value);
}
private int _qPos = 0;
public int qPos
{
    get => _qPos;
    set => SetProperty(ref _qPos, value);
}
private async Task nextStage()
{
    if (_stage < 2)
        stage++;
}
private async Task nextQ()
{
    if (_qPos < 5)
        qPos++;
}
private async Task prevQ()
{
    if (_qPos > 0)
        qPos--;
}
    • 查看**
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodel="clr-namespace:PawAdopt_v5.ViewModels.StartUp"
             xmlns:model="clr-namespace:PawAdopt_v5.Models.Misc"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="PawAdopt_v5.Views.StartUp.SetUp.SetUpOwnerView"
             Shell.NavBarIsVisible="False">
    <ContentPage.BindingContext>
        <viewmodel:SetUpOwnerViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <AbsoluteLayout>
            <ContentView AbsoluteLayout.LayoutBounds="0.5,0.5,0.9,0.5"
                         AbsoluteLayout.LayoutFlags="All"
                         ><!--Add BG Color here-->
                <ContentView.Triggers>
                    <DataTrigger TargetType="ContentView"
                                 Binding="{Binding stage}"
                                 Value="1">
                        <Setter Property="Content">
                            <Setter.Value>
                                <AbsoluteLayout>
                                    <StackLayout AbsoluteLayout.LayoutBounds="0,0.3"
                                                 AbsoluteLayout.LayoutFlags="YProportional"
                                                 Spacing="20">
                                        <Label Text="Welcome"
                                               TextColor="{x:StaticResource PrimaryColor}"
                                               FontSize="30"
                                               FontAttributes="Bold"/>
                                        <Label Text="Thank you for using Paw-Adopt. Help us set up your account."
                                               TextColor="Black"
                                               FontSize="14"/>
                                    </StackLayout>
                                    <Button Text="Next"
                                            TextTransform="None"
                                            BackgroundColor="Transparent"
                                            BorderColor="{x:StaticResource SecondaryColor}"
                                            BorderWidth="2"
                                            CornerRadius="10"
                                            AbsoluteLayout.LayoutBounds="1,1"
                                            AbsoluteLayout.LayoutFlags="PositionProportional"
                                            Command="{Binding nextStageComm}"/>
                                </AbsoluteLayout>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    <DataTrigger TargetType="ContentView"
                                 Binding="{Binding stage}"
                                 Value="2">
                        <Setter Property="Content">
                            <Setter.Value>
                                <StackLayout HorizontalOptions="StartAndExpand">
                                    <CarouselView ItemsSource="{Binding questionList}"
                                                  IsSwipeEnabled="False"
                                                  Position="{Binding qPos}">
                                        <CarouselView.ItemTemplate>
                                            <DataTemplate x:DataType="model:QuestionsModel">
                                                <AbsoluteLayout>
                                                    <StackLayout>
                                                        <Label FontSize="30"
                                                               FontAttributes="Bold"
                                                               TextColor="{x:StaticResource PrimaryColor}">
                                                            <Label.FormattedText>
                                                                <FormattedString>
                                                                    <Span Text="#"/>
                                                                    <Span Text="{Binding QuestionNumber}"/>
                                                                </FormattedString>
                                                            </Label.FormattedText>
                                                        </Label>
                                                        <Label Text="{Binding Question}"
                                                               TextColor="Black"
                                                               FontSize="17"/>
                                                    </StackLayout>
                                                    <ContentView AbsoluteLayout.LayoutBounds="0,0.5,1,0.15"
                                                                 AbsoluteLayout.LayoutFlags="All">
                                                        <ContentView.Triggers>
                                                            <DataTrigger TargetType="ContentView"
                                                                         Binding="{Binding AnswerableBy}"
                                                                         Value="Choices">
                                                                <Setter Property="Content">
                                                                    <Setter.Value>
                                                                        <Picker ItemsSource="{Binding Choices}"
                                                                                ItemDisplayBinding="{Binding Choice}"
                                                                                SelectedItem="{Binding ChoiceAnswer}">
                                                                        </Picker>
                                                                    </Setter.Value>
                                                                </Setter>
                                                            </DataTrigger>
                                                            <DataTrigger TargetType="ContentView"
                                                                         Binding="{Binding AnswerableBy}"
                                                                         Value="Numeric">
                                                                <Setter Property="Content">
                                                                    <Setter.Value>
                                                                        <Entry Keyboard="Numeric"
                                                                               Text="{Binding Answer}"/>
                                                                    </Setter.Value>
                                                                </Setter>
                                                            </DataTrigger>
                                                        </ContentView.Triggers>
                                                    </ContentView>
                                                </AbsoluteLayout>
                                            </DataTemplate>
                                        </CarouselView.ItemTemplate>
                                    </CarouselView>
                                    <StackLayout Orientation="Horizontal"> 
                                        <Button Text="Prev"
                                            TextTransform="None"
                                            BackgroundColor="Transparent"
                                            BorderColor="{x:StaticResource SecondaryColor}"
                                            BorderWidth="2"
                                            CornerRadius="10"
                                            HorizontalOptions="StartAndExpand"
                                            Command="{Binding prevQComm}"/>
                                        <Button Text="Next"
                                            TextTransform="None"
                                            BackgroundColor="Transparent"
                                            BorderColor="{x:StaticResource SecondaryColor}"
                                            BorderWidth="2"
                                            CornerRadius="10"
                                            HorizontalOptions="End"
                                            Command="{Binding nextQComm}"/>
                                    </StackLayout>
                                </StackLayout>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </ContentView.Triggers>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

我意识到,通过使用断点,在3个项目的间隔之后,答案被重置为null。这真的是转盘视图的工作原理吗?还是我的方法是错误的?
提前感谢您的回答

btqmn9zl

btqmn9zl1#

我测试了你提供的代码,也使用了断点。只是你说:
我意识到,通过使用断点,在3项差距之后,答案将重置为空。
ContentView的属性Content似乎不太支持Picker。
但是,您可以使用它来满足您的需要:

    • 查看**
<DataTrigger TargetType="ContentView"
                Binding="{Binding AnswerableBy}"
                Value="Text">
           <Setter Property="Content">
                <Setter.Value>
                     <Entry Keyboard="Text"
                            Text="{Binding Answer}"/>
                </Setter.Value>
           </Setter>
   </DataTrigger>
    • 视图模型**
new QuestionsModel()
        {
            QuestionNumber = 1,
            Question = "What is your preferred pet to adopt?",
            AnswerableBy = "Text",
            Answer = ""
        }

希望能对你有所帮助。

相关问题