Xamarin.Forms.Xaml.XamlParseException:位置4:8。无法分配属性"内容":

时间:2018-04-01 17:18:41

标签: c# xamarin xamarin.forms

我使用Xamarin非常新。我想将Microcharts实现到Master Detail xamarin表单应用程序的项目详细信息页面中。我想我已经将错误隔离到了两个类中,但我似乎无法解决它。我很欣赏任何有关如何修复它的见解。

ItemDetailPage.xaml

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:forms="clr-namespace:Microcharts.Forms;assembly=Microcharts.Forms" 
             x:Class="TestApp.ItemDetailPage" 
             Title="{Binding Title}">
    <DataTemplate>
        <ViewCell> 
            <TextCell Text="{Binding Item.Text}" 
                      TextColor="Black"/>
            <TextCell Text="{Binding Item.Info}" 
                      TextColor="Black"/>
        </ViewCell> 
    </DataTemplate>
    <forms:ChartView x:Name="Chart1" 
                     HeightRequest = "150"/>
</ContentPage>

ItemDetailPage.xaml.cs

using System; 
using Xamarin.Forms;
using System.Collections.Generic;
using Microcharts; 
using Entry = Microcharts.Entry;
using SkiaSharp; 

namespace TestApp
{
    public partial class ItemDetailPage : ContentPage
    {
        List<Microcharts.Entry> entries = new List<Microcharts.Entry>
        {
               new Entry(200)
                {
                    Label = "January",
                    ValueLabel = "200",
                    Color = SKColor.Parse("#266489")
                },
                new Entry(400)
                {
                    Label = "February",
                    ValueLabel = "400",
                    Color = SKColor.Parse("#68B9C0")
                },
                new Entry(-100)
                {
                    Label = "March",
                    ValueLabel = "-100",
                    Color = SKColor.Parse("#90D585")
                }
        };

        ItemDetailViewModel viewModel;

        // Note - The Xamarin.Forms Previewer requires a default, parameterless constructor to render a page.
        public ItemDetailPage()
        {
            InitializeComponent();

            var item = new Item
            {
                Text = "Item 1",
                Description = "This is an item description."
            };

            viewModel = new ItemDetailViewModel(item);
            BindingContext = viewModel;
        }

        public ItemDetailPage(ItemDetailViewModel viewModel)
        {
            InitializeComponent();

            BindingContext = this.viewModel = viewModel;
        }
    }
}

ItemPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestApp
{
    public partial class ItemsPage : ContentPage
    {
        ItemsViewModel viewModel;

        public ItemsPage()
        {
            InitializeComponent();

            BindingContext = viewModel = new ItemsViewModel();
        }

        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var item = args.SelectedItem as Item;
            if (item == null)
                return;

            await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));

            // Manually deselect item
            ItemsListView.SelectedItem = null;
        }

        async void AddItem_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new NewItemPage());
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            if (viewModel.Items.Count == 0)
                viewModel.LoadItemsCommand.Execute(null);
        }
    }
}

只有在单击列表视图页面上的任何项目时才会出现错误。

如果有帮助,这是完整的错误消息:

  

Xamarin.Forms.Xaml.XamlParseException:位置4:8。无法分配属性&#34;内容&#34;:属性不存在,或者不可分配,或者值和属性之间不匹配的类型     在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。 D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ ApplyPropertiesVisitor.cs中的HydrationContext上下文,System.Xml.IXmlLineInfo lineInfo)[0x000ee]:345     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ ApplyPropertiesVisitor中的Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(Xamarin.Forms.Xaml.ElementNode节点,Xamarin.Forms.Xaml.INode parentNode)[0x00338]。 CS:134     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ XamlNode中的Xamarin.Forms.Xaml.ElementNode.Accept(Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode parentNode)[0x000ab]。 CS:149     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ XamlNode中的Xamarin.Forms.Xaml.RootNode.Accept(Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode parentNode)[0x00078]。 CS:203     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ XamlLoader中的Xamarin.Forms.Xaml.XamlLoader.Visit(Xamarin.Forms.Xaml.RootNode rootnode,Xamarin.Forms.Xaml.HydrationContext visitorContext)[0x0007b]。 CS:141     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ XamlLoader.cs中的Xamarin.Forms.Xaml.XamlLoader.Load(System.Object视图,System.String xaml)[0x0004b]:89     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ XamlLoader.cs中的Xamarin.Forms.Xaml.XamlLoader.Load(System.Object视图,System.Type callingType)[0x0002f]:68     在D:\ agent_work \ 1 \ s \ Xamarin.Forms.Xaml \ ViewExtensions.cs中的Xamarin.Forms.Xaml.Extensions.LoadFromXaml [TXaml](TXaml视图,System.Type callingType)[0x00000]     在/Users/rehamathur/Projects/TestApp/iOS/obj/iPhoneSimulator/Debug/TestApp.iOS.Views.ItemDetailPage.xaml.g.cs:22中的TestApp.ItemDetailPage.InitializeComponent()[0x00001]     在TestApp.ItemDetailPage..ctor(TestApp.ItemDetailViewModel viewModel)[0x000c1] in /Users/rehamathur/Projects/TestApp/TestApp/Views/ItemDetailPage.xaml.cs:57     在TestApp.ItemsPage + d__2.MoveNext()[0x00037] in /Users/rehamathur/Projects/TestApp/TestApp/Views/ItemsPage.xaml.cs:26     在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]中/exceptionservicescommon.cs:152     在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.4/src/mono/mcs/class中的System.Runtime.CompilerServices.AsyncMethodBuilderCore +&lt;&gt; c.b__6_0(System.Object state)[0x00000] /referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018     在UIKit.UIKitSynchronizationContext + c__AnonStorey0。&lt;&gt; m__0()[0x00000] /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24     在Foundation.NSAsyncActionDispatcher.Apply()[0x00000] /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/Foundation/NSAction.cs:163     at at(wrapper managed-to-native)UIKit.UIApplication:UIApplicationMain(int,string [],intptr,intptr)     在UIKit.UIApplication.Main(System.String [] args,System.IntPtr principal,System.IntPtr委托)[0x00005]在/ Users / builder / data / lanes / 5665 / db807ec9 / source / xamarin-macios / src / UIKit中/UIApplication.cs:79     在/ Users / builder / data / lanes / 5665 / db807ec9 / source / xamarin-macios / src / UIKit中的UIKit.UIApplication.Main(System.String [] args,System.String principalClassName,System.String delegateClassName)[0x00038] /UIApplication.cs:63     在TestApp.iOS.Application.Main(System.String [] args)[0x00001] /Users/xxxxxxxx/Projects/TestApp/iOS/Main.cs:17

1 个答案:

答案 0 :(得分:0)

正如@Jason所说,ViewCellContentPage本身只能有一个子项隐式设置为Content属性值。

很难准确说出你想要什么,一旦你的代码断断续续,作为一个流离失所的DataTemplate并绑定到你的代码示例中未显示的属性(作为Item.InfoItem.Text)。

DataTemplate代码应该就像这个选项之一(但我真的不能说你会把它放在哪里,但是肯定不是那样的细节页面):

1 - 使用ViewCell StackLayout

<DataTemplate>
    <ViewCell> 
        <StackLayout>
            <Label Text="{Binding Item.Text}" 
                   TextColor="Black"/>
            <Label Text="{Binding Item.Info}" 
                   TextColor="Black"/>
        </StackLayout>
    </ViewCell> 
</DataTemplate>

2 - 使用TextCell

<DataTemplate>
    <TextCell Text="{Binding Item.Text}" 
              Detail="{Binding Item.Info}" 
              TextColor="Black" 
              DetailColor="Black"/>
</DataTemplate>

关于你ContentPage,根元素也必须是单一的。像这样:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:forms="clr-namespace:Microcharts.Forms;assembly=Microcharts.Forms" 
             x:Class="TestApp.ItemDetailPage" 
             Title="{Binding Title}">
    <StackLayout>
        <Label Text="Any Title"/>
        <!-- Some other components -->
        <forms:ChartView x:Name="Chart1" 
                         HeightRequest = "150"/>
    </StackLayout>
</ContentPage>