Note: this is a placeholder article so I can create a bit.ly link for an upcoming MSDN Magazine article that will point to the real documentation for this feature. Those docs are currently in progress and will likely be available by the time the article is published. But we have to finish the galley proof for the print magazine now, hence the placeholder!

Until just recently, here in the summer of 2015, the Visual Studio Online build system didn't have support for cross-platform apps like those created with Xamarin. That support has now been added.

When you're on VSO's Build tab for a project, you'll see "Build definitions" and "XAML definitions." The latter, XAML definitions, are those that worked with the previous build system that does not have cross-platform support.

VSO-Build1

If you create a new definition with the big green +, you'll be presented with the new list of templates:

VSO-Build2

Clearly you can see the options for building Xamarin.Android and Xamarin.iOS projects, along with the Xcode option for building a straight iOS native app.

What about Xamarin for Windows? I'm told you can use the Visual Studio template for that, but I haven't tried this myself. In any case, when you use one of the Xamarin templates you'll be required to enter your Xamarin credentials to activate the necessary platform licenses:

VSO-Build3

Anyway, that's all I'll say for now because I haven't tried out all of this myself. Again, proper documentation will be appearing soon on MSDN for all the details.

 


This blog post is an addendum to the article, Build a Xamarin App with Authentication and Offline Support, to be published in MSDN Magazine, September 2015. I'll update this post with a link once the article is available. Part 1, Cloud-Connected Mobile Apps – Create a Web Service with Azure Web Apps and WebJobs, which discusses the backend of the project, is available now.

As promised, there are a few points of interest in the app that have not been discussed previously.

First, we implemented some niceties for displaying items both in the home page list and on the item page:

  • When processing data from the backend in DataModel.ProcessItems, the app generates a description from the first 100 characters of the body. Because that body is typically HTML and the description is plain text, we use a one-line regular expression replacement to do a quick strip of the tags (this is the private function StripHTML in sync.cs):
Regex.Replace(source, "<[^>]*>", string.Empty).Replace("n", " ");
  • Similarly, we noticed during testing that item titles can have smart quotes in them, but the text field in the ListView control will display their escape sequence instead. So another little one-liner (CleanEscapes in sync.cs) takes care of this:
source.Replace("&#39;", """).Replace("&quot;", """);
  • The item page displays the name of the item’s source (such as StackOverflow or Twitter), and to make it easy to go to that source directly the ItemPage constructor makes it a hyperlink with the following code:
this.ProviderLabel.GestureRecognizers.Add(new TapGestureRecognizer
{
    Command = new Command (() =>
    {
        Device.OpenUri(new System.Uri(viewModel.Url));
    })
});
  • It’s also very likely that the item’s HTML displayed in the ItemPage’s WebView will contain hyperlinks. Left to themselves, these hyperlinks will cause navigation within the WebView, but because there are no navigation controls the user wouldn’t be able to return to the item itself. To prevent this, the following code redirects navigations within the WebView to the default browser. The trick (mentioned in Altostratus Extra #1) is that the exact behavior of the WebView varies by platform: on iOS and Windows Phone, a Navigating event will be raised when the webview is first loaded with content from the app, whereas on Android you get the event only for navigations within the WebView once it’s loaded. We thus use Device.OnPlatform to initialize a flag that says whether to ignore the first event:
private Boolean navigateToBrowser = Device.OnPlatform<Boolean>(false, true, false); 

// ...

wv.Navigating += (Object sender, WebNavigatingEventArgs e) =>
{
    if (navigateToBrowser)
    {
       Device.OpenUri(new System.Uri(e.Url));
       e.Cancel = true;
    }

    navigateToBrowser = true;
};

And second, although we wanted a change of configuration to trigger a sync with the backend, we obviously don’t want to do this with every UI activity in the configuration page. Instead, a sync should only be triggered if the user actually changes something in the configuration when they return to the home page.

To implement this, the UserPreferences class in the data model, which matches the data exchanged with the backend, supports cloning an instance (its Clone method) and comparing to another instance (its IsEqual method). The Configuration page uses these within its OnAppearing and OnDisappearing events to have the data model refresh itself if the user made changes when they return to the home page. This way, the user could toggle categories, change the conversation limit, and even log in and log off again, but if ultimately the user returns to the home page with exactly the same configuration, nothing needs to happen in the data model or the home page UI.


This blog post is an addendum to the article, Build a Xamarin App with Authentication and Offline Support, to be published in MSDN Magazine, September 2015. I’ll update this post with a link once the article is available. Part 1, Cloud-Connected Mobile Apps – Create a Web Service with Azure Web Apps and WebJobs, which discusses the backend of the project, is available now.

The OAuth credential flow in Altostratus is as follows, using Facebook as an example. Once the user is

  1. The mobile client app displays a web browser control and navigates to a known URL on the backend.
  2. The backend redirects to the Facebook sign-in page where the user signs in with his or her Facebook credentials.
  3. The backend does sends requests to Facebook to retrieve an access token.
  4. The client app redirects the browser back to the backend’s endpoint. The redirect URL includes the access token in the fragment hash.
  5. The client app has been waiting for the redirect. At this point, it parses the access token from the URL fragment.
  6. If the user is not yet registered with the backend, the client app sends a registration request to the backend. The backend creates a database entry for the user and issues a second access token. This step only happens on the first login.
  7. Once the user is registered, the client app includes the access token to make authenticated requests.

Put graphically:

AltoStratus_OAuth_Flow


This blog post is an addendum to the article, Build a Xamarin App with Authentication and Offline Support, to be published in MSDN Magazine, September 2015. I'll update this post with a link once the article is available. Part 1, Cloud-Connected Mobile Apps – Create a Web Service with Azure Web Apps and WebJobs, which discusses the backend of the project, is available now.

To summarize from the article: Xamarin.Forms is a framework that lets you use a single code base to implement apps with UI on multiple target platforms. However, as written in Part 2, "shared code doesn’t much reduce the effort needed to thoroughly test the app on each target platform: that part of your process will take about as long as it would if you wrote each app natively. Also, because Xamarin.Forms is quite new [it first came out in May 2014], you may find platform-specific bugs or other behaviors that you'll need to handle in your code."

That particular statement comes from direct experience! Here are the behaviors we encountered with Altostratus and had to manage in the client app code (http://aka.ms/altostratusproject, in the MobileClient project):

  • The Xamarin.Forms ListView control supports grouping, but not invocation of group headers as is supported on some individual platforms. We would have liked to enable this feature in the app, but chose not to until Xamarin.Forms makes it work.
  • ListView headers appear on iOS 7 but don’t appear on iOS 8. This is a known bug in Xamarin.Forms.
  • On iOS and Windows Phone, the OnAppearing and OnDisappearing events for page navigations happen in the expected order: the originating page receives an OnDisappearing before the target page receives an OnAppearing. There’s a significant Xamarin.Forms bug on Android (and here's a duplicate bug) that causes the target’s OnAppearing to fire first. For this reason it’s necessary in the app's Configuration page to update the page’s overall changed status with every UI activity, rather than just check it once within OnDisappearing. This clearly causes a lot of extra churn.
  • On all platforms, setting the Minimum property of a Slider control (as used on the Configuration page) will throw an exception unless Maximum is already set to a higher value. This makes it difficult to set the values through data binding, because the order in which XAML binding statements are processed is indeterminate. For this reason, these properties are set in code rather than through data binding. See https://bugzilla.xamarin.com/show_bug.cgi?id=21181 and https://bugzilla.xamarin.com/show_bug.cgi?id=23665.
  • Data binding the items in a drop-down listbox is not supported at the time of writing.
  • The Xamarin.Forms WebView control is written to fire a Navigating event when the user attempts to navigate a link within the WebView. In the mobile client, we capture this event to specifically disallow navigation directly within the control and redirect the navigation to the default browser, see the code in Altostratus Extra #3. (The code for this is in the ItemPage constructor in ItemPage.xaml.cs.) However, on iOS and Windows Phone 8.1, but not Android, the Navigating event is also raised when the WebView is initialized from local content. This means that on those platforms we want to ignore the first Navigating event, whereas on Android we want to pay attention to all of them. So we just set a flag (navigateToBrowser) within the ItemPage constructor to control whether we delegate a navigation to the browser.

 

The lesson to be learned here is that when platform technologies always have their bugs, especially new ones but often mature ones as well. If something you think should be happening isn't, or you encounter some other behavior that seems odd and especially those that are inconsistent between operating systems, check into the applicable bug database like bugzilla.xamarin.com, or check on the applicable forums. It'll save you plenty of frustration. :)


We just published Part 1 of a two-part series in MSDN Magazine that covers the cloud-connected mobile app project I worked on earlier this year, which we called “Altostratus” (the name of an interesting cloud).

Part 1 is entitled “Cloud-Connected Mobile Apps – Create a Web Service with Azure Web Apps and WebJobs”, and is on https://msdn.microsoft.com/en-us/magazine/mt185572. I didn’t write much of this first article, personally, as it covers the backend part of the project and I worked primarily on the client.

The Xamarin client is the focus of Part 2 that will be published in early September. In advance of that, you’ll see a couple posts here with some extra material that didn’t fit into the ~5000 words of the article. I’ll be making those posts shortly because we want to make sure the URLs are accurate before going to print. :)

The code for both the backend and the client are available now via https://aka.ms/altostratusproject (which goes to GitHub). It is an open project, so if you see anything to improve, we’re happy to accept contributions.

Enjoy