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

4

我正在尝试使用Xamarin Forms创建带有属性的xaml转换器。

例如,基于代码后面的属性,以不同的方式显示列表中的值。

我根据这个代码进行了编写:https://dev59.com/GF0a5IYBdhLWcg3w48Tn#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();
        }
    }
}

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>

代码后台:

public partial class MainPage : ContentPage
{

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

    public MainPage()
    {
        InitializeComponent();
    }
}

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

但如果我使用:

<conv:MyConverter x:Key="cnv" ConvParam="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />

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

xaml中已经识别了属性ConvParam本身:如果我使用以下行替换上述行:

<conv:MyConverter x:Key="cnv" ConvParam="333" />

一切正常。

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

<Label Text="{Binding Source={Reference MainPageXaml}, Path=PropParam}" />

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


1
您不能将绑定设置为非BindableProperty属性!您必须将ConvParam定义为BindableProperty。请参见https://xamarinhelp.com/bindable-properties-xamarin-forms/。 - deczaloth
另外,你最好尝试使用内置的ConverterParameter属性,而不是定义自己的属性。请参见https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters#binding-converter-parameters - deczaloth
1
谢谢Julipan,我修改了我的转换器并将ConvParam定义为BindableProperty,它起作用了。我不使用ConverterParameter是因为...它不是一个BindableProperty!我将修改我的问题以展示工作代码,但我想给你指出正确方向的功劳。 - Disti
你可以发布你的答案并接受它,这将帮助更多的人。 - Lucas Zhang
1个回答

8

多亏了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();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接