How can I make the keyboard go away

Eduardo Gomez Romero 1,375 Reputation points
2024-10-25T22:20:27.8866667+00:00

When I click on the upload button

   <Grid
       x:Name="TurbineDataGrid"
       Padding="10"
       RowDefinitions="Auto, Auto, Auto, Auto, Auto, Auto, Auto, Auto, *, *"
       RowSpacing="10">

       <!--  Installation Date Entry  -->
       <Border
           Grid.Row="0"
           Style="{x:StaticResource CommonBorderStyle}">
           <Label
               Padding="5"
               HeightRequest="{Binding Height, Source={x:Reference TurbineNameEntry}}"
               Text="{Binding Turbine.StringifyInstalationDate}"
               VerticalTextAlignment="Center">
               <Label.GestureRecognizers>
                   <TapGestureRecognizer
                       Command="{Binding OpenDatePickerCommand}"
                       CommandParameter="{Binding Source={x:Reference picker}}" />
               </Label.GestureRecognizers>
               <Label.Style>
                   <Style TargetType="Label">
                       <Setter Property="TextColor" Value="{Binding PlaceholderColor, Source={x:Reference TurbineNameEntry}}" />
                       <Style.Triggers>
                           <DataTrigger
                               Binding="{Binding Source={x:Reference picker}, Path=SelectedDate}"
                               TargetType="Label"
                               Value="{x:Null}">
                               <Setter Property="TextColor" Value="{Binding PlaceholderColor, Source={x:Reference TurbineNameEntry}}" />
                           </DataTrigger>
                           <DataTrigger
                               Binding="{Binding Source={x:Reference picker}, Path=SelectedDate}"
                               TargetType="Label"
                               Value="{Binding SelectedDate, Source={x:Reference picker}}">
                               <Setter Property="TextColor" Value="Black" />
                           </DataTrigger>
                       </Style.Triggers>
                   </Style>
               </Label.Style>
           </Label>
       </Border>

       <!--  Turbine Name Entry  -->
       <Border
           Grid.Row="1"
           Style="{x:StaticResource CommonBorderStyle}">
           <Entry
               x:Name="TurbineNameEntry"
               Keyboard="Text"
               Placeholder="{x:Static rex:AppResource.TurbineName}"
               Text="{Binding Turbine.Name}"
               TextColor="Black" />
       </Border>

       <!--  Turbine Address Entry  -->
       <Border
           Grid.Row="2"
           Style="{x:StaticResource CommonBorderStyle}">
           <Entry
               Keyboard="Text"
               Placeholder="{x:Static rex:AppResource.TurbineAddress}"
               Text="{x:Binding Turbine.Address}"
               TextColor="Black"
               VerticalTextAlignment="Center" />
       </Border>

       <!--  Turbine Power  -->
       <Border
           Grid.Row="3"
           Style="{x:StaticResource CommonBorderStyle}">
           <Entry
               FontAttributes="Bold"
               Keyboard="Numeric"
               Placeholder="{x:Static rex:AppResource.TubinePower}"
               Text="{Binding Turbine.Power}"
               TextColor="Black"
               VerticalOptions="Center" />
       </Border>

       <!--  Capacity Factor  -->
       <Border
           Grid.Row="4"
           Style="{x:StaticResource CommonBorderStyle}">
           <Entry
               FontAttributes="Bold"
               Keyboard="Numeric"
               Placeholder="{x:Static rex:AppResource.TurbineCapacity}"
               Text="{Binding Turbine.CapacityFator}"
               TextColor="Black"
               VerticalOptions="Center" />
       </Border>

       <!--  CO2 Emission Offset  -->
       <Border
           Grid.Row="5"
           Style="{x:StaticResource CommonBorderStyle}">
           <Entry
               FontAttributes="Bold"
               Keyboard="Numeric"
               Placeholder="{x:Static rex:AppResource.TurbineEmission}"
               Text="{Binding Turbine.Co2EmissionOffset}"
               TextColor="Black"
               VerticalOptions="Center" />
       </Border>

       <!--  Hidden Date Picker  -->
       <picker:SfDateTimePicker
           x:Name="picker"
           Grid.Row="6"
           Style="{x:DynamicResource DateTimePickerStyle}">
           <picker:SfDateTimePicker.Behaviors>
               <mct:EventToCommandBehavior
                   Command="{Binding BindingContext.CancelDateCommand, Source={x:Reference TurbinesCollection}}"
                   CommandParameter="{Binding Source={x:Reference picker}}"
                   EventName="CancelButtonClicked" />
               <mct:EventToCommandBehavior
                   Command="{Binding BindingContext.ConfirmDateCommand, Source={x:Reference TurbinesCollection}}"
                   CommandParameter="{Binding Source={x:Reference picker}, Path=SelectedDate}"
                   EventName="OkButtonClicked" />
           </picker:SfDateTimePicker.Behaviors>
       </picker:SfDateTimePicker>

       <!--  Turbine Images Button  -->
       <Button
           Grid.Row="7"
           Command="{Binding PickImagesCommand}"
           CommandParameter="{x:Binding Source={x:Reference TurbineDataGrid}}"
           ContentLayout="Left, 5"
           HorizontalOptions="Start"
           Text="{x:Static rex:AppResource.TurbineImages}">
           <Button.ImageSource>
               <FontImageSource
                   FontFamily="ma"
                   Glyph="{x:Static constant:MaterialFonts.Upload}"
                   Color="Black" />
           </Button.ImageSource>
       </Button>

       <!--  Image CollectionView  -->
       <CollectionView
           Grid.Row="8"
           ItemsSource="{Binding Turbine.Images}"
           VerticalOptions="Center">
           <CollectionView.ItemsLayout>
               <GridItemsLayout
                   HorizontalItemSpacing="10"
                   Orientation="Horizontal"
                   Span="2"
                   VerticalItemSpacing="10" />
           </CollectionView.ItemsLayout>
           <CollectionView.ItemTemplate>
               <DataTemplate>
                   <Image
                       HeightRequest="100"
                       Source="{Binding}"
                       WidthRequest="100" />
               </DataTemplate>
           </CollectionView.ItemTemplate>
       </CollectionView>

       <!--  Save Button  -->
       <Button
           Grid.Row="9"
           Command="{Binding SaveAndCloseCommand}"
           CommandParameter="{x:Binding Source={x:Reference TurbinePopUp}}"
           IsEnabled="{x:Binding Turbine.IsValid}"
           VerticalOptions="End" />
   </Grid>


I want to hide the keyboard

        [RelayCommand]
        async Task PickImages(object o) {
#if ANDROID || IOS
            if (o is Grid g) {
                foreach (var item in g.Children.OfType<Border>()) {
                    if (item.Content is Entry e) {
                        await e.HideKeyboardAsync();
                    }
                }
            }
#endif
            var results = await filePicker.PickMultipleAsync(new PickOptions {
                FileTypes = FilePickerFileType.Images,
            });
            foreach (var result in results) {
                if (!Turbine!.Images!.Contains(result.FullPath)) {
                    Turbine.Images.Add(result.FullPath);
                }
            }
        }
    }

I don't care what entry has the keyboard activated

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,451 Reputation points Microsoft External Staff
    2024-10-28T08:34:44.9266667+00:00

    Hello,

    You could hide the keyboard like the following:

    #if ANDROID
          if(Platform.CurrentActivity.CurrentFocus != null)
    	  Platform.CurrentActivity.HideKeyboard(Platform.CurrentActivity.CurrentFocus);
    #elif IOS
                UIApplication.SharedApplication.KeyWindow.EndEditing(true);
    #endif
            }
    

    Take care the namespace

    using Microsoft.Maui.Platform;
    #if IOS
    using UIKit;
    #endif
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.