xamarin-demos icon indicating copy to clipboard operation
xamarin-demos copied to clipboard

CustomKanbanModel raises MethodAccessException from base class in the sample project

Open SunshineSpring666 opened this issue 5 years ago • 4 comments

Adding an item to ObservableCollection of CustomKanbanModel will raise Exception: System.MethodAccessException HResult=0x80131510 Message=Attempt by method 'SampleBrowser.SfKanban.CustomKanbanModel.set_Rating(Single)' to access method 'Syncfusion.SfKanban.XForms.KanbanModel.RaisePropertyChanged(System.String)' failed.

CustomKanbanModel from the sample code is defined as follows: public class CustomKanbanModel : KanbanModel { public float Rating { get; set; } ... }

Anything wrong here? Thanks.

SunshineSpring666 avatar Feb 19 '20 03:02 SunshineSpring666

Another situation: On Android devices, when adding an item to ObservableCollection of the base KanbanModel, it raises System.NullReferenceException: 'Object reference not set to an instance of an object.'

However, if the ObservableCollection does not bind to ItemsSource of Kanban, the exception won't occur. I guess if it's something within the KanbanModel (e.g., the ImageURL has to be a valid filename, even if I don't need the image in the original template) that might cause this problem? Same code works on UWP.

BTW, the data initialization of Kanban is done in the overwrited OnAppearing in code-behind. Appreciate your kind help. Thanks a lot.

SunshineSpring666 avatar Feb 19 '20 15:02 SunshineSpring666

Hi @SunshineSpring666

We have checked the reported issue in our SampleBrowser sample with some changes (as you mentioned - added Items inside the OnAppearing method). On that we were able to reproduce the crash issue since BindingContext of Sample is not set. The BindingContext of Sample has been set after the InitializeComponent() of Page. So, before that OnAppearing method invoked and getting null exception on provided model. It has been resolved by setting bindingContext before InitializeComponent as per in below code snippet

public partial class KanbanCustomizationSample : SampleView
    {
        KanbanCustomViewModel model;


        public override void OnAppearing()
        {
            base.OnAppearing();
            kanban.BindingContext = model;


            model.Cards = new System.Collections.ObjectModel.ObservableCollection<CustomKanbanModel>();


            model.Cards.Add(
                new CustomKanbanModel()
                {
                    ID = 1,
                    Title = "Margherita",
                    ImageURL = "margherita.png",
                    Category = "Menu",
                    Rating = 4,
                    Description = "The classic. Fresh tomatoes, garlic, olive oil, and basil. For pizza purists and minimalists only.",
                    Tags = new string[] { "Cheese" }
                }
                     );


            model.Cards.Add(
                new CustomKanbanModel()
                {
                    ID = 2,
                    Title = "Double Cheese",
                    ImageURL = "doublecheesemargherita.png",
                    Category = "Menu",
                    Rating = 5,
                    Description = "The minimalist classic with a double helping of cheese",
                    Tags = new string[] { "Cheese" }
                }
            );

           .....
            model.Cards.Add(
                new CustomKanbanModel()
                {
                    ID = 2,
                    Title = "Double Cheese",
                    ImageURL = "doublecheesemargherita.png",
                    Category = "Delivery",
                    OrderID = "Order ID - #16365",
                    AnimationDuration = 60000,
                    Description = "The minimalist classic with a double helping of cheese",
                    Tags = new string[] { "Cheese" }
                }
            );
        }

And don't forget to add the NotifyPropertyChanged for Model's Data to have a notified on ItemsSource property.

public class KanbanCustomViewModel : INotifyPropertyChanged 
  { 
      private ObservableCollection<CustomKanbanModel> cards; 

      public ObservableCollection<CustomKanbanModel> Cards 
      { 
          get 
          { 
              return cards; 
          } 
          set 
          { 
              if (cards != value) 
              { 
                  cards = value; 
                  RaisePropertyChanged(nameof(Cards)); 
              } 
          } 
      } 

      private void RaisePropertyChanged(string propertyName) 
      { 
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
      } 
} 

Please check and let us know if you have any concern.

mrhemalatha avatar Mar 02 '20 13:03 mrhemalatha

Sorry for belated reply. Actually I didn't adopt MVVM pattern in the project, so all the logic is in code-behind of the ContentPage. In this situation, the mentioned Exception occurs. I tried to set BindingContext in corresponding Xaml, but it didn't help.

I'm wondering if Kanban can function normally if no separate view model class is written. BTW, I used Fody.PropertyChanged to inject RaisePropertyChanged during compilation, so I didn't add the NotifyPropertyChanged explicitly. Many thanks.

SunshineSpring666 avatar Mar 05 '20 16:03 SunshineSpring666

Based on the official demo and above code, the exception still got raised when adding custom kanban model to the observable collection, even when the BindingContext was set. Thanks a lot.

System.MethodAccessException: 'Attempt by method 'BlankDemo.CustomKanbanModel.set_Rating(Single)' to access method 'Syncfusion.SfKanban.XForms.KanbanModel.RaisePropertyChanged(System.String)' failed.'

SunshineSpring666 avatar Oct 06 '20 07:10 SunshineSpring666