3

I am from an Android background and am progressing into Windows Phone 8.

I have a Page that uses the camera to decode a QR code and this works fine. In Android I would start this Activity using the Intent StartActivityForResult, which would then give me the decoded value back to the original Activity.

I have searched but could not find an obvious equivalent in Windows Phone 8. My thought at the moment is to navigate to the calling page with a query string containing the decoded value and alter the back stack, but this seems a little messy.

The Question

Is there an equivalent to the process in android, and if so can someone outline the methodology so I may see it in action?

Graham Smith
  • 25,627
  • 10
  • 46
  • 69

1 Answers1

2

First, there's no such thing in WP8, so you'll need a workaround. Workarounds could be different, and the linked question (and answer) is one of the potential approaches. I personally do this a bit differently. I'll describe my current project's architecture here, though it may not be applicable to your situation, since my app is quite big and has a complex structure. But I'll try to explain how it could be applied to your situation.

Particularly, my current app consists of so called services (just my name, not a standard one). Those have different scope (some are used by 1 page, some are global for the app), different lifetime, and so on. Essentially, every service is a class implementing a well-defined interface, so that other services could use that.

Next, services could depend on each other. I'm using Ninject framework for dependency injection. Essentially, if service A depends on service B, it leads to a code like this:

public class B : IB
{
...
}
public class A
{
    IB b;
    public A(IB b)
    {
        this.b = b;
    }
}

where IB is an interface, which service B implements.

Then I have view models (yes, I'm using MVVM and you probably should too, if you want to build a reasonably big WP8 app). View models are using services to perform app functions. Some of the services are used by several view models. For example, I have one service that fetches some data from the web, and keep it up to date with periodic polling. That web data is used in several pages of the application, so it should be shared between different view models. It is achieved by dependency injection again, so that all interested view models accept this service instance as a constructor parameter.

public class MainPageViewModel : INotifyPropertChanged
{
    private string webData;
    public MainPageViewModel(IWebDataService service)
    {
        webData = service.CurrentWebData;
        service.WebDataChanged += (o, e) => webData = service.CurrentWebData;
    }
    ...
}

public class DetailPageViewModel : INotifyPropertChanged
{
    private string webData;
    public DetailPageViewModel(IWebDataService service)
    {
        webData = service.CurrentWebData;
        service.WebDataChanged += (o, e) => webData = service.CurrentWebData;
    }
    ...
}

public class WebDataService : IWebDataService
{
    public string CurrentWebData;
    public event EventHandler WebDataUpdated;
    ...
}

Ninject allows me to have a single instance of IWebDataService instantiated, so that main and details page share the same instance of it. When the web data is updated, an event is fired, so that both view models can update their instances of web data, and push this new data to the views.

So here's how I do it. You could potentially reuse some part of this architecture, like having a singleton instance of some class accessible by different pages. One page puts some new data to that singleton instance, and, when data is updated (event fired or during construction, if garbage collector had enough time to kill existing page and/or view model instance), another page reads the updated data. That's how they share.

If you want me to go deeper into details on some topic, please feel free to ask in comments. Android developers are more than welcomed to the Windows Phone. :)

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • WOW what a response, you have my thanks for both attitude and quality. I have found the overall move between platforms not too painful and I like VS better than Eclipse. I know a little of MVVM so I guess I have some homework for the weekend. – Graham Smith May 16 '13 at 18:28
  • VS is really good, agree. Been using Eclipse for Symbian development years ago, and it was okay as well, but not as good. But the best thing in WP development is the C# language, IMO. Moving from Java, I hope you'll find it awesome as well. :) – Haspemulator May 16 '13 at 18:38
  • [laughing] I'd say you were a Microsoft shill if I didn't totally agree, Haspemulator. I am, unfortunately, going the other way because my WP8 app is enough of a hit to make me want a bigger pond. Can I ask what on earth you have created that is so complex on a phone? My app is quite simple with all the action happening server side. – Peter Wone Dec 14 '13 at 09:33
  • You can read about my app here: http://techcrunch.com/2013/08/30/nokia-takes-its-auto-ambitions-into-high-gear-with-connected-driving-a-cross-platform-suite-of-in-car-navigation-and-location-services-and-smartphone-apps/ – Haspemulator Dec 14 '13 at 12:16