Xamarin Forms Xaml 转换器:可绑定属性

Dis*_*sti 3 c# xaml binding xamarin.forms

在 Xamarin Forms 中,我试图创建一个带有属性的 xaml 转换器。例如,这将用于根据属性背后的代码以不同方式显示列表中的值。

我的代码基于此:https : //stackoverflow.com/a/29869734

转换器:

namespace App2.Converters
{
    class MyConverter : IValueConverter
    {

        public int ConvParam { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return $"value: {value} - ConvParam: {ConvParam}";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:conv="clr-namespace:App2.Converters"
         x:Class="App2.MainPage"
         x:Name="MainPageXaml">

<ContentPage.Resources>
    <conv:MyConverter x:Key="cnv" ConvParam="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />
    <!--<conv:MyConverter x:Key="cnv" ConvParam="333" />-->
</ContentPage.Resources>

<StackLayout Orientation="Vertical">
    <!-- Place new controls here -->
    <Label Text="{Binding Source={Reference MainPageXaml}, Path=PropVal}" />
    <Label Text="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />
    <Label Text="{Binding Source={Reference MainPageXaml}, Path=PropVal, Converter={StaticResource cnv}}" />
</StackLayout>
Run Code Online (Sandbox Code Playgroud)

后面的代码:

public partial class MainPage : ContentPage
{

    public int PropVal { get; set; } = 111;
    public int PropParam { get; set; } = 222;

    public MainPage()
    {
        InitializeComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

目标是在后面的代码中将我的转换器的 ConvParam 绑定到 PropParam。

但如果我使用:

<conv:MyConverter x:Key="cnv" ConvParam="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />
Run Code Online (Sandbox Code Playgroud)

错误位置 10:39。未显示“ConvParam”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配,并且应用程序无法编译。

属性ConvParam本身在 xaml 中被识别:如果我用

<conv:MyConverter x:Key="cnv" ConvParam="333" />
Run Code Online (Sandbox Code Playgroud)

一切正常。

如果用作标签文本属性的源,我使用的绑定表达式({Binding Source={Reference MainPageXaml}, Path=PropParam})实际上有效:

<Label Text="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />
Run Code Online (Sandbox Code Playgroud)

但是如果我在资源中使用它,它就不起作用。

Dis*_*sti 6

多亏了Julipan,我才能让它发挥作用!

正如他所指出的,ConvParam 必须是一个 BindableProperty,所​​以我修改了我的转换器以从 BindableObject 继承并将 ConvParam 定义为 BindableProperty。

转换器:

namespace App2.Converters
{
    class MyConverter : BindableObject, IValueConverter
    {
        public static readonly BindableProperty ConvParamProperty = BindableProperty.Create(nameof(ConvParam), typeof(int), typeof(MyConverter));

        public int ConvParam
        {
            get { return (int)GetValue(ConvParamProperty); }
            set { SetValue(ConvParamProperty, value); }
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return $"value: {value} - ConvParam: {ConvParam}";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)