2017-07-26 52 views
1

我正在使用MVVMCross 5.0.6和Xamarin.Forms。我正在嘗試創建一個可重用的Xaml模板,並將其注入到我的主xaml視圖中。但我一直有一個「未處理的異常」錯誤。MVVMCross和Xamarin Forms的可重用ViewCell未處理的異常

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

FirstMemoPage.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> 

FirstMemoPage.xaml.cs

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

回答

1

您不能直接分配給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類。

或僅在ListView或類似容器中使用Cell

+0

非常感謝!這工作..如果我想要綁定FirstMemoPage內的一些變量,並且此變量從視圖更改爲另一個。你有一個想法如何做到這一點?非常感謝 – hanaa

+1

您將不得不在控件中創建並公開自己的可綁定屬性。谷歌'可重複使用的Xamarin控制'你會發現一些例子! –