hanaa hanaa - 3 years ago 222
C# Question

Reusable ViewCell with MVVMCross and Xamarin Forms Unhandled exception

I am using MVVMCross 5.0.6 and Xamarin.Forms. I am trying to create a reusable Xaml template, and inject it into my a main xaml View. But I keep having an "Unhandled exception" error.

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


Here is my code:

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

namespace ...Core.Pages
{
public partial class MemoPage
{
public MemoPage ()
{
InitializeComponent ();
}
}
}


MemoViewModel

namespace ...Core.ViewModels
{
public class MemoViewModel : MvxViewModel
{
IMvxNavigationService navigationService;

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


FirstMemoPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<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!"></Label>
</StackLayout>
</ViewCell>


FirstMemoPage.xaml.cs

namespace ...Core.Pages.Items
{
public partial class FirstMemoPage
{
public FirstMemoPage ()
{
InitializeComponent ();
}
}
}

Answer Source

You can't assign a Cell directly to the content of a Page.

So, either change your FirstMemoPage.xaml, to something like this:

<?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>

And change the FirstMemoPage class accordingly.

Or use the Cell only in a ListView or similar containers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download