代码之家  ›  专栏  ›  技术社区  ›  doxsi

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

  •  0
  • doxsi  · 技术社区  · 7 年前

    我试图从XAML文件访问方法类。

    添加到xaml内容页:

    xmlns:local="project.Utils"
    

    我试着用 myConverterMethod 在Utils文件夹中初始化,并将其用作:

    Converter={StaticResource myConverterMethod}
    

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

    我的错在哪里?

    2 回复  |  直到 7 年前
        1
  •  0
  •   hashimks    7 年前

    xmlns:local="clr-namespace:project.Utils;assembly=project"
    
        2
  •  0
  •   EvZ    7 年前

    不能引用 Method 在一个特定的类中,但是 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有一个很好的 documentation