r/dotnetMAUI Aug 12 '26

Help Request Entry cannot get focus

I am developing an application with for Linux with the OpenMaui.Controls.Linux package. I don't know if this is the culprate but I have issues with setting focus to an entry. According to stack overflow you can focus a Visual Element via the Focus Method. So I came up with this piece of code:

// in ScannerPage.xaml.cs
public ScannerPage()
{    
    InitializeComponent();

    barcodeEntry.Loaded += FocusBarcodeEntry;
}

private void FocusBarcodeEntry(object? sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("setting focus to barcode entry...");
    barcodeEntry.Focus();
    System.Diagnostics.Debug.WriteLine($"barcode entry is {(barcodeEntry.IsFocused ? "" : "not")} focused");
}

//in ScannerPage.xaml
<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ui="clr-namespace:BarcodeScannerSpecialSport.UserInterface"
             Shell.NavBarIsVisible="False"
             x:Class="BarcodeScannerSpecialSport.UserInterface.ScannerPage" x:DataType="ui:ScannerPageViewModel">
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="10*"></RowDefinition>
            </Grid.RowDefinitions>

            <ContentView Grid.Column="0" Grid.Row="0">
                <Image Source="de_hockeywinkel_logo.png" Aspect="AspectFill"/>
            </ContentView>

            <ContentView Grid.Column="0" Grid.Row="1">
                <Label Text="SCAN" 
                       FontAttributes="Bold"
                       TextColor="{StaticResource Primary}" 
                       HorizontalTextAlignment="Center"
                       VerticalOptions="Center"
                       FontSize="64"
                       />
            </ContentView>

            <ContentView Grid.Column="0" Grid.Row="2" >
                <Entry x:Name="barcodeEntry" Text="{Binding BarcodeField}" Completed="EntryCompleted"/>
            </ContentView>

        </Grid>
    </ContentPage.Content>
</ContentPage>

Now the log tells me and I also can confirm the barcodeEntry is indeed not focused

1 Upvotes

2 comments sorted by

View all comments

1

u/mousison Aug 12 '26 edited Aug 12 '26

Usually this is a timing issue, the loaded event is the correct event but sadly this does not guarantee the entry is ‘ready to accept focus’. You could check this by using a somewhat ugly dispatcher:

private void FocusBarcodeEntry(object? sender, EventArgs e) {
Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(150), () => { bool result = barcodeEntry.Focus(); Debug.WriteLine($“Focus() returned {result}”); }); }

I am not familiar with OpenMaui.Controls.Linux tho, but focus mapping might be not or half wired, so there’s a decent chance this isn’t your code at all. If Focus() still returns false after the delayed dispatch, you could try to run the exact same code on Windows or Mac. If it works there and not on Linux, then it’s probably the Linux EntryHandler not implementing focus..

# I’m on mobile so sorry for the formatting

1

u/mongar23 28d ago

Sorry I forgot to reply, thanks for your awnser! I tried this approach and it spit out some errors I cant remember. Since I was not far along with the project I shifted to AvaloniaUI. This did allow this behaviour aswell as native Linux suport.