微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OnPropertyChanged不更新Xamarin的Xzing条码扫描器的UI

如何解决OnPropertyChanged不更新Xamarin的Xzing条码扫描器的UI

我在整个应用程序中都在使用mvvm,并且数据绑定和属性更改在应用程序中的其他任何地方都有效,但是由于某些原因,在使用XZing条码扫描器进行查看时,扫描项目后视图没有更新。

我曾经使用过XAML viewmodel和Codebehind,但是他们设置条形码扫描仪的方式仅是viewmodel和Codebehind /单独的类。所以也许我错过了一些我不习惯做的简单事情。

我在下面发布了一些代码,这些代码仅是其中一个不更新的属性(productsLable.Text的PartName)的示例。当页面第一次加载并且伪零件名正确显示时绑定了它,但是在扫描新项目之后,我更新了viewmodel中的ScannedPart,但是在设置并调用OnPropertyChanged()之后再也不会调用“ get”;

这在我的应用程序的其他部分运行良好,但是也许我在这里错过了一些愚蠢的事情,就像我说过的那样,我更愿意在XAML中使用绑定。您看到我做错了什么还是想念什么吗?

谢谢。

public class TimsCustomScanPage : ContentPage
    {
        ZXingScannerView zxing;
        TimsCustomOverlay overlay;
        RequestPartsviewmodel viewmodel;

        public TimsCustomScanPage(RequestPartsviewmodel viewmodel) : base()
        {
           //Attaching viewmodel
            this.viewmodel = viewmodel;
            this.BindingContext = viewmodel;
            //Scanner found barcode
            zxing.OnScanResult += (result) =>
            {
                // Stop analysis until we navigate away so we don't keep reading barcodes
                zxing.IsAnalyzing = false;
                //Setting new scanned part
                viewmodel.ScannedPart = viewmodel.Parts.FirstOrDefault();
            };
--Some more code--
 
var productsLabel = new Label()
            {
                //Text = "Find New Part",TextColor = Color.Black,WidthRequest = 150,HorizontalOptions = Layoutoptions.Start,VerticalTextAlignment = TextAlignment.Center,Padding = new Thickness(0,0),};
            //Binding text property 
            productsLabel.SetBinding(Label.TextProperty,"PartName");
            productsLabel.BindingContext = viewmodel.ScannedPart;
}

viewmodel

public class RequestPartsviewmodel : Baseviewmodel
    {
        public ObservableCollection<TimsCategory> Categories { get; set; }
        public ObservableCollection<TimsCategory> FavoriteCategories { get; set; }
        public ObservableCollection<TimsPart> Parts { get; set; }
        public TimsCategory CurrentSelection { get; set; }
        public Command LoadItemsCommand { get; set; }
        TimsPart scannedPart;
        string partName;
        int totalRequestedParts;

        public RequestPartsviewmodel()
        {
            Title = "Request Parts";
            Categories = new ObservableCollection<TimsCategory>(db.Table<TimsCategory>().ToList());
            Parts = new ObservableCollection<TimsPart>(db.Table<TimsPart>().ToList());
            TotalRequestedParts = 0;
            ScannedPart = new TimsPart();
            ScannedPart.PartName = "Scan New Part";
            ScannedPart.AvailableQuantity = 0;

            //Attach parts to categories 
            foreach (var item in Categories)
            {
                int newId = item.CategoryId;
                var partsForCategories = Parts.Where(p => p.CategoryId == newId);
                var partsForCategoriesCollection = new ObservableCollection<TimsPart>(partsForCategories);
                item.Parts = partsForCategoriesCollection;
            }
            
            FavoriteCategories = new ObservableCollection<TimsCategory>(Categories.Take(6).ToList());

            LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
        }

        public TimsPart ScannedPart
        {
            get
            {
                return scannedPart;
            }
            set
            {
                if (scannedPart != value)
                {
                    scannedPart = value;
                    OnPropertyChanged("PartName");
                    OnPropertyChanged("RequestedQuantity");
                    OnPropertyChanged("AvailableQuantity");
                    OnPropertyChanged("ScannedPart");
                }
            }
        }

基本viewmodel

public class Baseviewmodel : INotifyPropertyChanged
    {
        public sqliteConnection db = TimsData.database;

        bool isBusy = false;
        public bool IsBusy
        {
            get { return isBusy; }
            set { SetProperty(ref isBusy,value); }
        }

        string title = string.Empty;
        public string Title
        {
            get { return title; }
            set { SetProperty(ref title,value); }
        }

        protected bool SetProperty<T>(ref T backingStore,T value,[CallerMemberName]string propertyName = "",Action onChanged = null)
        {
            if (EqualityComparer<T>.Default.Equals(backingStore,value))
                return false;

            backingStore = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;

            changed.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

解决方法

我认为问题在于您直接绑定到属性,然后又更改了基础对象,而不仅仅是属性。试试这个

productsLabel.SetBinding(Label.TextProperty,"ScannedPart.PartName");
// you already have a BindingContext set for your page that the Label will inherit
// productsLabel.BindingContext = viewModel.ScannedPart;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。