OnScanResult Zxing 后 Xamarin 数据网格不显示

Posted

技术标签:

【中文标题】OnScanResult Zxing 后 Xamarin 数据网格不显示【英文标题】:Xamarin datagrid not show up after OnScanResult Zxing 【发布时间】:2020-09-03 00:59:27 【问题描述】:
    我可以在扫描条形码后获取数据表,但是当我将它绑定到 sfDatagrid 时它不显示。我究竟做错了什么。我想我打电话给 vm.TimSPOnKho.Execute(null);在 .cs 中不正确 代码 xaml 和 .cs

<ContentPage.BindingContext>
        <vm:vmBanHang_get_TTSanPham_ScanCode />
    </ContentPage.BindingContext>
    <StackLayout>
        <Grid VerticalOptions="CenterAndExpand"
                  HorizontalOptions="CenterAndExpand">
            <zxing:ZXingScannerView x:Name="scanView"
                                        OnScanResult="scanView_OnScanResult"
                                        IsScanning="True"
                                        WidthRequest="200"
                                        HeightRequest="300"
                                        VerticalOptions="CenterAndExpand"
                                        HorizontalOptions="CenterAndExpand"/>
            <zxing:ZXingDefaultOverlay TopText="Align the barcode within the frame"/>
        </Grid>
        <datagrid:SfDataGrid HorizontalOptions="Center" x:Name="datagrid"
                                 AllowTriStateSorting="True" 
                                 ColumnSizer="Star"
                                 ItemsSource="Binding DataTableCollection">
        </datagrid:SfDataGrid>
    </StackLayout>
这是我的 xaml 文件
private void scanView_OnScanResult(Result result)
    
        Device.BeginInvokeOnMainThread(async () =>
        
            await DisplayAlert("Scanned result", "The barcode's text is " + result.Text + ". The barcode's format is " + result.BarcodeFormat, "OK");
            var vm = new vmBanHang_get_TTSanPham_ScanCode();
            vm.MaSanPham = result.Text;
            vm.IDCuaHang = 1;
            vm.TimSPTonKho.Execute(null);


        );

    

我的 ViewModel.cs

class vmBanHang_get_TTSanPham_ScanCode : INotifyPropertyChanged

    private ApiServices _apiServices = new ApiServices();
    public int IDCuaHang  get; set; 
    public string MaSanPham  get; set; 

    public vmBanHang_get_TTSanPham_ScanCode()
    
        DataTableCollection = _DataTableCollection;
    
    public DataTable DataTableCollection
    
        get  return _DataTableCollection; 
        set
        
            _DataTableCollection = value;
            OnPropertyChanged();
        
    

    public DataTable _DataTableCollection;


    public ICommand TimSPTonKho
    
        get
        
            return new Command(async () =>
            
                if (!string.IsNullOrEmpty(MaSanPham))
                
                    DataTableCollection = await _apiServices.get_TTSanPham_ScanCode(IDCuaHang, MaSanPham, Settings.Accesstoken);
                

            );
        
    

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    

2。我的第二个问题,如何获取 Datagrid 发送到 Datatable 的所有值?我必须在 xaml.cs 或 viewmodel 中实现它吗? 感谢帮助

【问题讨论】:

【参考方案1】:

我找到了解决方案。直接从 xaml.cs 绑定

datagrid.ItemsSource = await _apiServices.get_TTSanPham_ScanCode(1, result.Text, Settings.Accesstoken);

而不是在 Viewmodel 中调用 Icommand

public ICommand TimSPTonKho

    get
    
        return new Command(async () =>
        
            if (!string.IsNullOrEmpty(MaSanPham))
            
                DataTableCollection = await _apiServices.get_TTSanPham_ScanCode(IDCuaHang, MaSanPham, Settings.Accesstoken);
            

        );
    

【讨论】:

以上是关于OnScanResult Zxing 后 Xamarin 数据网格不显示的主要内容,如果未能解决你的问题,请参考以下文章

Xama不会跟踪Xamarin Android新文件

zxingobjc 怎么关闭扫码成功后的声音

Xamarin.Android Camera Permissions with ZXing.Net.Mobile 仅在应用重启后有效

Android使用zxing生成二维码

将 IsScanning 属性从 False 更改为 True 后 C# Xamarin ZXing 无法扫描条形码

请教一下Android Studio中ZXING的用法