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>
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));
}
}
}
我希望somewann可以帮助我 如果需要,我可以提供更多信息 提前谢谢你!
答案 0 :(得分:10)
您无法从Xaml
引用嵌套类。
你可以引用我这个,或谷歌,但这是一个普遍的事实。原因是Xaml Parsers无法根据点符号区分Attached(Bindable | Dependency)属性和嵌套类访问。
在您的情况下,将您的Foo类从CityGuide中直接移动到命名空间,它应该可以工作。
您的xmlns声明xmlns:local="clr-namespace:AmsterdamTheMapV3"
在没有assembly=
部分时是正确的,因为您指的是当前的程序集。
有关类似答案的更多信息:https://stackoverflow.com/a/14546917/1063783
有些人可能会问为什么不将+
符号用于嵌套类(就像在Reflection中完成的那样)。问题是包含+
符号的名称不是有效的xml元素名称(https://www.xml.com/pub/a/2001/07/25/namingparts.html)
答案 1 :(得分:-1)
您缺少装配参考。
xmlns:local="clr-namespace:AmsterdamTheMapV3; assembly=AmsterdamTheMapV3">
来自Xamarin:
要访问共享应用程序PCL的本地类,例如AppConstants,XAML程序员通常使用前缀local。名称空间声明必须指明CLR(公共语言运行时)名称空间名称(也称为.NET名称空间名称,它是出现在C#名称空间定义或using指令中的名称)和包含代码的程序集。