具有 MVVMCross 和 Xamarin Forms 的可重用 ViewCell 未处理的异常

Reusable ViewCell with MVVMCross and Xamarin Forms Unhandled exception

我正在使用 MVVMCross 5.0.6 和 Xamarin.Forms。我正在尝试创建一个可重复使用的 Xaml 模板,并将其注入到我的主 xaml 视图中。但是我一直有 "Unhandled exception" 错误。

FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: 

Exception has been thrown by the target of an invocation. ---> Xamarin.Forms.Xaml.XamlParseException: Position 8:10. Cannot assign property "Content": Property does not exists, or is not assignable, or mismatching type between value and property
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, Xamarin.Forms.Xaml.XmlName propertyName, System.Object value, System.Object rootElement, Xamarin.Forms.Xaml.INode node, Xamarin.Forms.Xaml.HydratationContext context, System.Xml.IXmlLineInfo lineInfo) [0x000de] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:310 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0023d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:139 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:175 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:225 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x0007a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:125 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x00046] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 
07-26 16:00:47.017 E/mono-rt (13364):   at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
07-26 16:00:47.017 E/mono-rt (13364):   at AmeliMemo.Core.Pages.MemoPage.InitializeComponent () [0x00001] in C:\Users\hamiri\documents\visual studio 2017\Projects\...\.Core.Pages.MemoPage.xaml.g.cs:20 
07-26 16:00:47.017 E/mono-rt (13364):   at ..Pages.MemoPage..ctor () [0x00008] in C:\Users..\visual studio 2017\Projects\..Core\Pages\MemoPage.xaml.cs:11 

这是我的代码:

MemoPage.xaml

<mvx:MvxContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:mvx="clr-namespace:MvvmCross.Forms.Core;assembly=MvvmCross.Forms" 
         x:Class="...Core.Pages.MemoPage"
         xmlns:views="clr-namespace:...Core.Pages.Items"
         Title="Memo">
    <views:FirstMemoPage/>

MemoPage.xaml.cs

public partial class MemoPage
{
    public MemoPage ()
    {
        InitializeComponent ();
    }
}

MemoViewModel

public class MemoViewModel : MvxViewModel
{
    IMvxNavigationService navigationService;

    public MemoViewModel(IMvxNavigationService navigationService)
    {
        this.navigationService = navigationService;
    }
}

第一MemoPage.xaml

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="...Core.Pages.Items.FirstMemoPage">
    <StackLayout>
        <Label Text="Hi!"/>
    </StackLayout>
</ViewCell>

第一MemoPage.xaml.cs

public partial class FirstMemoPage 
{
    public FirstMemoPage ()
    {
        InitializeComponent ();
    }
}

您不能将 Cell 直接分配给 Page 的内容。

因此,要么将您的 FirstMemoPage.xaml 更改为如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="...Core.Pages.Items.FirstMemoPage">
        <Label Text="Hi!"></Label>
</StackLayout>

并相应地更改 FirstMemoPage class。

或仅在 ListView 或类似容器中使用 Cell