HisTech Up

Xamarin.Forms: Handling the SizeChanged event

Introduction

user

Christian Hissibini

I am a Tech enthusiast who loves to blend Dev & Design on Web and Mobile Platforms. I am also a Windows Platform Dev MVP


LATEST POSTS

What are the differences between Data Mining, Machine Learning and Deep Learning? 11th February, 2018

What are the challenges of using Artificial Intelligence? 15th January, 2018

Mobile Development

Xamarin.Forms: Handling the SizeChanged event

Posted on .

There are a number of approaches that can be used to detect when the screen orientation changes in a Xamarin.Forms app. The simplest approach is to handle the SizeChanged event of the Page. This event fires when either the width or height of the Page changes. Then, an image can be chosen for display by comparing the height and width of the Page.

Detecting Screen Orientation

The following code example shows how to display an image based on the screen orientation:

public partial class HomePage : ContentPage
{
  public HomePage ()
  {
    InitializeComponent ();
    SizeChanged += OnSizeChanged;
  }

  void OnSizeChanged (object sender, EventArgs e)
  {
    image.Source = ImageSource.FromFile (Height > Width ? "portrait.jpg" : "landscape.jpg");
  }
}

The OnSizeChanged event handler sets the Image.Source property to a portrait or landscape image, depending on whether the Height of the ContentPage is greater than the Width of the ContentPage.

Summary

This recipe showed how to use Xamarin.Forms to detect the screen orientation and display an image based on the orientation.

 

 

Ref
https://developer.xamarin.com

profile

Christian Hissibini

I am a Tech enthusiast who loves to blend Dev & Design on Web and Mobile Platforms. I am also a Windows Platform Dev MVP

There are no comments.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

View Comments (0) ...
Navigation