Platform specific dependency in .NET MAUI

huangapple go评论70阅读模式
英文:

Platform specific dependency in .net MAUI

问题

I've tried to implement platform specific dependency for hide keyboard.

Platforms/Android/KeyboardHelper.cs

namespace MobileApp.Platforms.Android
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            var context = Platform.AppContext;
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null)
            {
                var activity = Platform.CurrentActivity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
                activity.Window.DecorView.ClearFocus();
            }
        }
    }
}

Platforms/iOS/KeyboardHelper.cs

namespace MobileApp.Platforms.iOS
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            UIApplication.SharedApplication.Delegate.GetWindow().EndEditing(true);
        }
    }
}

I've resolved through dependency injection.
Register in app.xaml.cs
DependencyService.Register<IKeyboardHelper, MobileApp.Platforms.KeyboardHelper>();

and use it when I need:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();
英文:

I've tried to implement platform specific dependency for hide keyboard.

Platforms/Android/KeyboardHelper.cs

namespace MobileApp.Platforms.Android
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            var context = Platform.AppContext;
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null)
            {
                var activity = Platform.CurrentActivity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
                activity.Window.DecorView.ClearFocus();
            }
        }
    }
}

Platforms/iOS/KeyboardHelper.cs

namespace MobileApp.Platforms.iOS
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            UIApplication.SharedApplication.Delegate.GetWindow().EndEditing(true);
        }
    }
}

I've resolved through dependency injection.
Register in app.xaml.cs
DependencyService.Register<IKeyboardHelper, MobileApp.Platforms.KeyboardHelper>();

and use it in when i need:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();

答案1

得分: 2

以下是翻译好的部分:

You created two different classes:

  • MobileApp.Platforms.Android.KeyboardHelper
  • MobileApp.Platforms.iOS.KeyboardHelper

If you wanted platform-specific implementations of the same class, they would have to be in the same namespace.

Platforms/Android/KeyboardHelper.cs
namespace MobileApp
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            // ... the Android implementation ...
        }
    }
}
Platforms/iOS/KeyboardHelper.cs
namespace MobileApp
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            // ... the iOS implementation ...
        }
    }
}

With the above approach, you will need an implementation defined for every target you are compiling for, or you will need a default no-op implementation defined somewhere.

Alternatively, you could create a single file for all implementations, leveraging conditional compilation directives:

KeyboardHelper.cs
namespace MobileApp
{
    public static class KeyboardHelper
    {
        public static void HideKeyboard()
        {
#if IOS
            // ... the iOS implementation ...
#elif ANDROID
            // ... the Android implementation ...
#else
            // ... the default implementation (if there is one) ...
#endif
        }
    }
}

Note: If you want the iOS implementation to also apply to Mac Catalyst, you could use either #if __IOS__ or you can use #if IOS || MACCATALYST. They do the same thing.

When invoking the code, you do not need to do a runtime check for platform. Just call KeyboardHelper.HideKeyboard().

See also: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code

英文:

You created two different classes:

  • MobileApp.Platforms.Android.KeyboardHelper
  • MobileApp.Platforms.iOS.KeyboardHelper

If you wanted platform-specific implementations of the same class, they would have to be in the same namespace.

Platforms/Android/KeyboardHelper.cs
namespace MobileApp
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            // ... the Android implementation ...
        }
    }
}
Platforms/iOS/KeyboardHelper.cs
namespace MobileApp
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            // ... the iOS implementation ...
        }
    }
}

With the above approach, you will need an implementation defined for every target you are compiling for, or you will need a default no-op implementationed defined somewhere.

Alternatively, you could create a single file for all implementations, leveraging conditional compilation directives:

KeyboardHelper.cs
namespace MobileApp
{
    public static class KeyboardHelper
    {
        public static void HideKeyboard()
        {
#if IOS
            // ... the iOS implementation ...
#elif ANDROID
            // ... the Android implementation ...
#else
            // ... the default implementation (if there is one) ...
#endif
        }
    }
}

Note: If you want the iOS implementation to also apply to Mac Catalyst, you could use either #if __IOS__ or you can use #if IOS || MACCATALYST. They do the same thing.

When invoking the code, you do not need to do a runtime check for platform. Just call KeyboardHelper.HideKeyboard().

See also: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code

huangapple
  • 本文由 发表于 2023年3月15日 19:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定