How to detect screen orientation












1















I am trying differenciate between the device being in Landscape or Portrait mode. 180° rotations don't matter to me, just which side is longer.



I have tried



SimpleOrientation OReading = SimpleOrientationSensor.GetDefault().GetCurrentOrientation();

if(OReading == SimpleOrientation.NotRotated || OReading == SimpleOrientation.Rotated180DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Landscape";
if (OReading == SimpleOrientation.Rotated90DegreesCounterclockwise || OReading == SimpleOrientation.Rotated270DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Portrait";


but the first line already throws a NullReferenceException. What would be the correct way to do this?



I have a .NET component that could technically also perform this, but I would prefer to do it in UWP if possible.



Note that I am not running in a simulator, but launching the App on a device that supports rotation.



I actually don't need the sensor data though, I just want to know how the screen orientation is, which can be different from the sensor data.



Edit:



public async void ChangedAsync(AppResourceGroupInfoWatcher sender, AppResourceGroupInfoWatcherExecutionStateChangedEventArgs args)
{
DisplayInformation current = DisplayInformation.GetForCurrentView();


This also does not work, and in UWP it gives me the crash




Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.




The function is getting called by an AppResourceGroupInfoWatcher.



I am using:



using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


targeting version 1803










share|improve this question

























  • Possible duplicate of SimpleOrientationSensor returning Null

    – Xiaoy312
    Nov 23 '18 at 16:41











  • Why not read the Screen.Bounds?

    – Poul Bak
    Nov 23 '18 at 16:59











  • @PoulBak This does only work in the .NET part, right?

    – DunDunDam
    Nov 23 '18 at 17:52











  • Yes, Screen is a .net class.

    – Poul Bak
    Nov 23 '18 at 18:23
















1















I am trying differenciate between the device being in Landscape or Portrait mode. 180° rotations don't matter to me, just which side is longer.



I have tried



SimpleOrientation OReading = SimpleOrientationSensor.GetDefault().GetCurrentOrientation();

if(OReading == SimpleOrientation.NotRotated || OReading == SimpleOrientation.Rotated180DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Landscape";
if (OReading == SimpleOrientation.Rotated90DegreesCounterclockwise || OReading == SimpleOrientation.Rotated270DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Portrait";


but the first line already throws a NullReferenceException. What would be the correct way to do this?



I have a .NET component that could technically also perform this, but I would prefer to do it in UWP if possible.



Note that I am not running in a simulator, but launching the App on a device that supports rotation.



I actually don't need the sensor data though, I just want to know how the screen orientation is, which can be different from the sensor data.



Edit:



public async void ChangedAsync(AppResourceGroupInfoWatcher sender, AppResourceGroupInfoWatcherExecutionStateChangedEventArgs args)
{
DisplayInformation current = DisplayInformation.GetForCurrentView();


This also does not work, and in UWP it gives me the crash




Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.




The function is getting called by an AppResourceGroupInfoWatcher.



I am using:



using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


targeting version 1803










share|improve this question

























  • Possible duplicate of SimpleOrientationSensor returning Null

    – Xiaoy312
    Nov 23 '18 at 16:41











  • Why not read the Screen.Bounds?

    – Poul Bak
    Nov 23 '18 at 16:59











  • @PoulBak This does only work in the .NET part, right?

    – DunDunDam
    Nov 23 '18 at 17:52











  • Yes, Screen is a .net class.

    – Poul Bak
    Nov 23 '18 at 18:23














1












1








1








I am trying differenciate between the device being in Landscape or Portrait mode. 180° rotations don't matter to me, just which side is longer.



I have tried



SimpleOrientation OReading = SimpleOrientationSensor.GetDefault().GetCurrentOrientation();

if(OReading == SimpleOrientation.NotRotated || OReading == SimpleOrientation.Rotated180DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Landscape";
if (OReading == SimpleOrientation.Rotated90DegreesCounterclockwise || OReading == SimpleOrientation.Rotated270DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Portrait";


but the first line already throws a NullReferenceException. What would be the correct way to do this?



I have a .NET component that could technically also perform this, but I would prefer to do it in UWP if possible.



Note that I am not running in a simulator, but launching the App on a device that supports rotation.



I actually don't need the sensor data though, I just want to know how the screen orientation is, which can be different from the sensor data.



Edit:



public async void ChangedAsync(AppResourceGroupInfoWatcher sender, AppResourceGroupInfoWatcherExecutionStateChangedEventArgs args)
{
DisplayInformation current = DisplayInformation.GetForCurrentView();


This also does not work, and in UWP it gives me the crash




Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.




The function is getting called by an AppResourceGroupInfoWatcher.



I am using:



using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


targeting version 1803










share|improve this question
















I am trying differenciate between the device being in Landscape or Portrait mode. 180° rotations don't matter to me, just which side is longer.



I have tried



SimpleOrientation OReading = SimpleOrientationSensor.GetDefault().GetCurrentOrientation();

if(OReading == SimpleOrientation.NotRotated || OReading == SimpleOrientation.Rotated180DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Landscape";
if (OReading == SimpleOrientation.Rotated90DegreesCounterclockwise || OReading == SimpleOrientation.Rotated270DegreesCounterclockwise)
ApplicationData.Current.LocalSettings.Values["Orientation"] = "Portrait";


but the first line already throws a NullReferenceException. What would be the correct way to do this?



I have a .NET component that could technically also perform this, but I would prefer to do it in UWP if possible.



Note that I am not running in a simulator, but launching the App on a device that supports rotation.



I actually don't need the sensor data though, I just want to know how the screen orientation is, which can be different from the sensor data.



Edit:



public async void ChangedAsync(AppResourceGroupInfoWatcher sender, AppResourceGroupInfoWatcherExecutionStateChangedEventArgs args)
{
DisplayInformation current = DisplayInformation.GetForCurrentView();


This also does not work, and in UWP it gives me the crash




Windows.Graphics.Display: GetForCurrentView must be called on a thread that is associated with a CoreWindow.




The function is getting called by an AppResourceGroupInfoWatcher.



I am using:



using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;


targeting version 1803







c# .net uwp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:56







DunDunDam

















asked Nov 23 '18 at 16:32









DunDunDamDunDunDam

62




62













  • Possible duplicate of SimpleOrientationSensor returning Null

    – Xiaoy312
    Nov 23 '18 at 16:41











  • Why not read the Screen.Bounds?

    – Poul Bak
    Nov 23 '18 at 16:59











  • @PoulBak This does only work in the .NET part, right?

    – DunDunDam
    Nov 23 '18 at 17:52











  • Yes, Screen is a .net class.

    – Poul Bak
    Nov 23 '18 at 18:23



















  • Possible duplicate of SimpleOrientationSensor returning Null

    – Xiaoy312
    Nov 23 '18 at 16:41











  • Why not read the Screen.Bounds?

    – Poul Bak
    Nov 23 '18 at 16:59











  • @PoulBak This does only work in the .NET part, right?

    – DunDunDam
    Nov 23 '18 at 17:52











  • Yes, Screen is a .net class.

    – Poul Bak
    Nov 23 '18 at 18:23

















Possible duplicate of SimpleOrientationSensor returning Null

– Xiaoy312
Nov 23 '18 at 16:41





Possible duplicate of SimpleOrientationSensor returning Null

– Xiaoy312
Nov 23 '18 at 16:41













Why not read the Screen.Bounds?

– Poul Bak
Nov 23 '18 at 16:59





Why not read the Screen.Bounds?

– Poul Bak
Nov 23 '18 at 16:59













@PoulBak This does only work in the .NET part, right?

– DunDunDam
Nov 23 '18 at 17:52





@PoulBak This does only work in the .NET part, right?

– DunDunDam
Nov 23 '18 at 17:52













Yes, Screen is a .net class.

– Poul Bak
Nov 23 '18 at 18:23





Yes, Screen is a .net class.

– Poul Bak
Nov 23 '18 at 18:23












1 Answer
1






active

oldest

votes


















0














This should work.



bool isInLandscapeMode = 
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation ==
Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape;

ApplicationData.Current.LocalSettings.Values["Orientation"] = isInLandscapeMode ?
"Landscape" : "Portrait";





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450194%2fhow-to-detect-screen-orientation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This should work.



    bool isInLandscapeMode = 
    Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation ==
    Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape;

    ApplicationData.Current.LocalSettings.Values["Orientation"] = isInLandscapeMode ?
    "Landscape" : "Portrait";





    share|improve this answer




























      0














      This should work.



      bool isInLandscapeMode = 
      Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation ==
      Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape;

      ApplicationData.Current.LocalSettings.Values["Orientation"] = isInLandscapeMode ?
      "Landscape" : "Portrait";





      share|improve this answer


























        0












        0








        0







        This should work.



        bool isInLandscapeMode = 
        Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation ==
        Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape;

        ApplicationData.Current.LocalSettings.Values["Orientation"] = isInLandscapeMode ?
        "Landscape" : "Portrait";





        share|improve this answer













        This should work.



        bool isInLandscapeMode = 
        Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Orientation ==
        Windows.UI.ViewManagement.ApplicationViewOrientation.Landscape;

        ApplicationData.Current.LocalSettings.Values["Orientation"] = isInLandscapeMode ?
        "Landscape" : "Portrait";






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 2:48









        chaosifierchaosifier

        925726




        925726






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450194%2fhow-to-detect-screen-orientation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'