简体   繁体   中英

How to get resolution of silverlight + XNA application

I am developing a game using Silverlight and XNA framework. I wants to get the height and width of the mobile screen for running the app in any device. Means scaling of application should be supported to all devices.

public partial class GamePage : PhoneApplicationPage
{
   public static ContentManager contentManager;
   GameTimer timer;
   SpriteBatch spriteBatch;
   GraphicsDeviceManager graphics;

   public GamePage()
   {
        InitializeComponent();

        //error at below line 
        graphics = new GraphicsDeviceManager(this);

        contentManager = (System.Windows.Application.Current as App).Content;

        rand = new Random();

        // Create a timer for this page
        timer = new GameTimer();
        timer.UpdateInterval = TimeSpan.FromTicks(333333);
        timer.Update += OnUpdate;
        timer.Draw += OnDraw;
    }

Please note I am not sure if this is available on mobile. GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width or Height will give you the devices screen resolution,

graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
graphics.ApplyChanges();

Edit:

Using silverlight you must do this:

Application.Current.Host.Content.ActualHeight;
Application.Current.Host.Content.ActualWidth;

Also note the silverlight equivalent of GraphicsDevciceManager is SharedGraphicsDeviceManager

Application.Current.RootVisual.RenderSize should give you that information.

Or you can try:

int ScreenWidth = Application.Current.Host.Content.ActualWidth;  
int ScreenHeight = Application.Current.Host.Content.ActualHeight;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM