英文:
UI rolled up when keyboard appears - MAUI
问题
以下是翻译好的部分:
这是我对XAML的定义:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Entry Placeholder="输入文本..." />
<Button Text="按钮" Grid.Row="1" VerticalOptions="End" />
</Grid>
当我点击输入控件(Entry)并且键盘可见时,我的界面被卷起:
我可以通过移除RowDefinition Height中的所有星号来解决这个问题,但是将值设置为固定值会导致它无法适应所有屏幕。
是否有可能在键盘可见时关闭界面卷起的效果?
英文:
It is my definition for xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Entry Placeholder="Write text..."/>
<Button Text="Button" Grid.Row="1" VerticalOptions="End" />
</Grid>
When I click on control Entry and keyboard is visable. My interface is rolled up:
I can fix it by remove all stars from RowDecinition Height but setting the value rigidly causes that it does not adapt to all screens.
Is it possible to turn off roll up interface when keyboard is visable?
答案1
得分: 1
你可以尝试将 Application.WindowSoftInputModeAdjust
附加属性设置为 Pan
。
请参考以下代码:
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp"
xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
android:Application.WindowSoftInputModeAdjust="Pan"
x:Class="MauiApp0207.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
更多信息,请查看Android 上的软键盘输入模式。
更新
在 iOS 中,你可以尝试通过 UIKeyboard.FrameEndUserInfoKey
获取键盘的框架,并计算需要更改的高度。
NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
CGSize keyboardSize = result.RectangleFValue.Size;
private void Entry_Focused(object sender, FocusEventArgs e)
{
if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
{
NFloat bottom;
try
{
UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();
bottom = window.SafeAreaInsets.Bottom;
}
catch
{
bottom = 0;
}
var heightChange = (keyboardSize.Height - bottom);
layout.TranslateTo(0, originalTranslationY.Value - heightChange, 50);
}
}
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
{
layout.TranslateTo(0, 0, 50);
}
}
英文:
You can try to set the Application.WindowSoftInputModeAdjust
attached property to Pan
.
Please refer to the following code:
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp"
xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
android:Application.WindowSoftInputModeAdjust="Pan"
x:Class="MauiApp0207.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
For more information,you can check Soft keyboard input mode on Android.
Update
In IOS, you can try to obtain keyboard's frame via UIKeyboard.FrameEndUserInfoKey
and calculate the height that need to change.
NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
CGSize keyboardSize = result.RectangleFValue.Size;
private void Entry_Focused(object sender, FocusEventArgs e)
{
if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
{
NFloat bottom;
try
{
UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow();
bottom = window.SafeAreaInsets.Bottom;
}
catch
{
bottom = 0;
}
var heightChange = (keyboardSize.Height - bottom);
layout.TranslateTo(0, originalTranslationY.Value - heightChange, 50);
}
}
private void Entry_Unfocused(object sender, FocusEventArgs e)
{
if (DeviceInfo.Current.Platform == DevicePlatform.iOS)
{
layout.TranslateTo(0, 0, 50);
}
}
答案2
得分: 0
在MainActivity.cs文件中:
protected override void OnCreate(Bundle savedInstanceState)
{
...
App.Current.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Pan);
...
}
英文:
in MainActivity.cs:
protected override void OnCreate(Bundle savedInstanceState)
{
...
App.Current.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Pan);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论