"Win 10 App development" How to know which platform UAP is running on

Source: Internet
Author: User

Modern applications facing the 22nd century can run on multiple devices at the same time, so there's a question for friends: sometimes we really need to determine which platform the UAP application is running on. Although we do not need to do this in most cases, some special cases have to be considered. For example, a list of data, I would like to show in a horizontal list when running on the desktop, but if it is running on the phone, it will be displayed in a vertical list.

In other words, we can only analyze two situations:

First, the desktop. (desktops, notebooks, tablets, game consoles, ad-hoc machines, navigators ...) )

Second, the mobile environment. It's actually a cell phone.

So, what are the ways to judge? Don't worry, old Zhou Xian tell you, the method is relatively simple. Let's look at a piece of code first:

 var  context = Resourcecontext.getforcu            Rrentview ();  string  s = string  .            Empty;  foreach  (var  item in   context. Qualifiervalues) {s  + = Item. Key +  :  "  + item.                Value; s  + =  \n   "             = s; 

The Resourcecontext class, under the Windows.ApplicationModel.Resources.Core namespace, is the API for accessing qualified resources with the current application, or it can be understood as some configuration information for the application. What is the language, the Japanese is also Chinese, what is the theme of the tone, light or dark, what proportion, scale 100% or 180% ...

Call the Resourcecontext.getforcurrentview () method to get the Resourcecontext instance directly, and then get a dictionary data from the Qualifiervalues property: Key is a string, and value is a string. The Qualifiervalues property represents the set of qualifiers for the resource.

The high-savvy friend may have guessed the mystery, but can not guess the urgency, now we use the above code to do two things, you will understand the two things done.

First thing, run the app on the local computer and the Tablet emulator, and get the results as follows:

The second thing, run on the phone simulator and get the result as follows:

I don't know if you understand. No, if you have not seen the mystery, the old week will be revealed: in the Resource qualifier dictionary, there is a record key is devicefamily, if run on the desktop, its value is desktop, if it is running on the phone, Value is mobile.

What, now you understand?

So, you guess, if you judge the device that UAP is running, you should think of a method.

Old weeks give an example, the interface has a StackPanel container, which put a few square cakes. If running on the desktop, the sesame cake on the horizontal, if running on the phone, sesame cake on the vertical put, to facilitate user drooling.

The XAML code is as follows:

        <StackPanelMargin= " the"Name= "Panel">            <RectangleMargin= "+"Width= " the"Height= " the"Fill= "Yellow"/>            <RectangleMargin= "+"Width= " the"Height= " the"Fill= "Red"/>            <RectangleMargin= "+"Width= " the"Height= " the"Fill= "Pink"/>            <RectangleMargin= "+"Width= " the"Height= " the"Fill= "Orange"/>        </StackPanel>

The processing code is as follows:

            //get the resource context objectResourceContext Rescontext =Resourcecontext.getforcurrentview (); //access the value of the key named Devicefamily            stringValue = rescontext.qualifiervalues["devicefamily"]; //Analysis            if(Value = ="Desktop") {panel. Orientation=Orientation.horizontal; }            Else{panel. Orientation=orientation.vertical; }


OK, first, run it on your local computer and the results are as follows:

Then, running on the phone simulator, the results are as follows:

It seems that this devicefamily is really family.

Example of this article: Http://files.cnblogs.com/files/tcjiaan/deviceFamilySmp.zip

"Win 10 App development" How to know which platform UAP is running on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.