How to use ZXING with XAML and Prism in Xamarin forms?

2,289 views
Skip to first unread message

Nick Smith

unread,
Nov 15, 2016, 12:15:22 PM11/15/16
to zxing
Hi , I'm sorry if it's a stupid question but i just can't figure it out . So I'm developing a pretty basic scanning up ( as a complete beginner )and i'm using xamarin forms , ZXing.Net.Mobile for Forms and also Prism and the MVVM design pattern.I've managed to navigate around using the Prism.NavigationService with 1 exception that i simply can not figure out .
If i navigate from any page to my scanning page everything it's fine , BUT i can't navigate away from my scanning page (AFTER I'M DONE SCANNING) .The "after i'm done scanning" it's important because if i add a button to the scanning page and bind that button to the same command and press the button before letting it scan everyting works fine

to make it easier to understand :
What i want to achive : PageA ->ScanningPage (done scanning) -> PageB
My current situation : ScanningPageViewModel : NavigateAsync("PageB ") ->Does Nothing.

Here's my code for the ScanningPage.(nothing fancy)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="MyApp.Views.PrismContentPage1">

<forms:ZXingScannerView IsScanning="{Binding IsScanning }" IsAnalyzing="{Binding IsAnalyzing}" Result="{Binding Result ,Mode=TwoWay}" ScanResultCommand="{Binding NavigateCommand }">

</forms:ZXingScannerView>

</ContentPage>


And in the viewmodel i just use the NavigationService to walk away from that page when i'm done scanning( or that's what i want to do) .

public class ScanningPageViewModel: BindableBase
{

private INavigationService _navigationService;
private bool _isAnalyzing = true;
private bool _isScanning = true;
public ZXing.Result Result { get; set; }

public bool IsAnalyzing
{
get { return _isAnalyzing; }
set
{
_isAnalyzing = value;
OnPropertyChanged();
}
}

public bool IsScanning
{
get { return _isScanning; }
set
{
_isScanning = value;
OnPropertyChanged();
}
}

public DelegateCommand NavigateCommand { get; private set; }

public ScanningPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
NavigateCommand = new DelegateCommand(Navigate, CanNavigate);
}

private void Navigate()
{
IsAnalyzing = false;
IsScanning = false;
_navigationService.NavigateAsync("Page3");

}

private bool CanNavigate()
{
return true;
}
}


When the scan it's complete the camera freezes and the app does nothing . In debug mode , in my ViewModel , on this line _navigationService.NavigateAsync("Page3") i get something like "Unknown member: NavigateAsync".
If I take out the ZXingScannerView from XAML and replace it with a button(or anything) and bind the button to the same command everything works fine .
Have i done something wrong? Is there a better way to do this?

Anu Viswan

unread,
Jan 13, 2017, 6:37:46 AM1/13/17
to zxing
Hi Nick.

Am currently running into same issue as yours. Were you able to get through this one ?

Thanks
Anu

Nivel400

unread,
Mar 2, 2017, 10:21:19 PM3/2/17
to zxing
Use GoBackAsync instead, I created a "GlobalVars" class with a static property, then saved the result in that property and last, i called the "GoBackAsync".

I modified your sample like this and now its scanning and then returning to last page!


private void Navigate()
{
IsAnalyzing = false;
IsScanning = false;

GlobalVars.LastBarCode = Result.Text;
_navigationService.GoBackAsync();

}

Jens Burgstaller

unread,
Mar 31, 2017, 6:30:04 AM3/31/17
to zxing
private async void Navigate()

{
IsAnalyzing = false;
IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
var p = new NavigationParameters();
p.Add("QrCodeResult", Result.Text);
await _navigationService.NavigateAsync("SuperAwesomePage", p);
});
}

Hey Guys, i had the same issue. Device.BeginInvokeOnMainThread did the trick for me.

Reply all
Reply to author
Forward
0 new messages