We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Serious failure in ItemsSource property when you have a constructor in ModelView with parameters on Android only.

When binding the ItemsSource in the listview or dataform it just doesn't load on Android when my ViewModel has parameters in the constructor.
It took me many days to figure out the error since the failure does not occur on the UWP platform.
I noticed that this affects all syncfusion controls when I use ItemsSource.

In Constructor ContentPage:
...
InitializeComponent();
BindingContext = new ViewModel.PrincipalViewModel(this);
...

In ModelView:

public class PrincipalViewModel
{
     readonly ContentPage _page;
     public ObservableCollection<<>> Items { get; set; }     // Not Working in Android Platform Only

     public class ModelView(ContentPage page)  // Not Working in Android Platform Only
     {
          _page = page;
     }
}

In XAML:

syncfusion:SfListView x:Name="listView" ItemSize="60"
                                                   ItemsSource="{Binding Items}"/

Greetings from Brazil.


3 Replies

GP Gnana Priya Namasivayam Syncfusion Team January 3, 2020 07:08 AM UTC

Hi Guilherme, 
 
Thanks for using Syncfusion products. 
 
We are not clear with the reported query “When ViewModel has a parameter it affects all syncfusion controls when I use ItemsSource.” and provided code snippets are not clear to understand your requirement exactly. Can you please share your use case with the viewmodel parameter and your requirement in detail to validate your query at our end. 
 
Regards,  
Gnana Priya N


GU Guilherme January 8, 2020 12:48 PM UTC

Simply the ItemsSource properties of any control does not work with MVVM when it has parameterized constructor.

Take the test and you'll find that on the UWP platform it works normally, but on Android it doesn't work.


GP Gnana Priya Namasivayam Syncfusion Team January 9, 2020 12:30 PM UTC

Hi Guilherme, 
 
We have checked by passing parameter to our viewmodel as mentioned in our sample using latest ListView 17.4.0.41 and Xamarin Forms 4.4 version. We have checked the reported issue in all platforms like UWP, iOS & Android but the reported issue did not occur in our sample in Android. We have attached the tested sample for your reference, please find them from following link. 
 
 
 
Model.cs 
public class Contacts : INotifyPropertyChanged 
{ 
   public Contacts(string name, string number) 
   { 
      contactName = name; 
      contactNumber = number; 
      displayString = name[0].ToString(); 
   } 
} 
 
ViewModel.cs 
 
public class MainPageViewModel  
{ 
    public MainPageViewModel( ContentPage page) 
    { 
        ContactsInfo = new ObservableCollection<Contacts>(); 
        foreach (var cusName in CustomerNames) 
        { 
            var contact = new Contacts(cusName, r.Next(720, 799).ToString() + " - " + r.Next(3010, 3999).ToString()); 
            contact.ContactImage = ImageSource.FromResource("ListViewPrism.Images.Image" + r.Next(0, 28) + ".png", assembly); 
            contact.ContactColor = Color.FromRgb(r.Next(40, 255), r.Next(40, 255), r.Next(40, 255)); 
            ContactsInfo.Add(contact); 
        } 
     } 
   } 
} 
MainPage.xaml.cs 
 
public partial class MainPage : ContentPage 
{ 
   MainPageViewModel viewModel; 
   public MainPage() 
   { 
      InitializeComponent(); 
      viewModel = new MainPageViewModel(this); 
      this.BindingContext = viewModel; 
   } 
} 
 
 
 
Can you please check above sample whether you are getting reported issue in our sample also. If not, revert us back with the issue replicated sample so that we can analyze and provide you better solution at our end. 
 
Regards, 
Gnana Priya N 


Loader.
Live Chat Icon For mobile
Up arrow icon