Device Class
- PDF for offline use:
- Sample Code:
- Related APIs:
Let us know how you feel about this.
0/250
last updated: 2016-02
The Device class contains a number of properties and methods to help developers
customize layout and functionality on a per-platform basis.
In addition to methods and properties to target code
at specific hardware types and sizes, the Device
class includes the BeginInvokeOnMainThread
method which should be used when interacting with UI controls
from background threads.
Device.Idiom
The Device.Idiom can be used to alter layouts or functionality depending on
whether the device is a phone or a tablet. The TargetIdiom enumeration contains
four values:
- Phone – iPhone, iPod touch, Windows Phone, Android devices narrower than 600 dips^
- Tablet – iPad, Windows 8.1 computers, Android devices wider than 600 dips^
- Desktop – only returned in UWP apps (currently in Preview) on Windows 10
- Unsupported – unused
^ dips is not necessarily the physical pixel count
Idiom is especially useful for building layouts that take advantage of larger screens, like this:
if (Device.Idiom == TargetIdiom.Phone) {
// layout views vertically
} else {
// layout views horizontally for a larger display (tablet or desktop)
}
Device.OS
Device.OS will be set to one of the values of the TargetPlatform enumeration:
- iOS
- Android
- WinPhone (returned for Windows 8 Silverlight)
- Windows (returned for Windows 8.1, Windows Phone 8.1, and all UWP/Windows 10 devices)
This enables platform-specific checks to alter layout or functionality, such as these if statements:
if (Device.OS == TargetPlatform.iOS) {
// move layout under the status bar
stackLayout.Padding = new Thickness (0, 20, 0, 0);
}
Font font;
if (Device.OS == TargetPlatform.iOS) {
font = Font.OfSize ("MarkerFelt-Thin", Device.GetNamedSize (NamedSize.Medium, typeof(Label)));
} else {
font = Font.SystemFontOfSize (Device.GetNamedSize (NamedSize.Medium, typeof(Label)));
}
label.FontFamily = font.FontFamily;
label.FontAttributes = font.FontAttributes;
label.FontSize = font.FontSize;
Device.OnPlatform
Device.OnPlatform is a generic method that has three optional parameters:
iOS, Android, and WinPhone. It can be used to provide platform-specific values
// left and right padding: 5; top padding: 20 (only on iOS)
layout.Padding = new Thickness (5, Device.OnPlatform(20,0,0), 5, 0),
It also accepts Action parameters to execute multiple platform-specific
instructions. This overload accepts an additional Default parameter.
Device.OnPlatform(
Android: () =>{
PositiveBalance = PositiveBalance.AddLuminosity(0.3);
NegativeBalance = NegativeBalance.AddLuminosity(0.3);
SubTitle = Color.FromRgb(115, 129, 130);
},
WinPhone: () =>{
PositiveBalance = PositiveBalance.AddLuminosity(0.3);
NegativeBalance = NegativeBalance.AddLuminosity(0.3);
}
);
OnPlatform does not currently support differentiating
the Windows 8.1, Windows Phone 8.1, and UWP/Windows 10 platforms.Using Xaml
OnPlatform can also be used in Xaml as shown on the BoxView control below
which sets two different attributes using . There is a built-in type converter
for Color so we can set that attribute using BoxView.Color element syntax.
The WidthRequest property is a double so that is set using x:TypeArguments="x:Double":
<BoxView HorizontalOptions="Center">
<BoxView.Color>
<OnPlatform x:TypeArguments="Color"
iOS="Green"
Android="#738182"
WinPhone="Accent" />
</BoxView.Color>
<BoxView.WidthRequest>
<OnPlatform x:TypeArguments="x:Double"
iOS="30"
Android="40"
WinPhone="50" />
</BoxView.WidthRequest>
</BoxView>
Device.Styles
The Styles property contains built-in style
definitions that can be applied to some controls' (such as Label)
Style property. The available styles are:
- BodyStyle
- CaptionStyle
- ListItemDetailTextStyle
- ListItemTextStyle
- SubtitleStyle
- TitleStyle
Device.GetNamedSize
GetNamedSize can be used when setting FontSize
in C# code:
myLabel.FontSize = Device.GetNamedSize (NamedSize.Small, myLabel);
someLabel.FontSize = Device.OnPlatform (
24, // hardcoded size
Device.GetNamedSize (NamedSize.Medium, someLabel),
Device.GetNamedSize (NamedSize.Large, someLabel)
);
Device.OpenUri
The OpenUri method can be used to trigger operations on the underlying platform,
such as open a URL in the native web browser (Safari on iOS or Internet on Android).
Device.OpenUri(new Uri("https://evolve.xamarin.com/"));
The WebView sample includes an example using OpenUri to open URLs and also trigger phone calls.
The Maps sample also uses Device.OpenUri to display maps and directions using the native Maps apps on iOS and Android.
Device.StartTimer
The Device class also has a StartTimer method which provides a simple way to trigger time-dependent tasks that works in Xamarin.Forms common code (including PCLs). Pass a TimeSpan to set the interval and return true to keep the timer running or false to stop it after the current invocation.
Device.StartTimer (new TimeSpan (0, 0, 60), () => {
// do something every 60 seconds
return true; // runs again, or false to stop
});
If the code inside the timer interacts with the user-interface (such as setting the text of a Label or displaying an alert) it should be done inside a BeginInvokeOnMainThread expression (see below).
Device.BeginInvokeOnMainThread
User interface elements should never be accessed by background threads, such as
code running in a timer or a completion handler for asynchronous operations like
web requests. Any background code that needs to update the user interface should
be wrapped inside BeginInvokeOnMainThread.
This is the equivalent of InvokeOnMainThread on iOS, RunOnUiThread on Android,
and Dispatcher.BeginInvoke on Windows Phone.
The Xamarin.Forms code is:
Device.BeginInvokeOnMainThread ( () => {
// interact with UI elements
});
Note that methods using async/await do not need to use BeginInvokeOnMainThread
if they are running from the main UI thread.
Summary
The Xamarin.Forms Device class allows fine-grained control over functionality
and layouts on a per-platform basis - even in common code (either PCL or Shared Projects).
Let us know how you feel about this.
0/250
Xamarin Workbook
If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.