我正在用Xamarin表单编写一个二维码应用程序,该应用程序接受文本条目中的输入字符串,并将其转换为二维码。我希望二维码在你输入或删除文本输入时动态变化,类似于我发现的这个网站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;
}发布于 2017-04-20 14:44:46
由于MyEntryChanged事件发生在与UI不同的线程中,因此元素内容不会出现在该线程中。
您应该像这样使用代码:
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;
});
}https://stackoverflow.com/questions/43508863
复制相似问题