UI rolled up when keyboard appears – MAUI

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

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)并且键盘可见时,我的界面被卷起:

UI rolled up when keyboard appears – MAUI

我可以通过移除RowDefinition Height中的所有星号来解决这个问题,但是将值设置为固定值会导致它无法适应所有屏幕。

是否有可能在键盘可见时关闭界面卷起的效果?

英文:

It is my definition for xaml:

&lt;Grid&gt;
    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;Entry Placeholder=&quot;Write text...&quot;/&gt;
    &lt;Button Text=&quot;Button&quot; Grid.Row=&quot;1&quot; VerticalOptions=&quot;End&quot; /&gt;
&lt;/Grid&gt;

When I click on control Entry and keyboard is visable. My interface is rolled up:

UI rolled up when keyboard appears – MAUI

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

请参考以下代码:

&lt;?xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?&gt; 
&lt;Application xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:local=&quot;clr-namespace:MauiApp&quot;                 
             xmlns:android=&quot;clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls&quot;
             android:Application.WindowSoftInputModeAdjust=&quot;Pan&quot;
             x:Class=&quot;MauiApp0207.App&quot;&gt;
    &lt;Application.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;ResourceDictionary.MergedDictionaries&gt;
                &lt;ResourceDictionary Source=&quot;Resources/Styles/Colors.xaml&quot; /&gt;
                &lt;ResourceDictionary Source=&quot;Resources/Styles/Styles.xaml&quot; /&gt;
            &lt;/ResourceDictionary.MergedDictionaries&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/Application.Resources&gt;
&lt;/Application&gt;

更多信息,请查看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:

&lt;?xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot; ?&gt; 
&lt;Application xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:local=&quot;clr-namespace:MauiApp&quot;                 
             xmlns:android=&quot;clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls&quot;
             android:Application.WindowSoftInputModeAdjust=&quot;Pan&quot;
             x:Class=&quot;MauiApp0207.App&quot;&gt;
    &lt;Application.Resources&gt;
        &lt;ResourceDictionary&gt;
            &lt;ResourceDictionary.MergedDictionaries&gt;
                &lt;ResourceDictionary Source=&quot;Resources/Styles/Colors.xaml&quot; /&gt;
                &lt;ResourceDictionary Source=&quot;Resources/Styles/Styles.xaml&quot; /&gt;
            &lt;/ResourceDictionary.MergedDictionaries&gt;
        &lt;/ResourceDictionary&gt;
    &lt;/Application.Resources&gt;
&lt;/Application&gt;

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&lt;Microsoft.Maui.Controls.PlatformConfiguration.Android&gt;().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Pan);
    
    ...
}
英文:

in MainActivity.cs:

protected override void OnCreate(Bundle savedInstanceState)
{
...
App.Current.On&lt;Microsoft.Maui.Controls.PlatformConfiguration.Android&gt;().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Pan);

...
}

huangapple
  • 本文由 发表于 2023年2月6日 17:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359287.html
匿名

发表评论

匿名网友

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

确定