I am using Visual Studio 2019, C#, for Android.
I am trying to set the orientation and lock it. When I follow the examples, I get the error message:
The name 'setRequestedOrientation' does not exist in the current context
This is a stripped-down beginning of my MainActivity.cs file:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using System;
using System.Text;
using SQLite;
using Android.Support.V4.Content;
using Android;
using Android.Support.V4.App;
using Android.Content.PM;
using System.IO;
using Android.Bluetooth;
using System.Collections;
using Java.Util;
using System.Collections.Generic;
namespace Fubar
{
[Activity(Label = "Fubar", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public partial class MainActivity : Android.Support.V7.App.AppCompatActivity
{
public static Android.Support.V4.Widget.DrawerLayout drawerLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
Vars.Is_Land = false;
base.OnCreate(savedInstanceState);
Vars.thisApp = this;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// .
// .
// .
What do I need to add to make this work? I assume that I have to add another "using" line.
This app has been around for a long time, so it currently supports Android 4.4.x (KitKat) and above. Do I need to stop supporting some of these early versions to get this working?
The problem with your code is that you are mixing Android Java with Android Xamarin. Given that Android Xamarin is a binding of the earlier then you need to know that any method in Android Java needs to follow the C# method naming convention when used from the Android Xamarin context. In your case you're using an Android Java method.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
which will not compile because that method already broke the Xamarin Android C# naming convention. The method does exist for Xamarin Android as a property of the Activity class object and you can use it like shown in the code snippet below.
RequestedOrientation = ScreenOrientation.Landscape;
.