This project has moved. For the latest updates, please go here.

Capture barcode from Windows Phone 8 then back to original page

Aug 20, 2013 at 1:52 PM
Hi, in WP8 I have a page with a LongListSelector databound to a list of items. Each item will have a capture button that should invoke capturing a bar code. Upon capturing the barcode the app will return to the page with the barcode for that particular item populated. Right now, I'm sort of doing it by using the code in the WP8 example client. When the capture button is clicked for a particular item, I navigate to the CaptureBarcode.xaml page, that I imported into my project. The code in that page works except I had to modify it to return to the original page when the bar code is captured. The code for that appears to be in the CaptureViewModel.cs page, which I also imported, and changed the InitializeAndGoMethod to redirect


   private async void InitializeAndGo()
        {
            Results = new ObservableCollection<Result>();
            CaptureResolution = await GetBestCaptureResolution();
            await InitializePhotoCaptureDevice(CaptureResolution);
            await StartCapturingAsync();
       
            var result = await GetBarcodeAsync();
                if (result == null)
                {
                    while (result == null)
                    {
                        result = await GetBarcodeAsync();
                       
                    }                              
                }
                Results.Add(result);
                if (bc != "")
                {
                    App.RootFrame.Navigate(new Uri("/detailspage.xaml?barcode=" + bc, UriKind.Relative));
                }       
        }
I can retrieve the passed barcode value on the details page, but I have to repopulate the data model because it is lost on the page load. Is there a better way to do this? How would I know which item was clicked and which item the barcode belonged to? Also, If I invoke the capture bar code page twice, the second time it always throws an exception in the InitializeAndGo method when trying to initialize the camera. Is there a way around this?
Developer
Aug 23, 2013 at 4:22 PM
Edited Aug 23, 2013 at 4:23 PM
Hi,

for passing values with MVVM I suggest reading about the mediator pattern. Simplyfied it's a dictionary that keeps some sort of messages you can pass between ViewModels.
http://pluralsight.com/training/Courses/TableOfContents/patterns-library
http://www.codeproject.com/Articles/35277/MVVM-Mediator-Pattern


To solve the second problem you need to Dispose() the PhotoCaptureDevice right before you navigate from the CatureBarcode.xaml.
You could also think about disposing it in your codebehind by ovveriding the OnNavigatedFrom() Method.

I'm sorry I couldn't response earlier but I'm at summer holidays in croatia at the moment and don't have that good internet connection ;)
Aug 23, 2013 at 5:44 PM
HI,

Thank you that helps a lot!