【问题标题】:Xamarin Android possible ScreenOrientation.Landscape bugXamarin Android 可能的 ScreenOrientation.Landscape 错误
【发布时间】:2017-02-17 20:19:16
【问题描述】:

我的应用有一些奇怪的行为,所以我重新创建了该应用,直到它出现错误并发现 ScreenOrientation.Landscape 是罪魁祸首。

如果您在 Visual Studio 15 中创建一个新的空白应用并将 MainActivity 替换为:

[Activity(Label = "TestLandscapeBug", MainLauncher = true, Icon = "@drawable/icon",
    ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
    System.Console.WriteLine("OnCreate");
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.Main);
}
protected override void OnDestroy()
{
    System.Console.WriteLine("OnDestroy");
    base.OnDestroy();
}
protected override void OnPause()
{
    System.Console.WriteLine("OnPause");
    base.OnPause();
}
protected override void OnRestart()
{
    System.Console.WriteLine("OnRestart");
    base.OnRestart();
}
protected override void OnResume()
{
    System.Console.WriteLine("OnResume");
    base.OnResume();
}
protected override void OnStart()
{
    System.Console.WriteLine("OnStart");
    base.OnStart();
}

protected override void OnStop()
{
    System.Console.WriteLine("OnStop");
    base.OnStop();
}

}

运行应用程序并按下睡眠按钮: 调用 OnPause、OnStop、OnDestroy、OnCreate、OnStart、OnResume 和 OnPause。

如果删除 ScreenOrientation = ScreenOrientation.Landscape OnPause 和 OnStop 会被调用。

这是一个错误吗?还是我做错了什么? 我该如何解决这个问题或使用其他将屏幕锁定为横向的东西。

【问题讨论】:

    标签: c# android visual-studio xamarin


    【解决方案1】:

    屏幕锁定时,Activity 切换到纵向模式是正常的。每当方向改变时,都会调用 OnDestroy,然后调用 OnCreate。因此,没有什么可担心的,因为您看到的是 Android 的默认行为。

    纵向是锁定屏幕的一种默认方向,因此您的活动在锁定时也切换到该方向是有意义的。

    【讨论】:

    • 您也可以使用 [Actitity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize )] 来阻止娱乐。
    • @b.holz 很高兴知道,没想到!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2018-04-27
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多