0

Ok I followed this link https://heartbeat.fritz.ai/force-an-orientation-on-a-single-page-in-xamarin-forms-b9c0c5295367 and many others I need to force portrait while in the app or at a minimum on camera click. this is an Xamarin app with Android and IOS but I really am focused on Android. I tried the following code added this to mainacrivity.cs

[Activity...ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]**strong text**

added this oncreate

 protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        //during page close setting back to portrait
        **MessagingCenter.Subscribe<ImageTest>(this, "quitLandScape", sender =>
        {
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait; ;
        });**


        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }

and from the camera click event from content page

 private async void CameraButton_Clicked(object sender, EventArgs e)
    {

        **MessagingCenter.Send(this, "quitLandScape");**
        var photo = await MediaPicker.CapturePhotoAsync();

        await LoadPhotoAsync(photo);
        Console.WriteLine($"CapturePhotoAsync COMPLETED: {PhotoPath}");
       
        btnAccept.IsEnabled = true;
       
        //    PhotoImage.Source = ImageSource.FromStream(() => { return photo.GetStream(); });
    }

    async Task LoadPhotoAsync(FileResult photo)
    {
        // canceled
        if (photo == null)
        {
            PhotoPath = null;
            return;
        }
        // save the file into local storage

        var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        using (var stream = await photo.OpenReadAsync())
        using (var newStream = File.OpenWrite(newFile))
        await stream.CopyToAsync(newStream);
        
        PhotoPath = newFile;
        picCardImage.Source = ImageSource.FromFile(PhotoPath.ToString());
        //Sor = ScreenOrientation.Portrait;

    }

also tried adding this to Manifest

<uses-feature android:name="android.hardware.screen.portrait" />

if i pick up the Android device and turn it landscape the picture will be landscape. I am ok with either the whole app forced to portrait if needed or only when I take a picture with the camera. I must be missing something any thoughts would be greatly appreciated.

user2180706
  • 307
  • 3
  • 8
  • `Send` and `Subscribe` need to use the same `T` value – Jason May 27 '21 at 13:03
  • PS I foind tyhis link and will advise afterward. https://stackoverflow.com/questions/36598052/android-xamarin-force-screen-to-stay-in-portrait-mode-using-androidmanife – user2180706 May 27 '21 at 13:03

0 Answers0