Bit*_*Zar 8 c# xamarin.forms uwp
我面对一些棘手的错误,而试图使用Xamarin实施新的CollectionView功能窗体4.在Android
它使实验性的功能在MainActivity.cs与后效果非常好项目:
global::Xamarin.Forms.Forms.SetFlags(new[] { "CollectionView_Experimental", "Shell_Experimental" });
Run Code Online (Sandbox Code Playgroud)
但是xamarin
文档没有提供有关 UWP 项目的任何信息,因此当我首先尝试编译 UWP 项目时,它会在尝试呈现使用 CollectionView 的页面时向我抛出此异常
System.InvalidOperationException:
'您尝试使用的类、属性或方法 ('VerifyCollectionViewFlagEnabled') 是 CollectionView 的一部分;要使用它,您必须在调用 Forms.Init() 之前通过调用 Forms.SetFlags("CollectionView_Experimental") 来选择加入。
所以我尝试在调用 InitializeComponent() 方法之前调用 UWP 项目中 App.xaml.cs 中的 SetFlags。所以这次它在尝试加载包含CollectionView
以下内容的页面时向我抛出此异常:
System.NullReferenceException: '未将对象引用设置为对象的实例。'
所以就像在这个例子中:
await Navigation.PushAsync(new PageWithCollectionView());
Run Code Online (Sandbox Code Playgroud)
成功执行 PageWithCollectionView 构造函数后抛出异常。
有人可以帮我解决这个问题吗?
更新
好的,所以在 UWP 项目的 App.xaml.cs 中添加 SetFlags 工作正常,并且 CollectionView 被正确初始化。但是 NRE 仍然存在(在 Android 上,CollectionView 工作没有问题),在试图摆脱这个问题时,我注意到它是在我尝试以这种方式嵌套 XAML 布局时引起的:
global::Xamarin.Forms.Forms.SetFlags(new[] { "CollectionView_Experimental", "Shell_Experimental" });
Run Code Online (Sandbox Code Playgroud)
如果我从 CollectionView.ItemTemplate 中删除所有 DataTemplate,只需将其留空,如下所示:
await Navigation.PushAsync(new PageWithCollectionView());
Run Code Online (Sandbox Code Playgroud)
页面被渲染为CollectionView
显示布局凌乱的 ItemSource 元素(没有边距和填充,并调用ToString()
元素的方法以在单元格内可视化它)。
[更新]
更新到Xamarin Forms
4 pre 8 后,异常消失了。
我创建了一个代码示例,其中包含CollectionView
. 根据文档,我们需要在 App.xaml.cs 文件中调用SetFlag
before Xamarin.Forms.Forms.Init(e)
,如下所示。
........
Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
Xamarin.Forms.Forms.Init(e);
........
Run Code Online (Sandbox Code Playgroud)
实现集合视图
<CollectionView>
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</CollectionView.ItemsSource>
</CollectionView>
Run Code Online (Sandbox Code Playgroud)
它运行良好,对于NullReferenceException
问题,您需要检查后面的代码中是否有空对象。CollectionView 的简单实现不会导致这样的错误。
更新
请尝试将Xamarin Forms 4更新到最新的预版本。
归档时间: |
|
查看次数: |
6269 次 |
最近记录: |