英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论