在图像Xamarin.Forms中显示QR代码

问题描述 投票:3回答:1

我正在使用ZXing.net.Mobile.Forms库读取/创建QR码的应用程序,我得到了一切工作,但对于CREATING部分,我想将其显示为图像。

到目前为止我只是将它添加到页面(见下面的代码)

  ZXingBarcodeImageView barcode = new ZXingBarcodeImageView();


  barcode.BarcodeFormat = BarcodeFormat.QR_CODE;
  barcode.BarcodeOptions.Width = 700;
  barcode.BarcodeOptions.Height = 700;
  barcode.BarcodeOptions.Margin = 10;
  barcode.BarcodeValue = txtQR.Text; // Text to be rendered to a QR CODE

  //StackPage name of the StackLayout
  StackPage.Children.Add(barcode);

而不是[StackPage.add(条形码)];

我想要这样的东西:

image.source =条形码;

任何想法,将不胜感激

xamarin.forms qr-code
1个回答
2
投票

Jason如何说ZXingBarcodeImageView继承自Image,因此它本身就是一个Image。所以你应该把它添加到View中。首先,在另一个文件中创建一个View,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
 x:Class="QRManager.Views.QRResult">
    <ContentView.Content>
    </ContentView.Content>
</ContentView>

现在,在您的MainPage中使用xmlns:local为您的命名空间添加此视图:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:QRManager.Views;assembly=QRManager"
         x:Class="QRManager.Views.QRGeneratorPage"
         Title="Generator Page">
    <ScrollView>
        <StackLayout Padding="10">
            <StackLayout>
                <Entry x:Name="contentEntry" TextColor="Black"
                   Placeholder="Texto" PlaceholderColor="Silver" 
                   Keyboard="Text" FontSize="18" HorizontalTextAlignment="Start"/>
                <Button Text="Generar QR" HorizontalOptions="FillAndExpand" BackgroundColor="#2196F3" TextColor="White" Clicked="Button_Clicked"/>
            </StackLayout>
            <local:QRResult x:Name="qrResult" />
        </StackLayout>
    </ScrollView>
</ContentPage>

最后在你的代码后面用ZXingBarcodeImageView设置你的View像这样:

private void Button_Clicked(object sender, EventArgs e)
{
    try
    {
         if (contentEntry.Text != string.Empty)
         {
             barcode = new ZXingBarcodeImageView
             {
                 HorizontalOptions = LayoutOptions.FillAndExpand,
                 VerticalOptions = LayoutOptions.FillAndExpand,
             };
             barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
             barcode.BarcodeOptions.Width = 500;
             barcode.BarcodeOptions.Height = 500;
             barcode.BarcodeValue = contentEntry.Text.Trim();
////////// SEE HERE //////////
             qrResult.Content = barcode;
         }
            else
            {
                DisplayAlert("Alert", "Introduzca el valor que desea convertir código QR", "OK");
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
            DisplayAlert("Alert", "Introduzca el valor que desea convertir código QR", "OK");
        }
}

有关更多信息,请参阅this repository及其样本。

© www.soinside.com 2019 - 2024. All rights reserved.