locked
Dynamically getting display dimensions in C#

    Question

  • Hi
    I am developing an windows app (I am basically porting my android app to windows store), for the same I have developed C# class library for data processing and background tasks, but to design GUI I want to get the current display size and set the other GUI elements accordingly. So here are two things I want to know,

    1) How to get the display dimensions (in px) available for my app (Excluding notification bar an all).

    2) How to dynamically set the dimensions of layout components in C#.

    Monday, February 2, 2015 8:37 PM

Answers

  • I want to maintain look of GUI in every device without leaving any empty margin, and I want the size of button and all to be predefined, not flexible

    The easy and recommended way is to use a Grid (&c) to set up the margins and layout the buttons. Their sizes can still all be pre-defined.

    If you really want to do it the hard way then you can get the window size from your CoreWindow's Bounds property and do the math to place the buttons. Put them in a Canvas and set the Canvas.Left and Canvas.Top properties to place them. I would strongly recommend looking into the layout features provided rather than trying to reinvent your own.

    See Quickstart: Defining layouts and the links I provided in my previous post for more information.

    --Rob

    Wednesday, February 4, 2015 3:14 AM
    Owner

All replies

  • You should base your layout on the size of the window, not of the device. You can get the window bounds from CoreWindow.Bounds.

    That said, you generally don't need the bounds to do proper layout beyond determining the general aspect ratio. Instead use flexible layout controls such as Grids or high level controls such as GridViews to lay out the controls.

    See Quickstart: Designing apps for different window sizes for an overview.


    Monday, February 2, 2015 8:49 PM
    Owner
  • I agree with using flexible layout control, but I want to maintain look of GUI in every device without leaving any empty margin, and I want the size of button and all to be predefined, not flexible. for the same am planning to use dynamic button creation in C# for my windows phone app. I have already done it in my android app which is going quite good because of that. so please suggest me how to,

    1) Get windows (Available space for my GUI) in C# (For windows phone app)

    2) How to create buttons dynamically and set size, text and background.

    In android designing GUI is most difficult task if you want to use flexible controls and maintain the look of layout across all the devices so I opt to use dynamic layout creation, I know how to it in android but have no idea about windows phone. I am developing my first windows app and stuck on this issue. Please help.

    and thank you for your suggestions. :-)

    Tuesday, February 3, 2015 2:03 PM
  • I want to maintain look of GUI in every device without leaving any empty margin, and I want the size of button and all to be predefined, not flexible

    The easy and recommended way is to use a Grid (&c) to set up the margins and layout the buttons. Their sizes can still all be pre-defined.

    If you really want to do it the hard way then you can get the window size from your CoreWindow's Bounds property and do the math to place the buttons. Put them in a Canvas and set the Canvas.Left and Canvas.Top properties to place them. I would strongly recommend looking into the layout features provided rather than trying to reinvent your own.

    See Quickstart: Defining layouts and the links I provided in my previous post for more information.

    --Rob

    Wednesday, February 4, 2015 3:14 AM
    Owner
  • Ok, I will follow the recommended method.
    Thank you

    Wednesday, February 4, 2015 6:27 AM