首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin: Window返回空,如何获取Window?

Xamarin: Window返回空,如何获取Window?
EN

Stack Overflow用户
提问于 2019-08-19 08:08:18
回答 1查看 180关注 0票数 0

我正在尝试在Xamarin for Android中获取窗口,但窗口返回空。我想要获得窗口的原因是,我可以应用一个标志来删除UI状态栏。

我已经寻找了一个与getWindow(java)等效的方法,但是没有找到。在本例中,我尝试使用this.Window:

代码语言:javascript
复制
            var _winflag1 = WindowManagerFlags.Fullscreen;

            this.Window.AddFlags(_winflag1); // window is always null

这不起作用,所以我也尝试了实例化,但没有用:

代码语言:javascript
复制
            var _winflag1 = WindowManagerFlags.Fullscreen;

            var _window = this.Window; //_window is always null

            _window.AddFlags(_winflag1);

我想知道一种在Xamarin.Android中获取当前窗口的方法。

编辑:我忘记提到这是在MainActivity.cs内部。

https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/FixLinkOverflow/MainActivity.cs

EN

回答 1

Stack Overflow用户

发布于 2019-08-19 08:30:03

问题是窗口变量必须在MainActivity.cs OnCreate中赋值。

代码语言:javascript
复制
public class MainActivity : FragmentActivity
{
   //it is very important to instantiate with static
   public static Window _window = null; // do this directly inside MainActivity

    protected override void OnCreate(Bundle savedInstanceState)
    {

       //bunch of code here

      _window = this.Window; //we can only assign Window inside 
               //OnCreate??  I tried to do it in another custom method
                //but it would always return null
    }

    public void MyCustomWindowMethod()
    {
         //I was previously trying to assign window inside this method
         //that doesn't work.

         //this var hides the statusbar on android
         var _winflag1 = WindowManagerFlags.Fullscreen;

         //since we assigned window in OnCreate, now _window isn't null
         _window.AddFlags(_winflag1); 
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57549232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档