Sergiy Baydachnyy

Blog about technologies

Tailored views or one more way to create Universal interfaces (part 2)

leave a comment »

In the previous post I showed how to create tailored views for known families of devices. But if you want to apply tailored views based on your custom logic you can make it as well.

First of all you need to follow naming rules in order to associate a new view with the existing code-behind file. In order to do it just use the following rule: <initial page name>.<any extension>.xaml. Don’t use “DeviceFamily-“ for your own extensions – probably it is reserved by Visual Studio and you will get runtime error if you use it. For example, if you create a new view for MainPage.xaml you can use MainPage.Raspberry.xaml name.

clip_image002

Once you add a new view to your project you can see that Visual Studio will generate one more InitializeComponent method.

clip_image003

At this step Visual Studio doesn’t check anything – just adds a new method, which should be used for different device families. Of course, if you use predefined device family, InitializeComponent method with parameter will be called “automatically” but in case of your own logic you can call it directly from your code. Of course, you need to change existing constructor of the page and call InitializeComponent in the following way:

this.InitializeComponent(new System.Uri("ms-appx:///MainPage.Raspberry.xaml"));

Using this approach you can implement any custom logic in your constructor and apply different views based on different devices, parameters etc.

Advertisements

Written by Sergiy Baydachnyy

08/20/2015 at 4:42 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: