在Xamarin Forms中如何调用MainActivity的方法或如何从另一个类获取MainActivity的“ Window”?

问题描述 投票:1回答:2

完成输入文本后,我想隐藏软件按钮。当用户将手指放在键盘外时,将调用此方法:

 public override void OnUserInteraction()
        {
            base.OnUserInteraction();
            HideSoftwareMenuBars();
        }

效果很好:

 private void HideSoftwareMenuBars()
        {
            try
            {
                int uiOptions = (int)Window.DecorView.SystemUiVisibility;
                uiOptions |= (int)SystemUiFlags.LayoutStable;
                uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
                uiOptions |= (int)SystemUiFlags.LayoutFullscreen;
                uiOptions |= (int)SystemUiFlags.LowProfile;
                uiOptions |= (int)SystemUiFlags.HideNavigation;
                uiOptions |= (int)SystemUiFlags.Fullscreen;
                uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
                this.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
            }
            catch (System.Exception Ex)
            {
            }
        }

但是MainActivity.cs中没有方法可以覆盖当用户按下键盘上的“ enter”键时被调用的方法。我已经覆盖了200多种检查方法。然后在Xamarin.Forms中的代码后面有事件:

private void Entry_Unfocused(object sender, FocusEventArgs e)
        {
            DependencyService.Get<ISoftwareButtons>().HideSoftwareMenuBars();
        }

然后通过调用依赖项接口,这里有两个错误:

[assembly: Dependency(typeof(SoftwareButtonsHandler))]
namespace Channel.Droid.DependencyHandlers
{
    public class SoftwareButtonsHandler
    {
        private void HideSoftwareMenuBars()
        {
            try
            {
                int uiOptions = (int) Window.DecorView.SystemUiVisibility;
                uiOptions |= (int)SystemUiFlags.LayoutStable;
                uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
                uiOptions |= (int)SystemUiFlags.LayoutFullscreen;
                uiOptions |= (int)SystemUiFlags.LowProfile;
                uiOptions |= (int)SystemUiFlags.HideNavigation;
                uiOptions |= (int)SystemUiFlags.Fullscreen;
                uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
                this.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
            }
            catch (System.Exception Ex)
            {
            }
        }
    }
}

问题是“ Window.Devorview”出现错误:

“抽象基类,用于顶级窗口外观和行为策略。非静态字段,方法或属性'Window.DecorView;“

和“ this.Window”

“ SoftwareButtonsHandler'不包含'Window'和没有可访问的扩展方法'Window'接受第一个参数可以找到“ SoftwareButtonsHandler”类型(您是否缺少使用指令还是程序集引用?“

有没有一种方法可以从另一个类中调用MainActivity.cs的方法,或者在依赖项处理程序中获得必需的“ Window”(仅在MainActivity.cs中可以访问?)

xamarin xamarin.forms xamarin.android
2个回答
0
投票

您可以对MainActivity进行静态引用,该引用将随处可见,或者您可以使用CurrentActivity插件。


0
投票

您可以在MainActivityInstance中创建一个MainActivity并将其设置为与MainActivityInstance = this中的OnCreate类似:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity MainActivityInstance { get; private set; }

    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);

        MainActivityInstance = this;

        LoadApplication(new App());
    }
}

然后您可以在依赖项界面中获得MainActivity:

public class SoftwareButtonsHandler : ISoftwareButtons
{

    void ISoftwareButtons.HideSoftwareMenuBars()
    {
        try
        {
            int uiOptions = (int)MainActivity.MainActivityInstance.Window.DecorView.SystemUiVisibility;
            uiOptions |= (int)SystemUiFlags.LayoutStable;
            uiOptions |= (int)SystemUiFlags.LayoutHideNavigation;
            uiOptions |= (int)SystemUiFlags.LayoutFullscreen;
            uiOptions |= (int)SystemUiFlags.LowProfile;
            uiOptions |= (int)SystemUiFlags.HideNavigation;
            uiOptions |= (int)SystemUiFlags.Fullscreen;
            uiOptions |= (int)SystemUiFlags.ImmersiveSticky;
            MainActivity.MainActivityInstance.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
        }
        catch (System.Exception Ex)
        {

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.