无法在Xaml上设置嵌套类的属性

N. *_*ing 4 c# xaml android xamarin xamarin.forms

Xamarin给出的错误:在xmlns clr-namespace中找不到类型Foo:AmsterdamTheMapV3

问题是我正在尝试定义Foo,但无论我做什么,它都会起作用.我认为问题出在xmls:local命名空间内.也许我在代码中犯了一个愚蠢的错误.

这是一个用于windows,android和apple的Xamarin.forms项目.

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"
             x:Class="AmsterdamTheMapV3.CityGuide"
             xmlns:local="clr-namespace:AmsterdamTheMapV3">
  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
  <ScrollView Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  Spacing="0">

      <!-- Button1 -->
      <Image
          x:Name="CityGuideButton1"
          Source="shopping_gray.png"
          Aspect="Fill"
          HorizontalOptions="FillAndExpand"
                VerticalOptions ="FillAndExpand"
          local:Foo.Tag="button1">
        <Image.GestureRecognizers>
          <TapGestureRecognizer
            Tapped="Categorie_Onclick"
            NumberOfTapsRequired="1"/>
        </Image.GestureRecognizers>
      </Image>
    </StackLayout>
  </ScrollView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

Cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace AmsterdamTheMapV3
{
    public partial class CityGuide : ContentPage
    {
        public CityGuide()
        {
            InitializeComponent();
        }

         public class Foo
        {
            public static readonly BindableProperty TagProperty = BindableProperty.Create("Tag", typeof(string), typeof(Foo), null);

            public static string GetTag(BindableObject bindable)
            {
                return (string)bindable.GetValue(TagProperty);
            }

            public static void SetTag(BindableObject bindable, string value)
            {
                bindable.SetValue(TagProperty, value);
            }
        }


        async void Categorie_Onclick(Object sender, EventArgs args)
        {
            Image cmd = sender as Image;
            string txt = Foo.GetTag(cmd);
            await Navigation.PushAsync(new CategoriePage(txt));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望somewann可以帮助我.
如果需要,我可以提供更多信息.
先感谢您!

Ste*_*oix 10

您无法从中引用嵌套类Xaml.

你可以引用我这个,或谷歌,但这是一个普遍的事实.原因是Xaml Parsers无法根据点符号区分Attached(Bindable | Dependency)属性和嵌套类访问.

在您的情况下,将您的Foo类从CityGuide中直接移动到命名空间,它应该可以工作.

xmlns:local="clr-namespace:AmsterdamTheMapV3"没有该assembly=部分的xmlns声明是正确的,因为您指的是当前程序集.

有关类似答案的更多信息:https://stackoverflow.com/a/14546917/1063783

有些人可能会问为什么不将+符号用于嵌套类(就像在Reflection中完成的那样).问题是包含+符号的名称不是有效的xml元素名称(https://www.xml.com/pub/a/2001/07/25/namingparts.html)