I use a MvxSpinner to show country phone prefixes in a combobox in a MvvmCross for Xamarin app. I can bind to the ItemsSource property correctly, so I can see the list of my prefixes but when I assign the property in my view model that is bind to the SelectedItem property of the MvxSpinner, it won't work and will always show the first element in the list as the selected item.
The way I do it is the following. In my ViewModel I get the user data from the server and assign the properties for Country and PhonePrefix. Then I get the list of all countries and prefixes also from server and bind them to the list properties that are binded to the ItemSource properties of the respewctive MvxSpinners (simplified):
public string PhonePrefix { get; set; }
public string PhoneNumber { get; set; }
public Country Country { get; set; } = new Country();
public List<Country> Countries { get; set; } = new List<Country>();
public List<string> Prefixes { get; set; } = new List<string>();
private async Task GetUserData()
{
try
{
var userDataResult = await _registrationService.GetLoggedInUserData();
if (userDataResult != null)
{
if (!userDataResult.HTTPStatusCode.Equals(HttpStatusCode.OK))
{
Mvx.IoCProvider.Resolve<IUserDialogs>().Alert(userDataResult.Error?.Message);
}
else
{
PhonePrefix = userDataResult.user.country_code_phone;
PhoneNumber = userDataResult.user.phone;
Country.id = userDataResult.user.person.addresses[0].country_id;
Country.name = userDataResult.user.person.addresses[0].country_name;
}
}
}
catch (Exception e)
{
Log.Error<RegistrationViewModel>("GetUserData", e);
}
}
/// <summary>
/// Populates the view model properties for Countries and Prefixes with information retrieved from the server
/// </summary>
private void ProcessFormData()
{
if (_registrationFormData != null)
{
Countries = _registrationFormData.Countries?.ToList();
var userCountry = Countries?.Where(c => c.id == Country?.id).FirstOrDefault();
Country = IsUserLogedIn && Country != null ? userCountry : Countries?[0];
var prefixes = new List<string>();
if (Countries != null)
{
foreach (var country in Countries)
{
//spinner binding doesn't allow null values
if (country.phone_prefix != null)
{
prefixes.Add(country.phone_prefix);
}
}
}
//we need to assign the binded Prefixes at once otherwise the binding for the ItemSource fails
Prefixes = prefixes;
PhonePrefix = Prefixes.Count > 0 && !IsUserLogedIn && string.IsNullOrWhiteSpace(PhonePrefix) ? Prefixes?[0] : PhonePrefix;
}
}
And in the axml layout:
<MvxSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:id="@+id/spnrCountry"
local:MvxBind="ItemsSource Countries; SelectedItem Country"/>
<MvxSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:layout_marginBottom="10dp"
android:id="@+id/spnrPrefix"
local:MvxBind="ItemsSource Prefixes; SelectedItem PhonePrefix"/>
On the output window I can see the following error regarding MvxBinding:
(MvxBind) Null values not permitted in spinner SelectedItem binding currently
I debugged and I never have any Null values in the lists or in the properties I bind to the ItemSource and SelectedItem properties of the MvxSpinner.
Actually the Countries ItemSource and SelectedItem work properly so if user saved it's country to be Argentina, when I load it's data the selected item in the spinner will be Argentina. Note that I use a Country entity like that:
public class Country
{
public int id { get; set; }
public bool favorite { get; set; }
public string name { get; set; }
public string name_de { get; set; }
public string code { get; set; }
public int rzl_code { get; set; }
public string phone_prefix { get; set; }
public string updated_at { get; set; }
public string created_at { get; set; }
public override string ToString()
{
return name;
}
}
I also tried to make the phone prefix in it's own entity wrapping a string value but it didn't work either.
Does anybody knows what I'm doing wrong? Why for the Countries it's working and for the prefixes not?
I use PropertyChanged.Fody.
Thanks!