Xamarin窗体项目的可绑定属性在MAUI中不起作用

ix0qys7i  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(155)

在将Xamarin forms应用程序迁移到.Net MAUI期间,当我在MAUI项目中使用BindableProperties时,Forms项目的自定义控件BindableProperty显示错误

The non-generic method 'BindableProperty.Create(string, Type, Type, object, BindingMode, BindableProperty.ValidateValueDelegate, BindableProperty.BindingPropertyChangedDelegate, BindableProperty.BindingPropertyChangingDelegate, BindableProperty.CoerceValueDelegate, BindableProperty.CreateDefaultValueDelegate)' cannot be used with type arguments (net7.0-maccatalyst)

字符串
这是我的代码

namespace PRojall
{
    public partial class CustomImage : Grid
    {
        public static readonly BindableProperty StatusProperty = BindableProperty.Create<CustomImage, ImageStatus>(p => p.Status, ImageStatus.Loading, propertyChanged: OnStatusChanged);

        public static readonly BindableProperty SourceProperty = BindableProperty.Create<CustomImage, ImageSource>(p => p.Source, null, propertyChanged: OnSourceChanged);

        public static readonly BindableProperty AspectProperty = BindableProperty.Create<CustomImage, Aspect>(p => p.Aspect, Aspect.AspectFit,propertyChanged: OnAspectChanged);

        public CustomImage()
        {
            InitializeComponent();
            SetImage(ImageStatus.Loading);
        }

        public ImageStatus Status
        {
            get { return (ImageStatus)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); SetImage(value); }
        }

        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public Aspect Aspect 
        { 
            get { return (Aspect)GetValue(AspectProperty); }
            set { SetValue(AspectProperty, value); }
        }

        private static void OnStatusChanged(BindableObject bindable, ImageStatus oldValue, ImageStatus newValue)
        {
            var obj = bindable as CustomImage;
            if (obj != null && oldValue != newValue)
            {
                obj.SetImage(newValue);
            }

        }

        private static void OnAspectChanged(BindableObject bindable, Aspect oldValue, Aspect newValue)
        {
            var obj = bindable as CustomImage;
            if (obj != null && oldValue != newValue)
            {
                obj.image.Aspect = newValue;
            }

        }

        private static void OnSourceChanged(BindableObject bindable, ImageSource oldValue, ImageSource newValue)
        {
            var obj = bindable as CustomImage;
            if (obj != null && oldValue != newValue && newValue != null)
            {
                obj.image.Source = newValue;
            }
        }
        private void SetImage(ImageStatus status)
        {
            switch (status)
            {
                case ImageStatus.Loading:
                    this.Source = ImageSource.FromFile("loading.png");
                    break;
                case ImageStatus.Error:
                    this.Source = ImageSource.FromFile("error.png");
                    break;

                case ImageStatus.NoImage:
                    this.Source = ImageSource.FromFile("noimage.png");
                    break;
            };
        }

    }
}


这些是BindableProperties中出现的错误


的数据
如何在MAUI项目中使用此代码?

z4bn682m

z4bn682m1#

查看Xamarin.Forms代码,这些Create重载已经过时很长时间了.这可能也是.NET MAUI最终删除它们的原因。
您希望去掉在尖括号之间定义的类型,因此可以将BindableProperty.Create<CustomImage, ImageStatus>()转换为BindableProperty.Create()
然后,从那里,有两个强制参数。在上面的示例中,完整声明可能如下所示:
BindableProperty.Create(nameof(Source), typeof(ImageStatus), typeof(CustomImage), ImageStatus.Loading, propertyChanged: OnStatusChanged);
因此,基本上,您在尖括号中定义的类型将分别作为第三个和第二个参数移动到参数中。

相关问题