简体   繁体   English

Xamarin Forms 布尔绑定在视图加载时不起作用

[英]Xamarin Forms bool binding not working when view loads

I have a simple View showing two barcodes in one place.我有一个简单的视图,在一个地方显示两个条形码。 For this I am using ZXing library.为此,我正在使用 ZXing 库。 The idea is to switch visibility between these two barcodes.这个想法是在这两个条形码之间切换可见性。 So when the view loads, it should display one barcode and after the button click it should switch to another.因此,当视图加载时,它应该显示一个条形码,并且在单击按钮后它应该切换到另一个。

So both of these barcode have IsVisible property binded to a ViewModel property.所以这两个条形码都有IsVisible 属性绑定到 ViewModel 属性。 One is set to true, other to false.一个设置为真,另一个设置为假。

Problem is that after view loads there is no barcode visible and only after the button click it starts to work.问题是视图加载后没有可见的条形码,只有在单击按钮后它才开始工作。 So logic and binding is OK but the view is not displaying anything on the first run.所以逻辑和绑定没问题,但视图在第一次运行时没有显示任何内容。

ViewModel (simplified):视图模型(简化):

    private bool _IsQR;
    public bool IsQR
    {
        get => _IsQR;
        set
        {
            SetProperty(ref _IsQR, value);
        }
    }

    private bool _Is128;
    public bool Is128
    {
        get => _Is128;
        set
        {
            SetProperty(ref _Is128, value);
        }
    }


    public ICommand ToogleCode { private set; get; }

    public BarcodeViewModel()
    {
        IsQR = false;
        Is128 = true;
        ToogleCode = new Command(ToogleCodeHandler);
    }

    private void ToogleCodeHandler()
    {
        Is128 = !Is128;
        IsQR = !IsQR;
    }

View (simplified):查看(简化):

<Grid x:DataType="vm:BarcodeViewModel">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <Label Grid.Row="0" Text="{Binding Code}" 
               LineBreakMode="NoWrap"
               HorizontalOptions="Center" 
               VerticalOptions="Center"
               FontSize="Large"
               FontAttributes="Bold"
               Padding="0,0,0,10"/>

        <Button Grid.Row="1" Text="Change" Command="{Binding ToogleCode}"/>

        <zxing:ZXingBarcodeImageView Grid.Row="2"
                                     BarcodeValue="{Binding Code}" 
                                     BarcodeFormat="QR_CODE" 
                                     IsVisible="{Binding IsQR, Mode=TwoWay}">
            
            <zxing:ZXingBarcodeImageView.BarcodeOptions>
                <zxcm:EncodingOptions Width="500" Height="500" />
            </zxing:ZXingBarcodeImageView.BarcodeOptions>
            
        </zxing:ZXingBarcodeImageView>

        <zxing:ZXingBarcodeImageView Grid.Row="2"
                                     BarcodeValue="{Binding Code}" 
                                     BarcodeFormat="CODE_128"
                                     Rotation="90"
                                     VerticalOptions="Center"
                                     HorizontalOptions="Center"
                                     IsVisible="{Binding Is128, Mode=TwoWay}">
            
            <zxing:ZXingBarcodeImageView.BarcodeOptions>
                <zxcm:EncodingOptions Width="1920" Height="1080" />
            </zxing:ZXingBarcodeImageView.BarcodeOptions>
            
        </zxing:ZXingBarcodeImageView>


    </Grid>

This is not the first time I saw this.这不是我第一次看到这个。 Once I was filling the CollectionView and it did not show anything until I set one property in the OnAppearing() event in the ViewModel.填充 CollectionView 后,它没有显示任何内容,直到我在 ViewModel 的 OnAppearing() 事件中设置了一个属性。 But here I can not make it work.但在这里我不能让它工作。

EDIT: It is not working only for the Barcode (128).编辑:它不仅适用于条形码(128)。 If I set QR = true and 128 = false it works as it should.如果我设置 QR = true 和 128 = false 它应该可以正常工作。 Problem is therefore related to ZXingBarcodeImageView因此问题与 ZXingBarcodeImageView 有关

I have made an sample to test, and when I clicked button, the command Binding doesn't work.我制作了一个示例进行测试,当我单击按钮时,命令绑定不起作用。 If you want to use command Binding in your button, you can use eventtocommandbehavior class.如果要在按钮中使用命令绑定,可以使用 eventtocommandbehavior class。 I think you may be able to use ButtonClicked Event in your view, such as:我认为您可以在视图中使用 ButtonClicked 事件,例如:

MainViewModel viewModel;

public MainPage()

{

InitializeComponent();

viewModel = (MainViewModel)this.BindingContext;

}
private void Button_ Clicked(object sender, EventArgs e)

{

viewModel. IsQR = ! viewModel.IsQR;

viewModel. Is128 = ! viewModel.Is128;

}

Give a default value to the bool value:给布尔值一个默认值:

   public BarcodeViewModel()
    {
        Is128 = True;
        IsQR = False;
        ToogleCode = new Command(ToogleCodeHandler);
    }

I have solution.我有解决方案。 Problem is not in the data binding but in the ZXingBarcodeImageView element.问题不在于数据绑定,而在于 ZXingBarcodeImageView 元素。 It seems that when Background property is not null, it works as it should.似乎当背景属性不是 null 时,它应该可以正常工作。 So data binding is ok.所以数据绑定没问题。

I am unable to delete this question.我无法删除这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM