无法在XAML中使用转换器

时间:2019-08-09 19:47:27

标签: c# xaml xamarin

我将在绑定中使用转换器... 我试图在Application.Resources中添加转换器,但是它说“验证您没有丢失程序集引用,并且所有引用的程序集都已构建”

App.xaml

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:converters="clr-namespace:MySchool.Converters;assembly=MySchool"
             mc:Ignorable="d"
             x:Class="MySchool.App">
    <Application.Resources>

        <converters:SubConverter x:Key="subInverter"/>
        <converters:CompConverter x:Key="compInverter"/>
    </Application.Resources>
</Application>

CompConverter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;

namespace MySchool.Converters
{
    public class CompConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value > (int)parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value)
                return parameter;

            //it's false, so don't bind it back
            throw new Exception("CompConverter: It's false, I won't bind back.");
        }
    }
}

SubConverter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;

namespace MySchool.Converters
{
    public class SubConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value - (int)parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value + (int)parameter;
        }
    }
}

News.xaml

<Label
    Text="{Binding attachmentsCount, ConverterParameter=1, Converter={StaticResource subInverter}, StringFormat='{0} Images...'}"
    IsVisible="{Binding attachmentsCount, ConverterParameter=1, Converter={StaticResource compInverter}}"
    TextColor="#c8c8c8"
    HorizontalTextAlignment="Center"
    VerticalTextAlignment="Center"
    AbsoluteLayout.LayoutFlags="PositionProportional"
    AbsoluteLayout.LayoutBounds="1,.9,80,40"
    BackgroundColor="#7F000000"/>

但是它说App.xaml中的编译错误。

  

未找到类型'converters:CompConverter'。确认您没有丢失程序集引用,并且所有引用的程序集均已构建。

我该怎么做?

0 个答案:

没有答案