【问题标题】:Can't set properties on nested classes from Xaml无法从 Xaml 设置嵌套类的属性
【发布时间】:2016-10-18 08:44:53
【问题描述】:

Xamarin 返回的错误:Type Foo not found in xmlns clr-namespace: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>

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

我希望有人能帮助我。
如果需要,我可以提供更多信息。
提前谢谢你!

【问题讨论】:

  • 请注意,您的 Xaml 可能不会显示您所期望的内容。您的 contentPage 有 2 个孩子,一个 Label 和一个 ScrollView,但 ContentPage 只接受一个属性为 Content。在撰写本文时,Xamarin.Forms Xaml 解析器非常宽容,忽略了 2 项中的一项(通常除了最后一项),但将来可能会改变并抛出。
  • 我在上传此问题后确实更改了它。标签不应该在那里。但仍然感谢您的反馈。我会记住这一点。

标签: c# android xaml xamarin xamarin.forms


【解决方案1】:

您不能从 Xaml 引用嵌套类。

你可以引用我的话,或者用谷歌搜索,但这是一个普遍的真理。原因是 Xaml 解析器无法根据点号区分 Attached (Bindable|Dependency)Property 和嵌套类访问。

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

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

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

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

【讨论】:

    【解决方案2】:

    您缺少程序集参考。

    xmlns:local="clr-namespace:AmsterdamTheMapV3; assembly=AmsterdamTheMapV3">
    

    来自 Xamarin:

    要访问共享应用程序 PCL 的本地类,例如 AppConstants,XAML 程序员通常使用前缀本地。命名空间声明必须指明 CLR(公共语言运行时)命名空间名称(也称为 .NET 命名空间名称,即出现在 C# 命名空间定义或 using 指令中的名称)和包含代码的程序集。

    【讨论】:

    • @N.Smeding 检查项目文件中的程序集名称,它们应该匹配。我以为它是 AmsterdamTheMapV3 但是,转到您的 ProjectFile.csproj,找到程序集名称,并将其替换为 assembly=
    • 我在哪里可以找到程序集名称在属性中找不到它。
    • 程序集名称相同 RootNamespace:AmsterdamTheMapV3 程序集名称:AmsterdamTheMapV3
    猜你喜欢
    • 2012-06-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多