不能在Xaml的嵌套类上设置属性
本文关键字:设置 属性 嵌套 Xaml 不能 | 更新日期: 2023-09-27 18:13:25
Xamarin返回的错误:Type Foo not found in xmlns clr-namespace:阿姆斯特丹themapv3
问题是我试图定义Foo,但无论我做什么,它都不起作用。我认为问题出在xml:local名称空间内部。也许我在代码中犯了一个愚蠢的错误。
这是Xamarin。windows, android和apple的表单项目。
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
的嵌套类
你可以引用我的话,或者谷歌一下,但这是一个普遍的真理。原因是Xaml解析器无法根据点符号区分Attached (Bindable|Dependency)属性和嵌套类访问。
在你的例子中,把你的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)
您缺少程序集引用。
xmlns:local="clr-namespace:AmsterdamTheMapV3; assembly=AmsterdamTheMapV3">
从Xamarin: 为了访问共享应用程序PCL的本地类,例如AppConstants, XAML程序员通常使用前缀local。命名空间声明必须指出CLR(公共语言运行时)命名空间名称(也称为。net命名空间名称,它是出现在c#命名空间定义或using指令中的名称)和包含代码的程序集。