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

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

我曾经使用过XAML ViewModel和Codebehind,但是他们设置条形码扫描仪的方式只有ViewModel和Codebehind / alone类。因此,也许我缺少了一些我不习惯做的简单事情。

我在下面发布了一些代码,这些代码只是不更新​​的属性之一(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, 0, 0),
            };
            //Binding text property 
            productsLabel.SetBinding(Label.TextProperty, "PartName");
            productsLabel.BindingContext = viewModel.ScannedPart;
}

视图模型

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
    }