4 Kasım 2016 Cuma

Xamarin.Forms Force Page Orientation and how to use Dependency simply


In Forms Project;

Dependency;



using Xamarin.Forms;

namespace MyProject
{
    public static class MBDGeneralMethods
    {
        static IMBDGeneralMethods _instance;
        public static IMBDGeneralMethods Instance {
            get{ 
                if (_instance == null) {
                    _instance = DependencyService.Get<IMBDGeneralMethods> ();
                }

                return _instance;
            }
        }
    }
}


DependencyInterface;


   using System;
   namespace MyProject
   {
      public interface IMBDGeneralMethods
      {
         void ForceLandscape();
         void ForcePortrait();
         void SetOrientionFree();
      }
   }




How to call it from any page;

In OnAppearing or in OnSizeAllocated or anywhere you want;


  
  protected override void OnSizeAllocated(double width, double height)
  {
     base.OnSizeAllocated(width, height); //must be called
     MBDGeneralMethods.Instance.ForceLandscape();
  }




In iOS Project; 

Info.plist;




AppDelegate.cs;


    [Export("application:supportedInterfaceOrientationsForWindow:")]
    public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
    {
       return UIInterfaceOrientationMask.AllButUpsideDown;
    }



Dependency;


using System;
using System.Threading;
using Foundation;
using YH.MB.iOS;
using UIKit;
using System.Drawing;
using UIKit;
using CoreGraphics;

[assembly: Xamarin.Forms.Dependency(typeof(MBDGeneralMethods))]
namespace MyProject.iOS
{
    public class MBDGeneralMethods : IMBDGeneralMethods
    {      
        public void ForceLandscape()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
        }

        public void ForcePortrait()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        }

        public void SetOrientionFree()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Unknown), new NSString("orientation"));
        }
    }
}



In Android Project;

Dependency;


using System;
using YH.MB.Droid;
using System.Net;
using Xamarin.Forms;
using Android.Graphics;
using Android.Content;
using System.IO;
using Android.App;
using Android;
using Android.Support.V4.Content;
using Android.Content.PM;
using Android.Support.V4.App;
using XLabs.Platform;
using Android.Views.InputMethods;
using System.Collections.Generic;
using System.Text;
using Android.Support.V4.Hardware.Fingerprint;
using Android.Hardware.Fingerprints;
using Android.Security.Keystore;
using Javax.Crypto;
using Java.Security;
using Android.Util;
using Java.Lang;

[assembly: Dependency(typeof(MBDGeneralMethods))]
namespace MyProject.Droid
{
    public class MBDGeneralMethods : IMBDGeneralMethods 
    {    
        public void ForceLandscape()
        {
            var activity = (Activity)Forms.Context;
            activity.RequestedOrientation = ScreenOrientation.Landscape;
        }

        public void ForcePortrait()
        {
            var activity = (Activity)Forms.Context;
            activity.RequestedOrientation = ScreenOrientation.Portrait;
        }

        public void SetOrientionFree()
        {
            var activity = (Activity)Forms.Context;
            activity.RequestedOrientation = ScreenOrientation.FullSensor;
        }
    }    
}


MainActivity.cs;

Add this line;
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)

like this;

    namespace MyProject.Droid{

    [Activity(Label = MBConsts.ApplicationName, Icon = "@drawable/icon",
        ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : XFormsAppCompatApplicationDroid
    {

    ............



That's all.
Bye.
Kib :P

(Burada bir karikatür vardı ama telif yerim diye kaldırdım. Herkese dava açıyorlarmış karikatür paylaşılıyor diye. Karikatürün doğasına hiç uymuyor bence bu tavır :-/ Hayatın anlamını bulduktan hemen sonrası... amaan bu muymuş)

1 yorum:

  1. The History of the Baccarat Rules - Werrione
    The カジノ シークレット Baccarat rules are based on a simple, rather than traditional, 바카라 way of playing. Baccarat 제왕카지노 – the dealer's hand consists of two hands holding two hands,

    YanıtlaSil