Hello,
Welcome to our Microsoft Q&A platform!
MessagingCenter "does not exist in the current context" inside MainActivity.cs
Please copy MessagingCenter
is placed below LoadApplication(new App());
like following code.
[Activity(Label = "App52", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
Xamarin.Forms.MessagingCenter.Subscribe<App, string>(App.Current, "AllowLandscape", (snd, arg) =>
{
RequestedOrientation = ScreenOrientation.Landscape;
});
Xamarin.Forms.MessagingCenter.Subscribe<App, string>(App.Current, "PreventLandscape", (snd, arg) =>
{
RequestedOrientation = ScreenOrientation.Portrait;
});
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Then, copy following code in the Forms contentpage.
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send<App, string>((App)App.Current, "AllowLandscape", "");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Send<App, string>((App)App.Current, "PreventLandscape", "");
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.