在 XAML 命名空间上找不到类型



我正在尝试从 XAML 文件访问方法类。

我的类在文件夹:项目上。实用工具。

在 XAML 内容页上添加:

xmlns:local="project.Utils"

我尝试在 Utils 文件夹中使用myConverterMethod类并将其用作:

Converter={StaticResource myConverterMethod}

但是error Type myConverterMethod not found in xmlns project.Utils.

我的错在哪里?

您可以使用

xmlns:local="clr-namespace:project.Utils;assembly=project"

不能引用特定类中的Method,而可以引用IValueConverter

为了实现你想要的,你需要定义一个实现IValueConverter的类:

public class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value != 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1 : 0;
}
}

在可访问的范围内定义创建的转换器:页面/视图或应用程序。我所说的范围是指资源:

<ContentPage.Resources>
<ResourceDictionary>
<local:IntToBoolConverter x:Key="intToBool" />
</ResourceDictionary>
</ContentPage.Resources>

最后以下一种方式消耗转换器:

<Button Text="Search"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
IsEnabled="{Binding Source={x:Reference entry1},
Path=Text.Length,
Converter={StaticResource intToBool}}" />

Xamarin有一个非常好的文档,可以回答你的所有问题,它通常有一个很好的代码示例。

相关内容

最新更新