在图片中显示二维码 Xamarin.Forms

Show QR code in an image Xamarin.Forms

我正在使用 READ/CREATE 使用 ZXing.net.Mobile.Forms 库的 QR 代码的应用程序,我的一切正常,但对于创建部分,我想将其显示为图像。

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

  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 = 条形码;

如有任何想法,我们将不胜感激

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

<?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 像这样:

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 示例。