【问题标题】:Class property declaration in XAMLXAML 中的类属性声明
【发布时间】:2018-07-22 19:47:54
【问题描述】:

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

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();
    //}
}

}

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

<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>

但有错误:严重代码描述项目文件行抑制状态 错误 XLS0415 在类型“BooleanToObjectConverter”中找不到可附加属性“FalseObject”。 CoTraveller App.xaml 11

有什么问题?

【问题讨论】:

标签: xaml xamarin


【解决方案1】:

从 DependencyObject 派生以便能够使用可绑定的 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);
    }
}

旁注:这里使用泛型没有多大意义,因此我将其删除。

如果最初的意图是使用实际的泛型类型,请参阅:Generic Type in Xaml Resources,因为为资源字典中的条目指定泛型类型参数并不是那么直接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多