我有一个包含多个数据模板的列表视图。其中一个模板是简单的文本框和条形码扫描按钮(ZXingLibrary)。列表视图的页面以模态显示。该页面显示一个问题,其中包含绑定到列表视图的响应列表。条形码类型模板就是这样一种反应。
我正在更新以下代码中绑定到listview的可观察范围集合。成功完成条形码扫描后调用此功能。
private void UpdateUserResponseList(UserResponse obs, string result)
{
var temp = obs;
obs.RValue = result;
ObsList.Remove(temp);
ObsList.Add(obs);
}
当UI刷新时,两个平台中的listitem索引都会发生变化。这在iOS中完美运行。但是在Android中,只有当ObsList的计数大于1时才会刷新UI。它会保存集合中的值,但不会在文本框中显示/更新。
的ListView
<ListView x:Name="ObsListView" IsVisible="{Binding IsObservations}" CachingStrategy="RecycleElement" Margin="20" ItemsSource="{Binding ObsList}" SeparatorVisibility="None" HasUnevenRows="true" BackgroundColor="Transparent"
ItemTemplate="{StaticResource ObservationDataEntrySelector}" />
ObservableRangeCollection
public ObservableRangeCollection<UserResponse> ObsList
{
get
{ return _obsList; }
set
{
if (_obsList != value)
{
_obsList = value;
OnPropertyChanged("ObsList"); } }
}
}
用户点击扫描条形码按钮时调用的功能
private void BarcodeScannerExecuted(UserResponse obj)
{
ZXingScannerPage scanPage = new ZXingScannerPage();
var customOverlay = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var Cancel_Button = new Button
{
Text = "Cancel",
Command = CancelScanningCommand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
var Text_shown = new Label
{
Text = "Hold your device up to the barcode,Scanning will happen automatically",
TextColor = Xamarin.Forms.Color.DarkRed,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
customOverlay.Children.Add(Cancel_Button);
customOverlay.Children.Add(Text_shown);
scanPage = new ZXingScannerPage(customOverlay: customOverlay);
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
UpdateUserResponseList(obj, result.Text);
await _navigation.NavigatePopModalAsync();
});
};
_navigation.NavigatePushModalAsync(scanPage);
}