dotnet / MobileBlazorBindings

Experimental Mobile Blazor Bindings - Build native and hybrid mobile apps with Blazor
MIT License
1.2k stars 174 forks source link

Question about Android WebView behavior change on hybrid apps #216

Open ktaze1 opened 3 years ago

ktaze1 commented 3 years ago

Hello. Before preview 5, I had a working page but after migration I notice this change, I don't know if it's intended or a bug so here we go:

There is a setting page on my project where users can change their information. In here I just receive the user from a remote server, show the information with EditForm and when OnValidSubmit happens I send a PUT request.

@page "/setting"
@inject AspNetUsersDAL userDAL
@inject GroupsDAL groupDAL
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject CugemderApp.Security.CustomStateProvider customStateProvider

<EditForm Model="@user" OnValidSubmit="@EditUser">
    <DataAnnotationsValidator />

    <div class="form-group">
        <label>Group:</label>
        <div>
            <select class="form-control"
                    @bind="user.Group">
                <option value="0">---Select Group---</option>
                @foreach (var group in groups)
                {
                    @if (group.Id == groupId)
                    {
                        <option selected value="@group.Id">@group.GroupName</option>
                    }
                    else
                    {
                        <option value="@group.Id">@group.GroupName</option>
                    }
                }
            </select>
        </div>
    </div>

    <button type="submit" class="btn btn-success">
        Update
    </button>
</EditForm>

@code {

    private AspNetUsers user;
    private List<Groups> groups;
    private int groupId = 0;

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var userAuth = authState.User;

        groups = await groupDAL.GetGroupsAsync();

        if (userAuth.Identity.IsAuthenticated)
        {
            user = await userDAL.GetUsername(userAuth.Identity.Name);
            groupId = user.Group.HasValue ? (int)user.Group : 0;
        }
    }

    async Task EditUser()
    {
        user.UpdatedAt = DateTime.UtcNow;
        userDAL.PutUser(user.Id, user);
    }

}

As I said, this page worked as intended before. Now after migration, this page doesn't work. First I had **System.ObjectDisposedException:** 'Cannot access a disposed object.Object name: 'Android.Webkit.WebView'.' error then I see InvalidOperationException: EditForm requires a Model parameter, or an EditContext parameter, but not both. error. After a little research, I found out that I had to initialize the variables beforehand. After changing private AspNetUsers user; to private AspNetUsers user = new AspNetUsers(); (and of course other variables too) page worked perfectly. Was this intentional?

Eilon commented 3 years ago

Sorry you're running into this! I doubt this was intentional, but let me tell you, getting WebView to work is not easy, so I wouldn't be surprised if this accidentally changed.

This could be related to something that @jspuij is working on fixing. On Android in particular the order of operations in .NET is quite different from what one might normally expect, so sometimes you can get these weird disposed errors.

I'll try to reproduce this particular issue so that we can investigate further.

jspuij commented 3 years ago

@ktaze1 I see you're using Auth already, nice job! Could you point us to the repo if it's public? This would save us so much time!

ktaze1 commented 3 years ago

Since there was no Auth before I had to use a custom one. Here is a link to repo: https://github.com/ktaze1/CugemderMobileApp. It's mostly in Turkish and sorry for some spaghetti code along the way.

jspuij commented 3 years ago

Thank you. We'll investigate an let you know.

ktaze1 commented 3 years ago

Also find out that when trying to access non-existing page, rather than showing "Sorry, there's nothing at this address." from Router's NotFound tag in App.razor, it crashes app with **System.ObjectDisposedException:** 'Cannot access a disposed object.Object name: 'Android.Webkit.WebView'.'

ktaze1 commented 3 years ago

Worth to point out that whenever there is a property waiting for a data from a database, app inconsistently crashes giving this error:

This error message shows the line where GetFromJsonAsync method is called. It mostly works, but not always.

image