首页 > 解决方案 > 相机点击 Xamarin Force Portrait

问题描述

好的,我点击了这个链接https://heartbeat.fritz.ai/force-an-orientation-on-a-single-page-in-xamarin-forms-b9c0c5295367和许多其他我需要在应用程序中或在相机点击的最小值。这是一个带有 Android 和 IOS 的 Xamarin 应用程序,但我真的专注于 Android。我尝试将以下代码添加到 mainacrivity.cs

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

在创建时添加了这个

 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());
    }

以及来自内容页面的相机点击事件

 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;

    }

还尝试将此添加到清单

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

如果我拿起 Android 设备并将其转为横向,则图片将是横向的。如果需要,或者仅当我用相机拍照时,我可以接受整个应用程序强制进行人像。我一定会错过任何想法,将不胜感激。

标签: androidxamarinxamarin.android

解决方案


推荐阅读