在 Syncfusion.SfDataGrid 中使用分页时出现“System.Reflection.TargetException”类型的异常

Posted

技术标签:

【中文标题】在 Syncfusion.SfDataGrid 中使用分页时出现“System.Reflection.TargetException”类型的异常【英文标题】:Exception of type 'System.Reflection.TargetException' when using Paging in Syncfusion.SfDataGrid 【发布时间】:2018-08-03 09:12:45 【问题描述】:

我尝试在 Xamarin 项目(.net 标准 2.0 作为包含视图的共享代码)中使用 SfDataGrid 和 paging。

android 项目在我的 Motorola G4 上的 Android 7.1.2 (LineageOS 14.1) 下运行良好。

但是 UWP 项目崩溃了。在 Visual Studio 2017 的调试输出中,我看到以下异常:

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()

所有项目include the Nuget package。任何想法为什么会失败?对于 Android 项目,我可以通过自定义 Behavior 类来设置 Grid 和 Pager 以及源,但在 UWP 中,我只看到这个异常,其中加载分页按钮的图标失败,但从不调用行为代码/调试器永远不会在OnAttachedTo 的断点处停止。

知道出了什么问题或如何解决吗?


// 编辑 2018-03-13

当我比较文件版本时,我看到Syncfusion.SfDataGrid.XForms.Android.dllSyncfusion.SfDataGrid.XForms.dll 的Android 版本16.1451.0.26。但对于 UWP,存在不匹配。 Syncfusion.SfDataGrid.XForms.UWP.dll 的版本为 16.1460.0.26,而 Syncfusion.SfDataGrid.XForms.dll 的版本为 16.1451.0.26。所以 6051 不匹配的接缝会导致 UWP 下的问题,这可以解释为什么 Android 应用程序可以工作,因为这里的 DLL 具有相同的版本。

有谁知道带有assemblyBinding 的 app.config 是否仍可在 UWP 中工作以解决此版本不匹配问题并强制将 DLL 与不同版本一起使用?


// 编辑 2018-04-04

我尝试对其进行更多调查,获得了 Syncfusion Xamarin 组件的源代码,将它们修改为使用最新的 Xamarin 版本 2.5.0.280555 Service Release 5,并将项目直接添加到我的解决方案中,但我仍然遇到同样的错误。

问题发生在directly when I follow the paging guide:

创建一个新的 SfDataPager 实例

这会直接触发异常:

var appearanceManager = new CustomAppearance();
var dataPager = new SfDataPager

    AppearanceManager = appearanceManager
;

原因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()

但在 CustomAppearance 类中,我重写了 GetIcons 方法(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;
    

在 Android 中一切正常。在这里,我可以逐步完成所有覆盖并获取我的自定义图像,并将它们显示在寻呼机中。在 UWP 项目中,我遇到了崩溃。

我也followed the steps which should be done in Release mode only,来加载程序集:

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);

因此,DLL Syncfusion.SfDataGrid.XForms.dllSyncfusion.SfDataGrid.XForms.UWP.dll 已加载,但从 Syncfusion.SfDataGrid.XForms.dll 加载图像失败:

我现在没有主意了。


【问题讨论】:

能否提供发生崩溃的代码? @sme 崩溃发生在 .g.cs 文件中的 InitializeComponent() 处。在这里,我可以单步执行来自使用的控件的所有条目,但是当我离开 InitializeComponent 方法时,异常处理程序会处理异常。 你试过这个代码sample. @NicoZhu-MSFT 这个演示没有使用 SfDataPager 所以它没有问题。 【参考方案1】:

// 编辑 2018-07-23

问题终于在16.2.0.42 中得到解决,他们现在添加了typeof(SfDataPager).GetTypeInfo().Assembly 调用。

将 Syncfusion.SfDataGrid.XForms 的源代码添加到我的解决方案后,我在Syncfusion.SfDataGrid.XForms.DataPager.Header.CreateHeaderContent() 处设置了一个断点。在这里创建图像:

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

这里调用了GetFirstPageIcon()。于是我跳进代码里看看

public virtual ImageSource GetFirstPageIcon()

     return ImageSource.FromResource("Syncfusion.SfDataGrid.XForms.Resources.FirstPageIcon.png");

ImageSource.FromResource 处,System.Reflection.TargetException 被提出。所以调用ImageSource.FromResource 失败,因为它转换为:

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

第二个参数默认为空。这是加载图像的程序集。

在UWP下,这会造成麻烦,可以通过改变方法来解决

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

现在 Xamarin 使用 Syncfusion DLL 来加载资源,而不是我的应用程序(不包含图像的 .net 标准共享代码库)。

这也是 Xamarin tracker/forum 中的其他用户发现的,但它是 not documented by Microsoft。

所以我必须等待 Syncfusion 的错误修复,他们会更改所有方法以将 typeof(SfDataPager).GetTypeInfo().Assembly 作为第二个参数包含在 ImageSource.FromResource

【讨论】:

【参考方案2】:

感谢您联系 Syncfusion 支持。

当示例和 SfDataGrid 程序集之间的 Xamarin NuGet 包不匹配时,通常会发生此错误。我们的 16.1.0.24 版本支持 Xamarin NuGet 版本 2.1。

Xamarin 建议内联 NuGet 包以避免潜在问题。因此,请卸载所有其他版本的 NuGet,并在 PCL 和示例的渲染器项目中使用 2.1 版本。还要确保在任何 PCL 和渲染器项目中都没有安装其他版本的 Xamarin NuGet。在运行之前清理并重建项目。您可以从我们的自述信息中了解 Syncfusion 控件的软件要求。

自述文件链接: http://files2.syncfusion.com/Installs/v16.1.0.24/ReadMe/Xamarin_Forms.html

问候, 苏里亚

【讨论】:

2.1 版是什么意思? 2.1 Xamarin.Forms?您的链接页面显示“Essential Studio for Xamarin 16.1.0.24 与 Xamarin.Forms 2.4.0.280 或更高版本兼容”。作为共享项目,我使用 .net 标准 2.0,而不是 PCL 有什么更新吗?我已经更新到 16.1.0.26 并且仍然看到问题。 .net 标准 2.0 dll 和 UWP 解决方案共享相同版本的 Grid 和 Xamarin.Forms。 16.1.0.26 中修复了什么? 嗨@magicandre1981,您能否确保您的应用程序中所有项目中的 Xamarin.Forms 版本是“2.4.0.280”? 我有posted what causes the issue。请将此修复添加到您的代码库中 @Harikrishnan 我是posted what causes the issue。请将此修复添加到您的代码库中

以上是关于在 Syncfusion.SfDataGrid 中使用分页时出现“System.Reflection.TargetException”类型的异常的主要内容,如果未能解决你的问题,请参考以下文章

在 Maven 中,如何在版本中命名战争文件与在快照构建中不同

存储在 plist 中的数据在模拟器中有效,但在设备中无效

在 C 中声明 == 在 C++ 中定义? [复制]

初始化发生在哪里,在 init 方法中还是在实例声明中?

查询在数据库中花费了更多时间,尽管在连接条件中使用了索引列,那么我们可以在代码中做些啥来优化

在 Athena 中,如何在结构中的数组中查询结构的成员?