Page 75 - MSDN Magazine, September 2017
P. 75

click OK. Once again, the app will launch. This time, however, the Message dialog will have the Query String and AbsolutePath values displayed.
Stop the app and re­run the same command from the Run dia­ log. Once again, the app doesn't progress beyond the splash screen. It’s time to address that.
Figure 4 Modifying the Code Inside the OnActivated Method
Navigating to a Page
Modify the OnActivated method in App.xaml.cs to match the code in Figure 5. While this code looks complex, it’s actually not. Much of the additional code concerns revolve around finding the current content of the active window. In cases where the app is already running, the current content for this app will be the MainPage class. When the app isn’t running and is launched by protocol activation, there’s no current content, and Window.Current.Content has a null value. In those cases, the code creates a new Frame, sets the appropriate event handlers and content, and navigates to the MainPage. For more information about the Window and Frame classes, refer to bit.ly/2tGwt1H and bit.ly/2sQ8LTY, respectively.
The rest of the code is a slight modification from before, with the addition of making a call to a NavigateWithParameters method. This is how the information from the URI will get sent to the MainPage class. Now is the time to add that method to the MainPage.xaml.cs file and add some UI elements to the MainPage.xaml file.
First, modify the MainPage.xaml file to have the following XAML inside the Page element:
<Grid Name="grdBackground" Background=
"{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Name="grdForeground" Width="100" Height="100"></Grid>
</Grid>
This adds the name “grdBackground” to the Grid control already present and adds an inner grid named “grdForegound.”
Next, add the code in Figure 6 to the MainPage.xaml.cs file. The bulk of the code converts a hex code ARGB color value like #FFAA3CC7 into something that a SolidColorBrush can use. The conversion code drew inspiration from the blog post at bit.ly/2tGuFFH, which also contains a great explanation of how the conversion works. The remainder of the code is fairly simple; it changes the color of one grid or the other based on the target parameter.
Once all the code is in place, run the app. In the Run dialog box type “msdncolors:foreground?#FFAA3CC7” and click OK. This should turn the foreground grid a shade of purple. Now, type “msdncolors:background?#FFCFCFCF” into the textbox of the Run dialog and click OK. The background Grid should turn a light gray. Close the app. Now, re­enter the previous commands to see that the app is the same when it’s not already running.
Wrapping Up
In this column, I demonstrated how to register a UWP app to handle a particular protocol. Adding this feature will make your UWP apps accessible to other apps on a user’s device. This lets other developers create interesting uses for the apps you develop and makes your app more desirable. While much of the activation in this column was done using the Run dialog, passing the same URI to the Launcher class will yield the same results. n
Frank La Vigne is chief evangelist at DataLeader.IO, where he helps custom- ers leverage technology in order to create a better world. He is co-host of the data science podcast Data Driven (DataDriven.tv) and blogs regularly at FranksWorld.com.
Thanks to the following technical expert for reviewing this article: Jose Luis Manners
if (e.Kind == ActivationKind.Protocol) {
var args = e as ProtocolActivatedEventArgs; var message = "no parameters passed";
if (args.Uri.PathAndQuery != string.Empty) {
var queryString = args.Uri.Query;
var absolutePath = args.Uri.AbsolutePath;
message = $"Query String: {queryString}\
 AbsolutePath: {absolutePath}";
}
var dialog = new MessageDialog(message); await dialog.ShowAsync();
} Window.Current.Activate();
Figure 5 Updated OnActivated Code
protected override void OnActivated(IActivatedEventArgs e) {
if (e.Kind == ActivationKind.Protocol) {
var args = e as ProtocolActivatedEventArgs; if (args.Uri.PathAndQuery != string.Empty) {
Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null)
{
rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null) {
if (!rootFrame.Navigate(typeof(MainPage))) {
throw new Exception("Failed to create initial page"); }
}
var fragment = args.Uri.Fragment;
var absolutePath = args.Uri.AbsolutePath;
var mainPage = rootFrame.Content as MainPage; mainPage.NavigateWithParameters(absolutePath, fragment);
} }
Window.Current.Activate(); }
Figure 6 The NavigateWithParameters Method
public void NavigateWithParameters(string target, string color) {
string hexCode = color.Replace("#", string.Empty);
byte a = (byte)(Convert.ToUInt32(hexCode.Substring(0, 2), 16)); byte r = (byte)(Convert.ToUInt32(hexCode.Substring(2, 2), 16)); byte g = (byte)(Convert.ToUInt32(hexCode.Substring(4, 2), 16)); byte b = (byte)(Convert.ToUInt32(hexCode.Substring(6, 2), 16));
var brush = new SolidColorBrush(Color.FromArgb(a, r, g, b));
if (target.ToLower() == "background") {
grdBackground.Background = brush; }
else {
grdForeground.Background = brush; }
}
msdnmagazine.com
September 2017 67











































   73   74   75   76   77