Search code examples
c#xamlwindows-phone-8longlistselector

How to get screen size and use as resources - WP8


I'm create an app using a LongListSelector with layout grid, I want to get only 3 items per row but the "gridcellsize" property is fixed in WP8, so I think I've to get the device screen size set 1/3 for each item, something like this:

<phone:LongListSelector ItemTemplate="{...}" LayoutMode="Grid" 
        GridCellSize="{StaticResource val},{StaticResource val}"/>

I wrote this in app.xaml.cs but I don't know how to make this to resources

Double val = (Application.Current.RootVisual.RenderSize.Width)/3;

Solution

  • In your App.xaml.xs you just do

    double yourWidth = (Application.Current.RootVisual.RenderSize.Width)/3;
    double yourHeight = //whatever you want your height to be
    Resources.Add("ScreenWidth", yourWidth);
    Resources.Add("ScreenHeight", yourHeight);
    

    Then in your xaml you do:

    {StaticResource ScreenWidth}
    

    and

    {StaticResource ScreenHeight}