MonoDroid 中 OnConfigurationChanged() 的工作原理

发布于 2025-01-05 17:13:07 字数 1416 浏览 6 评论 0原文

我是 Android 开发新手,到目前为止我一直在通过 java(eclipse) 开发 Android,但现在我正在切换到使用 C# 的 Mono for Android

在java中,方向改变时,我使用了以下代码片段:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
             Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

        } else{
             Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

当尝试在Mono for Android中的C#中执行相同操作时,我尝试了这个

 public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            if (newConfig.Orientation == Android.Content.Res.Configuration.ORIENTATION_LANDSCAPE)  
            {
                Toast.MakeText(this, "landscape",ToastLength.Long).Show();
            } 
            else /* if (orientation == 0) */
            {
                Toast.MakeText(this, "portrait", ToastLength.Long).Show();
            }
        }

但是Android.Content.Res。当我尝试使用以下语句时,配置没有任何值为“ORIENTATION_LANDSCAPE”的属性

if (newConfig.Orientation == Android.Content.Res.Configuration.ORIENTATION_LANDSCAPE)

解决方案可能是什么?还有其他选择吗?

任何帮助表示赞赏!

I'm new to android development and I've been working on android through java(eclipse) till now but now I'm switching to Mono for Android which uses C#.

In java on orientation changed, I've used following code snippet :

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
             Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

        } else{
             Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

When trying to do the same in C# in Mono for Android, I tried this

 public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            if (newConfig.Orientation == Android.Content.Res.Configuration.ORIENTATION_LANDSCAPE)  
            {
                Toast.MakeText(this, "landscape",ToastLength.Long).Show();
            } 
            else /* if (orientation == 0) */
            {
                Toast.MakeText(this, "portrait", ToastLength.Long).Show();
            }
        }

but Android.Content.Res.Configuration doesn't have any attribute with value 'ORIENTATION_LANDSCAPE` when I try to use following statement

if (newConfig.Orientation == Android.Content.Res.Configuration.ORIENTATION_LANDSCAPE)

What might be the solution? Any other option available?

Any help appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-01-12 17:13:07

您必须使用:

Android.Content.Res.Orientation.Landscape

API 参考 - 枚举

API 参考 - 配置

You must use:

Android.Content.Res.Orientation.Landscape

API reference - enum

API refenence - Configuration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文