dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.12k stars 1.73k forks source link

Blazor with maui can not use <InputSelect> or <select> #4325

Closed hy999 closed 2 years ago

hy999 commented 2 years ago

Description

1,when I write this code:

<InputSelect>
    <option value="0">test</option>
<InputSelect>

the application print "An unhandled error has occurred."

2,When I try to use <select> label in the page, It is dosen't work.

Steps to Reproduce

1,Open a page

2,Add the Select like:

<InputSelect>
    <option value="0">test</option>
<InputSelect>

or

<select>
    <option value="0">test</option>
<select>

They are dosen't work

Version with bug

Preview 12 (current)

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

10.0.19041.0

Did you find any workaround?

none

Relevant log output

No response

mkArtakMSFT commented 2 years ago

This may be caused by the fact that there is no a wrapping Form element and that may be causing a hidden exception. @SteveSandersonMS I vaguely remember that we had an issue saying that Input components can't be used without Form element. Should we dupe this with that?

hy999 commented 2 years ago

Thanks,I will try it.

hy999 commented 2 years ago

It is also bad,but,I have an inconvenient way to fix it my code is:

<li class="w-50 list-group-item">
     <select @bind=@DotNetVersion class="form-select form-select-sm">
                   <option value="6.0">推荐:6.0</option>
                   <option value="5.0" >5.0</option>
                   <option value="3.1" >3.1</option>
                   <option value="3.0">3.0</option>
                   <option value="2.2" >不推荐:2.2</option>
                   <option value="2.1">不推荐:2.1</option>
                   <option value="2.0">不推荐:2.0</option>
     </select>
</li>

when it run,do this: 1,click the selector 2,keydown the "Enter" and then,you can select it.

It is very very inconvenient

davidalucas commented 2 years ago

I'm seeing a similar problem in the latest preview, running VS Enterprise 2022 17.1.0 Preview 4.0, debugging for a Windows machine targeting net6.0-windows10.0.19041:

@page "/newsurvey"
@using MauiBlazorAppTest.Data

<EditForm Model="@survey" OnInvalidSubmit="@StartSurvey">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <p>
        <label class="form-label">Derivative
            <InputSelect @bind-Value="survey.Derivative" class="form-select">
                <option selected>Select the derivative...</option>
                <option value="Derivative1">First Derivative</option>
                <option value="Derivative2">Second Derivative</option>
                <option value="Derivative3">Third Dervative</option>
            </InputSelect>
        </label>
    </p>

    <button type="submit" class="btn btn-primary">Start Survey</button>
</EditForm>

@code {

    private SurveyModel survey = new() { };

    private void StartSurvey()
    {
        //start the survey
    }
}

This page itself renders correctly, but the dropdown doesn't respond when I click on it:

image

As hy999 mentioned, it does respond to other events, such as selecting the element and pressing [Enter] to make the dropdown render, as well as the up and down arrow keys to go through the options. It seems to be isolated to the onClick event.

TanayParikh commented 2 years ago

2,Add the Select like:

<InputSelect>
    <option value="0">test</option>
<InputSelect>

or

<select>
    <option value="0">test</option>
<select>

They are dosen't work

InputSelect will need to be within an EditForm as Artak mentioned above. Also, you'll need to use closing tags on the third line of each example (</InputSelect> / </select>). After making these changes, it's working as expected.

<select>
    <option value="0">test</option>
    <option value="1">test2</option>
    <option value="2">test3</option>
</select>

<EditForm Model="model">
    <InputSelect @bind-Value="model.Vehicle">
        @foreach (var value in Enum.GetValues(typeof(Vehicle)))
        {
            <option>@value</option>
        }
    </InputSelect>
</EditForm>

@code {
    Model model = new Model();

    class Model
    {
        public Vehicle Vehicle { get; set; }
    }

    enum Vehicle
    {
        Car,
        Truck,
        Van,
        Bike
    }
}

@davidalucas I tried repro'ing your issue, but it's working fine for me (at least on iOS). Are you experiencing the issue specifically on Windows or other platforms as well? Are you able to provide a minimal, public, github repository app that reproduces this issue.

https://user-images.githubusercontent.com/14852843/151616241-46583737-1461-42d7-b25a-1500fa3a7d19.mp4

FelipeCostaGualberto commented 2 years ago

Hello,

I'm having the same issue. This issue only happens in 'Windows Machine'. It works fine in Android / iOS.

A code as simple as below renders, but when I click in the select element, the select list doesn't expand. Truth to be told, the list just blinks (opens and then close immediately, I can't even see if the items were rendered in the list). I'm trying it in a brand new .NET MAUI Blazor App, Visual Studio 2022 Version 17.1.0 Preview 4.0

<select>
    <option value="0">test</option>
    <option value="1">test2</option>
    <option value="2">test3</option>
</select>
denisalibasic commented 2 years ago

Hello,

I have same issue as the Felipe on the "Windows Machine". When i click on the element, list just flashes and closes. Tried inputSelect and select (same problem). It is inside of EditForm Also working on new .net MAUI blazor app.

<InputSelect class="form-control" @bind-Value="model.WorkoutOwnership"> @foreach (var ownership in Enum.GetValues(typeof(OwnershipEnum))) { <option>@ownership</option> } </InputSelect >

Eilon commented 2 years ago

When i click on the element, list just flashes and closes.

That sounds like this WebView2 bug, which repros in MAUI on Windows: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1693

Try clicking the dropdown and then pressing Alt+Down on the keyboard and that will open it. (That's just a workaround - the actual bug is being fixed by the WebView2/WinUI team.

FelipeCostaGualberto commented 2 years ago

Thanks for the workaround and I'm glad it is being fixed.

Laftek commented 2 years ago

What about MacOS and Android? Does anybody know if its working there? Thanks.

SteveSandersonMS commented 2 years ago

@Laftek MAUI only uses WebView2 on Windows, so a WebView2 bug wouldn't affect macOS/iOS/Android.

Laftek commented 2 years ago

@SteveSandersonMS Thanks Steve! I wasnt sure if this is bug only in WebView2.

Eilon commented 2 years ago

I wasnt sure if this is bug only in WebView2.

Only a bug with WebView2 specifically on WinUI, which is what .NET MAUI uses. It doesn't affect WebView2 for WinForms and WPF apps.

davidalucas commented 2 years ago

Thanks all! It looks like that WebView2 bug is probably what I'm running into as well. There are some other UI behaviors I've noticed which are having similar problems on Windows machines, but I'll wait for that WebView2 bug patch before submitting any new issues.

TanayParikh commented 2 years ago

I've confirmed this isn't an issue on Android/iOS earlier. Thanks @Eilon for pointing out the WebView2 bug. Should we keep this issue open tracking the external https://github.com/MicrosoftEdge/WebView2Feedback/issues/1693 or would it be safe to close this out as a 'duplicate'?

Eilon commented 2 years ago

@TanayParikh if the only "active" issue in this thread is the dropdown that disappears on WinUI, then I recommend closing this issue because there's no action to take. We have email threads with the WebView2 folks on the dropdown issue so it's not getting forgotten.

v-xiaofchen commented 2 years ago

Not reproducible with 17.2.0 Preview 2.0 [32209.335.main] using Windows.