【问题标题】:Xamarin.Forms.Xaml.XamlParseExceptionXamarin.Forms.Xaml.XamlParseException
【发布时间】:2018-04-19 18:07:28
【问题描述】:

我刚开始写代码。虽然是 Helloworld 代码,但出现此错误:xamarin.forms.xamlparseexception: position 12:13 cannot assign property "click": Property does not exist, or is not assignable, or mismatched type between value and property

我要调试王佩智能手表KW88-android 5.1的设备。

代码:

namespace HelloWorld 
{
    [global::Xamarin.Forms.Xaml.XamlFilePathAttribute("C:\\Users\\Gizem\\source\\repos\\HelloWorld\\HelloWorld\\HelloWorld\\MainPage.xaml")]
    public partial class MainPage : global::Xamarin.Forms.ContentPage {

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(MainPage));//Exception here!!!*****
        }
    }
}

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:local="clr-namespace:HelloWorld"
         x:Class="HelloWorld.MainPage">

<Label Text="Welcome to Xamarin.Forms!" 
       VerticalOptions="Center" 
       HorizontalOptions="Center" />
<Entry Placeholder="Write your name"/>
<Button Text="say hello"
        Click="Button_Click"/>

xaml.cs:

using System;
using Xamarin.Forms;

namespace HelloWorld
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        void Button_Click(object sender, EventArgs e)
        {

        }

    }
}

【问题讨论】:

    标签: c# xaml xamarin


    【解决方案1】:

    要绑定的属性名为Clicked,而不是Click。将您的代码更改为

    <Button Text="say hello"
            Clicked="Button_Click"/>
    

    它应该可以工作。

    编辑

    Alessandro Caliaro 提出了一个非常好的观点,即 ContentPage 只能包含一个元素。因此,您还需要将控件包装到容器中,例如 StackLayout:

    <StackLayout VerticalOptions="Center" >
        <Label Text="Welcome to Xamarin.Forms!" 
               HorizontalOptions="Center" />
        <Entry Placeholder="Write your name"/>
        <Button Text="say hello"
                Clicked="Button_Click"/>
    </StackLayout>
    

    【讨论】:

      【解决方案2】:

      我认为您应该将控件添加到布局中(如 StackLayout),否则可能会出现其他问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        相关资源
        最近更新 更多