【问题标题】:Xamarin - Replace QR code on TextChanged() EventXamarin - 在 TextChanged() 事件上替换 QR 码
【发布时间】:2017-04-20 01:52:53
【问题描述】:

我正在 Xamarin Forms 中编写一个 QR 码应用程序,该应用程序在文本条目中获取输入字符串并将其转换为 QR 码。我希望二维码在您输入或删除文本条目时动态更改,类似于我找到的这个网站here

我相信使用TextChangeEventArgs 可以做到这一点,但我不确定它是如何工作的。我在这里错过了什么?

我的文字输入

var myEntry = new Entry
            {
                Text = "Hello SO"                    
            };

这是我在更改My Entry 时创建新条形码的函数(它还没有被任何东西调用)

void MyEntryChanged(Entry myEntry, TextChangedEventArgs e)
        {
            barcode = new ZXingBarcodeImageView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                AutomationId = "zxingBarcodeImageView",
            };
            barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
            barcode.BarcodeOptions.Width = 300;
            barcode.BarcodeOptions.Height = 300;
            barcode.BarcodeOptions.Margin = 10;
            barcode.BarcodeValue = myEntry.Text;

            Content = barcode;
        }

【问题讨论】:

  • 更新UI时可能需要使用Device.BeginInvokeOnMainThread

标签: c# xamarin xamarin.forms qr-code


【解决方案1】:

由于 MyEntryChanged 事件发生在与您的 UI 不同的线程中,因此元素 Content 不会出现在该线程中。

你应该使用这样的代码:

void MyEntryChanged(Entry myEntry, TextChangedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            barcode = new ZXingBarcodeImageView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                AutomationId = "zxingBarcodeImageView",
            };
            barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
            barcode.BarcodeOptions.Width = 300;
            barcode.BarcodeOptions.Height = 300;
            barcode.BarcodeOptions.Margin = 10;
            barcode.BarcodeValue = myEntry.Text;

            Content = barcode;
        });

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多