OnPropertyChanged 没有为 xamarin 的 xzing 条码扫描器更新 UI
OnPropertyChanged not updating UI for xzing barcode scanner for xamarin
我在整个应用程序和数据绑定中使用 mvvm,并且 属性 更改在应用程序的其他任何地方都有效,但出于某种原因,在我使用 XZing Barcode Scanner 的视图中,我的视图在我之后没有更新扫描一个项目。
我习惯于使用 XAML ViewModel 和 Codebehind,但我设置条码扫描仪的方式只有 ViewModel 和 Codebehind/alone class。所以也许我错过了一些我不习惯做的简单事情。
我在下面发布了一些代码,它只是一个未更新属性的示例,productsLable.Text 的 PartName。它在页面首次加载且虚拟 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");
}
}
}
基础视图模型
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;
我在整个应用程序和数据绑定中使用 mvvm,并且 属性 更改在应用程序的其他任何地方都有效,但出于某种原因,在我使用 XZing Barcode Scanner 的视图中,我的视图在我之后没有更新扫描一个项目。
我习惯于使用 XAML ViewModel 和 Codebehind,但我设置条码扫描仪的方式只有 ViewModel 和 Codebehind/alone class。所以也许我错过了一些我不习惯做的简单事情。
我在下面发布了一些代码,它只是一个未更新属性的示例,productsLable.Text 的 PartName。它在页面首次加载且虚拟 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");
}
}
}
基础视图模型
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;