在使用DataTemplateSelector填充列表视图时,我遇到了一个很奇怪的问题:
public class QuestionDataTemplateSelector : DataTemplateSelector
{
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
if (item == null)
{
return null;
}
var types = item.GetType().GetInterfaces().Where(w => w.GetGenericTypeDefinition() == typeof(IQuestionViewModel<>));
var type = types.First().GenericTypeArguments.First();
if (type == null)
{
throw new InvalidOperationException($"Could not extract IQuestionViewModel<> generic parameter from {item.GetType().FullName}");
}
if (DataTemplateCache.Contains(type))
{
return DataTemplateCache.Get(type);
}
if (!(IocContainer.Instance.Get(typeof(IQuestionView<>).MakeGenericType(type)) is ViewCell page))
{
throw new InvalidOperationException($"Could not create a view for type {type.FullName}");
}
var template = new DataTemplate(page.GetType());
DataTemplateCache.Add(type, template);
return template;
}
}
现在,这可以在三台不同的机器上的DEBUG模式下正常工作。但是,当我们尝试以发布模式或通过VSTS Build Pipelines中的签名APK运行它时,会得到:
InvalidOperationException: This operation is only valid on generic types.
at System.RuntimeType.GetGenericTypeDefinition() [0x00012] in <e12c8a98444d40b09ff73f6575530692>:0
at Spirit.Smartphone.Common.Inspections.Views.QuestionDataTemplateSelector+<>c.<OnSelectTemplate>b__0_0 (System.Type w)[0x00000] in <c2d5efcc31454b1989609d0a23094856>:0
at System.Linq.Enumerable+WhereArrayIterator`1[TSource].MoveNext ()[0x0002c] in <f40c30bd170444e4afebf14b88d6b816>:0
at System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Boolean& found) [0x00045] in <f40c30bd170444e4afebf14b88d6b816>:0
at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <f40c30bd170444e4afebf14b88d6b816>:0
at Spirit.Smartphone.Common.Inspections.Views.QuestionDataTemplateSelector.OnSelectTemplate(System.Object item, Xamarin.Forms.BindableObject container) [0x00034] in <c2d5efcc31454b1989609d0a23094856>:0
at Xamarin.Forms.DataTemplateSelector.SelectTemplate(System.Object item, Xamarin.Forms.BindableObject container) [0x00035] in <2213e65041254f04b27ae6e57d256a86>:0
at Xamarin.Forms.Platform.Android.ListViewAdapter.GetItemViewType(System.Int32 position) [0x000e7] in <81626d5b2a054a23bc21d192df515fa9>:0
at Android.Widget.BaseAdapter.n_GetItemViewType_I(System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position) [0x00008] in <86b02aaac0b44e61842dbba0cf915ee9>:0
at(wrapper dynamic-method) System.Object.392050fa-08f2-49d9-91d8-72e79027d071(intptr, intptr, int)
来自
var types = item.GetType().GetInterfaces().Where(w => w.GetGenericTypeDefinition() == typeof(IQuestionViewModel<>));
任何人都对为什么我们只能在RELEASE版本中看到它有任何想法?
谢谢。
编辑1: 具有“使用共享运行时”功能的内置发行版。也许,这是关于没有MonoRuntime的事情?
编辑2: 将链接器设置为“无”可停止崩溃。