Dynamic views in a ContentPage

Hello, i'm new in Xamarin forms and i need to do a dynamic form questionary, depending on how many possible questions or answers i have.
I'm working with a PCL cross platform project , targeting iOS & Android.
I've seen that in this cases, they use MVVM. I've read a little and i tried to do my best to get this working but i couldn't.

What i need to achieve is this:

Label with the Question: (ex) Are you happy with xamarin?
Picker with possible answers: (ex) 1-No,2-yes,3-I love it [I will show only Name property, the value will be the property Answer]

For the picker i've used Xlabs.BindablePicker (https://github.com/XLabs/Xamarin-Forms-Labs)

Actually is not showing the picker also. But my biggest doubt is when i click on complete, how to retrieve the selected value of each answer.

I share you same code to explain myself better.

 public class Questionary : ContentPage
     {
        public Questionary ()
            {
            qVM= new QuestionViewModel();
                 this.BindingContext = qVM;
            var stack = new StackLayout();
            foreach (var question in qVM.Questions)
                    {
                        Label lblQuestion = new Label() { BindingContext = question };
                        lblQuestion.SetBinding(Label.TextProperty, "Question", BindingMode.TwoWay);

                        BindablePicker pckPossibleAnswers = new BindablePicker();
                        pckPossibleAnswers.BindingContext = qVM.Answers;
                        pckPossibleAnswers.SetBinding(BindablePicker.ItemsSourceProperty, "Name", BindingMode.TwoWay);

                        stack.Children.Add(lblQuestion);
                        stack.Children.Add(pckPossibleAnswers);
                    }
            btnComplete = new Button() { Text = "Complete", HorizontalOptions = LayoutOptions.End };>
                btnComplete.Clicked += BtnComplete_Clicked;
        stack.Children.Add(btnComplete);
                Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
            var scroll = new ScrollView() { Orientation = ScrollOrientation.Both };
            scroll.Content = stack;
            Content = scroll;
        }
    }


public class QuestionViewModel
{
    public ObservableCollection<QuestionVM> Questions { get; }
    public ObservableCollection<AnswerVM> Answers { get;}

    public QuestionViewModel()
    {
        using (var db = new QuestionaryDB())
        {
            Questions = new ObservableCollection<QuestionVM>();
            Answers = new ObservableCollection<AnswerVM>();
            List<Question> questionList= db.GetQuestions();
            foreach (var q in questionList)
            {
                QuestionVM vm = new QuestionVM(q.QuestionId,q.Question);
                Questions.Add(vm);
            }

            List<Answer> answerList = db.GetAnswers();
            foreach (var a in answerList)
            {
                AnswerVM vm = new AnswerVM(a.AnswerId, a.Name, a.Answer);
                Answers.Add(vm);
            }

        }
    }
}

public class QuestionVM : INotifyPropertyChanged
{
    public QuestionVM(Guid id, string question)
    {
        this.QuestionId = id;
        this.Question = question;
    }
private Guid questionId;
    public Guid QuestionId
    {
        set
        {
            if (questionId!= value)
            {
                questionId= value;
                OnPropertyChanged("QuestionId");
            }
        }
        get { return questionId; }
    }
private string question;
    public string Question
    {
        set
        {
            if (question!= value)
            {
                question= value;
                OnPropertyChanged("Question");
            }
        }
        get { return question; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
    }
}

public class AnswerVM:INotifyPropertyChanged
{
    public AnswerVM(Guid id, string name,int answer)
    {
        this.AnswerId = id;
        this.Name = name;
        this.Answer = answer;
    }
private Guid answerId;
    public Guid AnswerId
    {
        protected set
        {
            if (answerId!= value)
            {
                answerId= value;
                OnPropertyChanged("AnswerId");
            }
        }
        get { return answerId; }
    }
private string name;
    public string Name
    {
        protected set
        {
            if (name!= value)
            {
                name= value;
                OnPropertyChanged("Name");
            }
        }
        get { return name; }
    }
private int answer;
    public int Answer
    {
        protected set
        {
            if (answer != value)
            {
                answer= value;
                OnPropertyChanged("Answer");
            }
        }
        get { return answer; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
    }
}

Posts

Sign In or Register to comment.