BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Designing Loosely Coupled Metro Applications with URIs

| by Jonathan Allen Follow 131 Followers on Sep 15, 2011. Estimated reading time: 1 minute |

Metro-style applications are meant to be small and highly-focused. The large scale, monolithic applications that we traditionally build for the enterprise can be reimagined as work-flow specific tools. For example, a stock trading application you may have one tool for locating and viewing basic customer information and another for researching financial products. From either of these modes you would then need to jump over to the “purchase a stock” workflow, which may be in an entirely different application.

The way you can do this in Metro is by leveraging protocols. For our example above the protocol may look like “acme-stock-purchase://client=123&stock=XYZ”. At install time the stock purchasing tool would be registered to listen for messages sent to the “acme-stock-purchase” protocol. When that happens the tool is launched and receives the indicated parameters.

The code to handle this is quite easy to write and is available in C++, .NET, and JavaScript based applications. The code excerpt below shows a C++ application listening for OnActivated and OnFileActivated events. The latter is used when launching the application via a registered file type.

  
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args) 
  { 
    Window::Current->Content = ref new MainPage(true); 
    Window::Current->Activate(); 
  } 

  void App::OnFileActivated(Windows::ApplicationModel::Activation::FileActivatedEventArgs^ args) 
  { 
    MainPage^ page = ref new MainPage(false); 
    page->SelectScenario3(args->Files->GetAt(0)->Name); 
    Window::Current->Content = page; 
    Window::Current->Activate(); 
  } 

  void App::OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) 
  { 
    MainPage^ page = ref new MainPage(false); 
    if (args->Kind == Windows::ApplicationModel::Activation::ActivationKind::Protocol) 
    { 
        Windows::ApplicationModel::Activation::ProtocolActivatedEventArgs^ protocolArgs = dynamic_cast<Windows::ApplicationModel::Activation::ProtocolActivatedEventArgs^>(args); 
        page->SelectScenario4(protocolArgs->Uri->RawUri); 
    } 
    Window::Current->Content = page; 
    Window::Current->Activate(); 
  }

This sort of communication is one-way, the source application has no way of knowing how the destination is going to handle the message. While this limits some of your design choices, it keeps the various tools loosely coupled. As your companies business needs change individual tools can be updated without having to redeploy the whole stack.

Since these are URIs, they don’t have to be triggered by another Metro application. Any application that is capable of launching the web browser using “http://sample.com” can launch a protocol-enabled Metro application. Likewise users can simply paste the URI into the Run menu or the address field in either Windows Explorer or Internet Explorer. This allows developers to easily integrate legacy, web, and Metro applications.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT