0
votes

I try to use the SfDataGrid in a Xamarin project (.net standard 2.0 as shared code which includes the views) with paging.

The Android project works fine under Android 7.1.2 (LineageOS 14.1) on my Motorola G4.

But the UWP project crashes. In debug output of Visual Studio 2017 I see the following exception:

Exception thrown: 'System.Reflection.TargetException' in System.Private.CoreLib.dll
System.Reflection.TargetException: Exception of type 'System.Reflection.TargetException' was thrown.
   at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Xamarin.Forms.ImageSource.FromResource(String resource, Assembly sourceAssembly)
   at Syncfusion.SfDataGrid.XForms.DataPager.AppearanceManager.GetFirstPageIcon()
   at Syncfusion.SfDataGrid.XForms.DataPager.Header.CreateHeaderContent()
   at Syncfusion.SfDataGrid.XForms.DataPager.Header..ctor(SfDataPager pager)
   at Syncfusion.SfDataGrid.XForms.DataPager.SfDataPager..ctor()
   at Foo.BarPage.InitializeComponent()
   at Foo.BarPage..ctor()

All projects include the Nuget package. Any ideas why this fails? For the Android project I can step trough the custom Behavior class where I set the Grid and Pager and the source, but in UWP I only see this exception where loading of the icons for the paging buttons fails but the behavior code is never called/debugger never stops at breakpoint in OnAttachedTo.

Any idea what is wrong or how to fix it?


// Edit 2018-03-13

When I compare the file versions I see version 16.1451.0.26 for Android for Syncfusion.SfDataGrid.XForms.Android.dll and Syncfusion.SfDataGrid.XForms.dll. But for UWP, there is a mismatch. Syncfusion.SfDataGrid.XForms.UWP.dll has version 16.1460.0.26 while Syncfusion.SfDataGrid.XForms.dll has 16.1451.0.26. so the 60 to 51 mismatch seam to cause the issue under UWP and this would explain why the Android app works, because here the DLLs have same version.

Does anyone know if app.config with assemblyBinding still works in UWP to workaround this version mismatch and force using the DLLs together with different version?


// Edit 2018-04-04

I tried to investigate it more, got the source code for Syncfusion Xamarin components, modified them to use the last Xamarin version 2.5.0.280555 Service Release 5 and added the projects directly to my solution, but I still get the same error.

And the issue happens directly when I follow the paging guide:

Create a new SfDataPager instance

This triggers the exception directly:

var appearanceManager = new CustomAppearance();
var dataPager = new SfDataPager
{
    AppearanceManager = appearanceManager
};

causes System.Reflection.TargetException:

System.Reflection.TargetException: Exception of type 'System.Reflection.TargetException' was thrown.
   at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Xamarin.Forms.ImageSource.FromResource(String resource, Assembly sourceAssembly)
   at Syncfusion.SfDataGrid.XForms.DataPager.AppearanceManager.GetFirstPageIcon()
   at Syncfusion.SfDataGrid.XForms.DataPager.Header.CreateHeaderContent()
   at Syncfusion.SfDataGrid.XForms.DataPager.Header..ctor(SfDataPager pager)
   at Syncfusion.SfDataGrid.XForms.DataPager.SfDataPager..ctor()

but in CustomAppearance class I override the GetIcons methods (GetFirstPageIcon, GetLastPageIcon, GetNextPageIcon, GetPreviousPageIcon)

public class CustomAppearance : AppearanceManager
{
    public override ImageSource GetFirstPageIcon()
    {
        var image = ImageSource.FromResource("App.Resources.FirstPageIcon.png");
        return image;
    }

    public override ImageSource GetLastPageIcon()
    {
        var image = ImageSource.FromResource("App.Resources.LastPageIcon.png");
        return image;
    }

    public override ImageSource GetNextPageIcon()
    {
        var image = ImageSource.FromResource("App.Resources.NextPageIcon.png");
        return image;
    }

    public override ImageSource GetPreviousPageIcon()
    {
        var image = ImageSource.FromResource("App.Resources.PrevPageIcon.png");
        return image;
    }
}

and in Android everything works fine. Here I can step through all overrides and get my custom images and they show in the Pager. And in the UWP project I get the crash.

I also followed the steps which should be done in Release mode only, to load the assemblies:

List<Assembly> assembliesToInclude = new List<Assembly>();
//Now, add all the assemblies your app uses

assembliesToInclude.Add(typeof(SfDataGridRenderer).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(SfDataPager).GetTypeInfo().Assembly);

Xamarin.Forms.DependencyService.Register<SfDataGridRenderer>();
Xamarin.Forms.DependencyService.Register<SfDataPager>();

// replaces Xamarin.Forms.Forms.Init(e);
Xamarin.Forms.Forms.Init(e, assembliesToInclude);

So the DLLs Syncfusion.SfDataGrid.XForms.dll and Syncfusion.SfDataGrid.XForms.UWP.dll are loaded but loading the images from the Syncfusion.SfDataGrid.XForms.dll fails:

enter image description here

I'm now out of ideas.


2
Can you provide the code where the crash is occurring? - sme
@sme the crash happens at InitializeComponent() in a .g.cs file. Here I can step though all entries from the used controls, but when I leave the InitializeComponent method the exception handler handles the exception. - magicandre1981
Have you tried this code sample. - Nico Zhu - MSFT
@NicoZhu-MSFT this demo doesn't use the SfDataPager so it has no issues. - magicandre1981

2 Answers

1
votes

// Edit 2018-07-23

the issue is finally fixed in 16.2.0.42 and they now added the typeof(SfDataPager).GetTypeInfo().Assembly call.








After adding the sourcecode of Syncfusion.SfDataGrid.XForms to my solution I set a breakpoint at Syncfusion.SfDataGrid.XForms.DataPager.Header.CreateHeaderContent(). Here the image is created:

var image = new Image () {
    Source = this.Datapager.AppearanceManager.GetFirstPageIcon(),
    HorizontalOptions = LayoutOptions.Center,
    VerticalOptions = LayoutOptions.Center,
};

and here the GetFirstPageIcon() is called. So I jumped into the code and see

public virtual ImageSource GetFirstPageIcon()
{
     return ImageSource.FromResource("Syncfusion.SfDataGrid.XForms.Resources.FirstPageIcon.png");
}

and here at ImageSource.FromResource the System.Reflection.TargetException is raised. So the call ImageSource.FromResource fails, because it translate to this:

 public virtual ImageSource GetFirstPageIcon()
 {
      return ImageSource.FromResource("Syncfusion.SfDataGrid.XForms.Resources.FirstPageIcon.png", null);
 }

The 2nd parameter is by default null. This is the assembly where to load the image.

Under UWP, this causes trouble and can be fixed by changing the method to

 public virtual ImageSource GetFirstPageIcon()
 {
      return ImageSource.FromResource("Syncfusion.SfDataGrid.XForms.Resources.FirstPageIcon.png", typeof(SfDataPager).GetTypeInfo().Assembly);
 }

Now Xamarin uses the Syncfusion DLL to load the resource and not my app (.net standard shared code library which doesn't include the images).

This was also discovered by other users in the Xamarin tracker/forum, but is not documented by Microsoft.

So I have to wait for a bugfix by Syncfusion where they change all methods to include typeof(SfDataPager).GetTypeInfo().Assembly as 2nd parameter at ImageSource.FromResource.

0
votes

Thank you for contacting Syncfusion Support.

This error usually occurs when there is a mismatch of Xamarin NuGet packages between your sample and SfDataGrid assemblies. Our 16.1.0.24 version of release supports Xamarin NuGet of version 2.1.

Xamarin recommends the NuGet packages to be inline in order to avoid potential issues. So please uninstall all the other version of NuGets and use 2.1 version in the PCL and the renderer projects of your sample. Also ensure no other version of Xamarin NuGets were installed in any of your PCL and renderer projects. Clean and rebuild the project before running it. You can see the software requirements of Syncfusion controls from our read me information.

ReadMe link: http://files2.syncfusion.com/Installs/v16.1.0.24/ReadMe/Xamarin_Forms.html

Regards, Suriya