2017-12-01 6 views
2

私はZXing.net.Mobile.Formsライブラリを使用してQRコードを読み取り/作成するアプリで作業していますが、私はすべて動作するようになっていますが、CREATING部分では画像。画像にQRコードを表示するXamarin.Forms

は、これまでただ、それをページに追加してい代わりの

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 =バーコードを。

任意のアイデアは、ジェイソン・イメージからZXingBarcodeImageView継承を言うので、それは画像自体でどのように

+0

ZXingBarcodeImageViewはImageから継承されているため、Imageそのものです。なぜそれをページに挿入したくないのですか? – Jason

+0

@ジェイソン私はこれをそのままにしておくと、ユーザーがQRコードを生成するたびに、そのバーコードが追加されますが、交換する必要があります。 –

+0

既に持っているImageコントロールをZxIngBarcodeImageViewに置き換えてください。 – Jason

答えて

1

をいただければ幸いです。だからあなたはそれをビューに追加するべきです。まず、このような別のファイルにビューを作成します。

<?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> 

今、あなたのメインページにあなたの名前空間の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"); 
     } 
} 

詳細については、を参照してください。

+0

優れた応答@ウィルソン・バルガス。あなたのソリューションは魅力的に機能します。唯一の問題は、「QRを生成」ボタンを再度クリックしたときです。一度クリックすると、アプリケーションがクラッシュします。 –

+0

@Joel:あなたが新しいstackoverflowの質問として得る例外を投稿してください – knocte

関連する問題