本文介绍了XAML 中的类属性声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XAML 命名空间声明感到困惑.请帮助澄清我的情况.我有类 BooleanToObjectConverter : CoTraveller 命名空间中的 IValueConverter:

I am confused in XAML namespace declaration.Please help to clarify my case. I have class BooleanToObjectConverter : IValueConverter in CoTraveller namespace:

public class EmailValidatorBehavior : Behavior<Entry>
{
    const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
        @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";

static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false);
public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

static readonly BindablePropertyKey IsVisiblePropertyKey = BindableProperty.CreateReadOnly("IsVisible", typeof(bool), typeof(EmailValidatorBehavior), false);
public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty;

public bool IsValid
{
    get { return (bool)base.GetValue(IsValidProperty); }
    private set { base.SetValue(IsValidPropertyKey, value); }
}

public bool IsVisible
{
    get { return (bool)base.GetValue(IsVisibleProperty); }
    private set { base.SetValue(IsVisiblePropertyKey, value); }
}

protected override void OnAttachedTo(Entry entry)
{
    entry.TextChanged += OnEntryTextChanged;
    base.OnAttachedTo(entry);
}

protected override void OnDetachingFrom(Entry entry)
{
    entry.TextChanged -= OnEntryTextChanged;
    base.OnDetachingFrom(entry);
}

void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
    if (e.NewTextValue.Length > 0)
    {
        IsVisible = true;
        Entry entry = (Entry)sender;
        IsValid = Regex.IsMatch(e.NewTextValue, digitRegex);

        if (IsValid) // Check only if we have a valid email
        {
            // Here we validate if the email contains our requirements
            String email = entry.Text;
            int pos = email.IndexOf("@"); // Exclude the domain
            string username = email.Substring(0, pos);
            if (username.Contains(FRIEND) || username.Contains(FRIENDS))
            {
                IsValid = true;
            }
            else
                IsValid = false;
        }
    }
    else
        IsVisible = false;
}

public class BooleanToObjectConverter<Image> : IValueConverter
{
    public Image FalseObject { set; get; }

    public Image TrueObject { set; get; }

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return (bool)value ? this.TrueObject : this.FalseObject;
    }

    //public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    //{
    //    throw new NotImplementedException();
    //}

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        return ((Image)value).Equals(this.TrueObject);
    }

    //public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    //{
    //    throw new NotImplementedException();
    //}
}

}

我想在应用资源中使用 FalseObject 和 TrueObject 属性,例如:

I want to use FalseObject and TrueObject properties in App resources like:

<Application
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="CoTraveller.RegistrationPage"
   xmlns:local="clr-namespace:CoTraveller">
    <Application.Resources>
        <!-- Application resource dictionary -->
        <ResourceDictionary>
            <local:BooleanToObjectConverter x:Key="boolToStyleImage"
x:TypeArguments="Style">
                <local:BooleanToObjectConverter.FalseObject>
                    <Style TargetType="Image">
                        <Setter Property="HeightRequest" Value="24" />
                        <Setter Property="Source"
Value="your_wrong_image_here.png" />
                    </Style>
                </local:BooleanToObjectConverter.FalseObject>

                <x:BooleanToObjectConverter.TrueObject>
                    <Style TargetType="Image">
                        <Setter Property="HeightRequest" Value="24" />
                        <Setter Property="Source"
Value="your_correct_image_here.png" />
                    </Style>
                </x:BooleanToObjectConverter.TrueObject>
            </local:BooleanToObjectConverter>
        </ResourceDictionary>
    </Application.Resources>
</Application>

但有错误:Severity Code Description Project File Line Suppression State错误 XLS0415 在BooleanToObjectConverter"类型中找不到可附加属性FalseObject".CoTraveller 应用程序.xaml 11

but have error: Severity Code Description Project File Line Suppression StateError XLS0415 The attachable property 'FalseObject' was not found in type 'BooleanToObjectConverter'. CoTraveller App.xaml 11

有什么问题?

推荐答案

从 DependencyObject 派生以便能够使用可绑定的 DependencyProperties:

Derive from DependencyObject in order to be able to use the bindable DependencyProperties:

public class BooleanToObjectConverter: DependencyObject, IValueConverter
{
    public Image FalseObject
    {
        get
        {
            return (Image)GetValue(FalseObjectProperty);
        }
        set
        {
            SetValue(FalseObjectProperty, value);
        }
    }

    public static readonly DependencyProperty FalseObjectProperty =
        DependencyProperty.Register(
            "FalseObject",
            typeof(Image),
            typeof(BooleanToObjectConverter<Image>),
            new PropertyMetadata(null));

    public Image TrueObject
    {
        get
        {
            return (Image)GetValue(TrueObjectProperty);
        }
        set
        {
            SetValue(TrueObjectProperty, value);
        }
    }

    public static readonly DependencyProperty TrueObjectProperty =
        DependencyProperty.Register(
            "TrueObject",
            typeof(Image),
            typeof(BooleanToObjectConverter<Image>),
            new PropertyMetadata(null));

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        return (bool)value ? this.TrueObject : this.FalseObject;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        return ((Image)value).Equals(this.TrueObject);
    }
}

旁注:这里使用泛型没有多大意义,所以我删除了它.

Side note: The use of the generic here doesn't make a lot of sense so I removed it.

如果最初的意图是使用实际的泛型类型,请参阅:Xaml 资源中的泛型类型 因为为资源字典中的条目指定泛型类型参数并不是那么简单.

If the original intention was to do use an actual generic type see: Generic Type in Xaml Resources because it is not that straight forward to specify a generic type argument for entries in a resources dictionary.

这篇关于XAML 中的类属性声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:16
查看更多