I have a code scanner that after scans, it returns me a string value. Then I add this string value to an observable collection.
When the ScanCode() method is triggered, the camera opens up and it scans a code then adds the value to the list. Then it gets back to the page, but the picker remains empty. Kindly help to figure out the issue.
private ObservableCollection<string> _codes;
public ObservableCollection<string> Codes
{
get { return _codes; }
set
{
_codes = value;
OnPropertyChanged();
}
}
public async void ScanCode()
{
codes= new ObservableCollection<string>();
var cd = await CodeScanViewModel.CodePage1(); // returns the code in string
if (cd != null)
{
_codes.Add(cd.ToString());
}
}
Then in my XAML, I have defined the picker as follows:
<Picker
Title="Codes"
ItemsSource="{Binding Codes}"
VerticalOptions="Center" />
At this point, as you have set a new instance of collection to _codes
but the didn't raise the property-changed notification. So the Picker
control is still listening to the older instance for collection-changed events. In order to fix that, make sure to use:
public async void ScanCode()
{
Codes = new ObservableCollection<string>();
...
Or,
public async void ScanCode()
{
_codes = new ObservableCollection<string>();
OnPropertyChanged(nameof(Codes));
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.