Hello All,
I’m trying to create a survey within my main project and I’m having binding issues. The survey for the main project will have about 60 questions and there will be about 5 different ways to answer the questions. For example, there will be control types such as, DatePicker, Slider, Entry, Picker, and MultiSelect(Checkboxes). Through some research, I’ve been able to create 5 different DataTemplates for each control type and I’m using a CarouselView’s ItemTemplate to change each DataTemplate(Page). For convenience, I’ve created a small sample project that reads json from a text file which creates 6 questions. Each question is a page. When using the next & previous buttons, I’m keeping track of the current page with a property in the viewmodel. Also, I’m using a switch statement for the control types to assign values to the appropriate property in the codebehind. See small example below:
var page = _vm.SurveyPages.Where(p => p.PageNumber == _vm.CurrentPage).First();
switch (page.Question.ControlType) { case "entry": _vm.SelectedEntryAnswer.SelectedEntryAnswer = page.Question.SelectedEntryAnswer; break;
My problem is that my bindings lose their values when I go back using the previous button. For example, when I use the picker control to select “Male” or “Female”, go to next page and use the slider, go to next page and input text in the entry control, and click the next button. After that click the previous button until you get to the picker page again. You will notice that the selection you chose is no longer selected. When debugging, the last setter call to the property it’s bound to, the value is null, which is why the selection is no longer there. I cannot figure out why this is happening.
My goal is to for the pages to keep all the answers set on them even when moving to next or previous page. That way once the user finishes, I can submit the answers to the database. If anyone can help me figure out why my values are not persisting through the page navigation, it would be much appreciated. My other problem is I do not know how to access my controls in my datatemplates. I’ve tried solutions available such as FindVisualChildren, but I could not seem to get it to work. I’ve been on this for over 2 weeks now and I am at my wits end. I don’t want to write hacky code in order to move forward, because I believe there is a better way to achieve what I’m trying to do. I’m new to these forums, so if there’s anymore info needed please ask.
Thank you in advance!!!
P.S. This sample project uses Xamarin Forms, MVVM, and is tested using a Android Phone.