0 Comments

NXMLFormatter is an open-source XML Formatter and beautifier written with C#. The DLL is available as a portable class library, so you can use it from UWP, ASP.NET etc.

Usage

  1. Add reference to NXMLFormatter.dll.
  2. var formattedXML = NXMLFormatter.Formatter.Format(originalXML);

Project doesn't have any external references and it uses only the features provided by the .NET Framework.

NuGet

NXMLFormatter is available through the NuGet with the package name NXMLFormatter. Type "install-package NXMLFormatter" to install it.

Example

Input
<catalog><book id="bk101"><author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">   <author>Ralls, Kim</author>   <title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description></book>
<book id="bk103">   <author>Corets, Eva</author>   <title>Maeve Ascendant</title>   <genre>Fantasy</genre>
   <price>5.95</price>   <publish_date>2000-11-17</publish_date>
   <description>After the collapse of a nanotechnology society in England, the young survivors lay the 
   foundation for a new society.</description></book></catalog>
Output
<catalog>
  <book
    id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications with XML.</description>
  </book>
  <book
    id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
  </book>
  <book
    id="bk103">
    <author>Corets, Eva</author>
    <title>Maeve Ascendant</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-11-17</publish_date>
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the 
   foundation for a new society.</description>
  </book>
</catalog>

Implementation

Under the covers the library uses XmlWriter to format the code.

Licenses

The library is distributed under the terms of the MIT License (see mit.txt).

Source code

NXMLFormatter’s source code is available from GitHub.

0 Comments

Problem

When you create a Universal Windows Platform app and run it in Windows 10, you may notice that the logo displayed on taskbar is not full size.

For example, if you have this beautiful 24x24 logo:

image

And you set that as your taskbar logo:

image

And then run the app, you’ll notice that it looks out of place:

image

Two problems:

  1. The logo is not full size
  2. The remaining space is filled with your accent color

Solution

The problem can be fixed by renaming the logo file. When you set the logo through Package.appxmanifest’s designer, the asset will receive file name Square44x44Logo.targetsize-24.png:

image

Just rename the file to Square44x44Logo.targetsize-24_altform-unplated.png:

image

And now when you ran the app, you should see that your icon fits the taskbar nicely:

image

0 Comments

imageThis post will show you how to use repositioning to create a responsive layout for your XAML Universal Windows app (UWP) using Grid and AdaptiveTrigger.

Background

With Windows 10 and its UWP stack, making your app look nice on both mobile and desktop is one of the core requirements. The Responsive design 101 for Universal Windows Platform (UWP) apps guide on MSDN outlines six different ways for responsive design:

  • Reposition
  • Resize
  • Reflow
  • Reveal
  • Replace
  • Re-architect

In this post we’ll use the familiar Grid-control with the AdaptiveTrigger to reposition our app’s content. On desktops and tablets the app will use 2-column layout. On mobile devices (or when user resizes the app to have a small window) the second column drops under the first one.

AdaptiveTrigger

AdaptiveTrigger is a new addition in Windows 10. You can use AdaptiveTrigger to automatically change the VisualState when the app’s width or height changes. For example the following Channel9-video contains good information about the AdaptiveTrigger.

The code

Here’s out app’s simple layout:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="MainGrid">

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid x:Name="FirstGrid" Grid.Column="0" Grid.Row="0" Background="#0078d7" />
        <Grid x:Name="SecondGrid" Grid.Column="1" Grid.Row="0" Background="#107C10" />

    </Grid>

Without any VisualStates or AdaptiveTriggers, the app will look the same on all screen sizes:

image

But what we want is to reposition our SecondGrid under the FirstGridon mobile devices. To do this, we need to change the Grid.Column and Grid.Row of the SecondGrid:

                        <Setter Target="SecondGrid.(Grid.Column)" Value="0"/>
                        <Setter Target="SecondGrid.(Grid.Row)" Value="1"/>

Also, there’s no need for two columns so we modify the MainGrid for our needs:

                        <Setter Target="MainGrid.RowDefinitions[1].Height" Value="*"/>
                        <Setter Target="MainGrid.ColumnDefinitions[1].Width" Value="auto"/>

Last thing we do is change the margins to make things little prettier:

                        <Setter Target="MainGrid.Margin" Value="12"/>
                        <Setter Target="FirstGrid.Margin" Value="0 0 0 6"/>
                        <Setter Target="SecondGrid.Margin" Value="0 6 0 0"/>

And that’s it. Here’s all the VisualState changes combined. First the WideState (2-column layout):

                        <Setter Target="MainGrid.Margin" Value="24"/>
                        <Setter Target="MainGrid.RowDefinitions[1].Height" Value="auto"/>
                        <Setter Target="MainGrid.ColumnDefinitions[1].Width" Value="*"/>
                        <Setter Target="FirstGrid.Margin" Value="0 0 6 0"/>
                        <Setter Target="SecondGrid.Margin" Value="6 0 0 0"/>
                        <Setter Target="SecondGrid.(Grid.Column)" Value="1"/>
                        <Setter Target="SecondGrid.(Grid.Row)" Value="0"/>

And then the NarrowState (mobile layout):

                        <Setter Target="MainGrid.Margin" Value="12"/>
                        <Setter Target="FirstGrid.Margin" Value="0 0 0 6"/>
                        <Setter Target="SecondGrid.Margin" Value="0 6 0 0"/>
                        <Setter Target="MainGrid.RowDefinitions[1].Height" Value="*"/>
                        <Setter Target="MainGrid.ColumnDefinitions[1].Width" Value="auto"/>
                        <Setter Target="SecondGrid.(Grid.Column)" Value="0"/>
                        <Setter Target="SecondGrid.(Grid.Row)" Value="1"/>

With these few lines of XAML we have used repositioning to achieve a nice responsive layout.

image

Download source code

The full source code is available from GitHub.

https://github.com/mikoskinen/UWPResponsiveXAMLLayoutGridAdaptiveTrigger

0 Comments

We’re working with a Windows 8.1 Store App where we need to know the physical screen size of the display. Here’s some simple code which seems to do the trick in most cases:

            var width = Window.Current.Bounds.Width * (int)DisplayProperties.ResolutionScale / 100;
            var height = Window.Current.Bounds.Height * (int)DisplayProperties.ResolutionScale / 100;

            var dpi = DisplayInformation.GetForCurrentView().RawDpiY;

            var screenDiagonal = Math.Sqrt(Math.Pow(width / dpi, 2) +
                        Math.Pow(height / dpi, 2));

            return screenDiagonal.ToString();

Please note that the DPI will return 0 if you have two displays in duplicate mode: http://msdn.microsoft.com/en-us/library/windows/apps/windows.graphics.display.displayinformation.rawdpix

Portable Class Libraries are a great way to share common code between different platforms. But here lies the danger: A portable class library project cannot change the underlying platform.

Here’s a simple explanation of what that means.

Example project

The example application is built for Windows Forms, Windows RT and Windows Phone. The authentication mechanism is identical in every app so that is placed in a Portable Class Library project:

    public class WebTool
    {
        public async Task<string> GetAuthenticationKey()
        {
            var cookieContainer = new CookieContainer();
            var client = new HttpClient(new HttpClientHandler() { CookieContainer = cookieContainer });

            // Authenticate with the service
            await client.GetStringAsync("http://cookietest.api.domain.com/cookie/get");

            // Return the received cookie as authentication key
            var cookieHeader = cookieContainer.GetCookieHeader(new Uri("http://cookietest.api.domain.com/"));

            return cookieHeader;
        }
    }

Using this library doesn’t get easier: Each platform can call it with identical code:

            var webTool = new WebTool();

            var result = await webTool.GetAuthenticationKey();

            if (string.IsNullOrWhiteSpace(result))
                this.Result.Text = "Didn't receive authentication key!";

            else
                this.Result.Text = "Authentication key: " + result;

Few minutes and everything is done, so the testing phase begins.

First, Windows Forms:

image

Then Windows 8:

image

And last but not least, Windows Phone:

image

Uh oh.

Cookies and CookieContainer especially are different beasts in different platforms. Portable Class Library cannot hide that fact.

Have you encountered similar issues when dealing with Portable Class Library projects?