Reputation: 31313
Referencing code found at...Highlight a Route on a Map
They show...
var customMap = new CustomMap
{
WidthRequest = App.ScreenWidth
};
App.ScreenWidth
isn't available any longer. Has it been replaced with Application.Current.MainPage.Width
?
Upvotes: 5
Views: 2848
Reputation: 864
I believe the alternative, the OP suggested in the question, is the correct answer. look here
Update: One problem with this solution is that it will return -1 when MainPage did not appear yet. In this case, @SushiHangover solution is better.
Upvotes: 0
Reputation: 628
Most simplest and accurate way to get device height & width in PCL:
using Xamarin.Forms;
namespace ABC
{
public class MyPage : ContentPage
{
private double _width;
private double _height;
public MyPage()
{
Content = new Label
{
WidthRequest = _width,
Text = "Welcome to Xamarin.Forms!"
};
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
_width = width;
_height = height;
}
}
}
Upvotes: 7
Reputation: 74094
In that demo, App.ScreenWidth
and App.ScreenHeight
are static variables defined in the App
class and assigned in the native
projects:
iOS app project:
App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height
Android app project:
App.ScreenWidth = (width - 0.5f) / density;
App.ScreenHeight = (height - 0.5f) / density;
Ref: https://github.com/xamarin/recipes/search?p=2&q=ScreenWidth&utf8=✓
Upvotes: 9