Xamarin 绑定在行为中不起作用 [英] Xamarin binding does not work in Behavior<>

查看:48
本文介绍了Xamarin 绑定在行为中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了将图像传输到 ViewModel 属性的行为.当用户单击该元素时,图库将打开.当用户从图库中选择一些图像时,我的 ImageBytes 将包含该图像的字节数.但是在我为该属性分配了一个新值后,它并没有传递给我的 VM.

I made Behavior for transfer image to the ViewModel property. When the user сlicks on the element, the gallery will open. When the user chooses some image from gallery, my ImageBytes will have bytes of this image. But after I assign a new value to the property, it is not passed to my VM.

我的视图模型不响应行为的变化.

My view model does not respond to changes in the Behavior.

    public class FolderDialogBehavior : Behavior<View>
    {
        public byte[] ImageBytes
        {
            get { return (byte[])GetValue(ImageBytesProperty); }
            private set 
            { 
                SetValue(ImageBytesProperty, value);
            }
        }

        public readonly static BindableProperty ImageBytesProperty = BindableProperty.Create(nameof(ImageBytes), typeof(byte[]),
            typeof(FolderDialogBehavior), null, BindingMode.TwoWay);


        private TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer()
        {
            NumberOfTapsRequired = 1
        };

        protected override void OnAttachedTo(View view)
        {
            base.OnAttachedTo(view);

            tapGestureRecognizer.Tapped += OnTapGestureRecognizerTapped;
            view.GestureRecognizers.Add(tapGestureRecognizer);
        }

        protected override void OnDetachingFrom(View view)
        {
            base.OnDetachingFrom(view);

            tapGestureRecognizer.Tapped -= OnTapGestureRecognizerTapped;
            view.GestureRecognizers.Remove(tapGestureRecognizer);
        }

        private void OnTapGestureRecognizerTapped(object sender, EventArgs e)
        {
            GetPhotoAsync();
        }

        private async void GetPhotoAsync()
        {
            try
            {
                var photo = await MediaPicker.PickPhotoAsync();

                byte[] bytes;

                using (Stream sourceStream = await photo.OpenReadAsync())
                {
                    bytes = new byte[sourceStream.Length];
                    await sourceStream.ReadAsync(bytes, 0, (int)sourceStream.Length);
                }

                ImageBytes = bytes;
            }
            catch (Exception ex)
            {
                //await DisplayAlert("Сообщение об ошибке", ex.Message, "OK");
            }
        }
    }

     <Frame>
         <Frame.Behaviors>
             <local:FolderDialogBehavior ImageBytes="{Binding AddEmployee.UserImage, Mode=TwoWay}"/>
         </Frame.Behaviors>
     </Frame>

     public class EmployeeViewModel : OnPropertyChangedClass
     {
         private byte[] _userImage;

         public byte[] UserImage 
        {
            get => _userImage; 

            // *** I don't get here with debugging.***

            set => SetProperty(ref _userImage, value); 
        }
     }

    public abstract class OnPropertyChangedClass : INotifyPropertyChanged
    {
        /// <inheritdoc cref="INotifyPropertyChanged"/>
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void SetProperty<T>(ref T propertyFiled, T newValue, [CallerMemberName] string propertyName = null)
        {
            if (!object.Equals(propertyFiled, newValue))
            {
                T oldValue = propertyFiled;
                propertyFiled = newValue;
                RaisePropertyChanged(propertyName);

                OnPropertyChanged(propertyName, oldValue, newValue);
            }
        }

        protected virtual void OnPropertyChanged(string propertyName, object oldValue, object newValue) { }
    }

推荐答案

    protected override void OnAttachedTo(View bindable)
    {

        BindingContext = bindable.BindingContext;
        bindable.BindingContextChanged += Bindable_BindingContextChanged;
        /* Some Code */
        base.OnAttachedTo(bindable);
    }

    protected override void OnDetachingFrom(View bindable)
    {
        bindable.BindingContextChanged -= Bindable_BindingContextChanged;
        /* Some Code */
        base.OnDetachingFrom(bindable);
    }

    private void Bindable_BindingContextChanged(object sender, EventArgs e)
    {
        if (sender is View view)
        {
            BindingContext = (sender as View).BindingContext;
        }
    }

这篇关于Xamarin 绑定在行为中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆