kikipoulet / CherylUI

MIT License
248 stars 8 forks source link

Blank Page When Navigating from view to another #3

Closed LSXAxeller closed 6 months ago

LSXAxeller commented 6 months ago

When my app launches and I try to navigate to a page from the MainView, I get a blank page. I am using an empty <controls:InteractiveContainer /> in the MainView and loading from the code-behind OnLoaded.

Code:

public static class NavigationService
{
    public static async Task<bool> Navigate<T>(bool preventBack = false, double delay = 0) where T : UserControl, new()
    {
        await Task.Delay((int)(delay * 1000));

        UserControl page = new T();
        MobileNavigation.Push(page, preventBack);
        return true;
    }
}
public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object? sender, RoutedEventArgs e)
    {
        _ = NavigationService.Navigate<LoginScreen>(true);
    }
}
public partial class LoginScreen : UserControl
{
    public LoginScreen()
    {
        DataContext = new LoginScreenModel();
        InitializeComponent();
    }
}

Even if I use

<controls:InteractiveContainer>
    <views:LoginScreen />
</controls:InteractiveContainer>

inside MainView and try to load another page from LoginScreen, I get an error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

on MobileNavigation.Push(page, preventBack); from NavigationService with details:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Avalonia.Base
  StackTrace:
   at Avalonia.VisualTree.VisualExtensions.<GetVisualDescendants>d__13.MoveNext()

But it navigates to HomeScreen only after 3 seconds if I put a delay of 3, but not less. NavigationService.Navigate<HomeScreen>(true, 3); else it shows a blank page.

public partial class LoginScreenModel : ViewModelBase
{
    [ObservableProperty]
    public string _username;
    [ObservableProperty]
    public string _password;
    [ObservableProperty] private bool _isLoggingIn;

    private readonly HttpClient _httpClient = new();
    private readonly IPreferences _preferences;

    public LoginScreenModel()
    {
        _preferences = App.Current.Services.GetService<Preferences>();
        OnScreenLoaded();
    }

    public async void OnScreenLoaded()
    {
        if (DateTime.TryParse(_preferences.Get("authExpiration", ""), out DateTime expireDate) && DateTime.Now < expireDate)
        {
            IsLoggingIn = true;
            IsLoggingIn = !await NavigationService.Navigate<HomeScreen>(true);
        }
    }
}
kikipoulet commented 6 months ago

Is it platform specific ?

I had something similar on android one day but never on desktop.

LSXAxeller commented 6 months ago

Is it platform specific ?

I had something similar on android one day but never on desktop.

it's on windows only.

I just tested on android and got this and and debugging stops while trying to load the HomeScreen from LoginScreen without the 3 seconds delay

System.Exception
  Message=You are trying to use a InteractiveContainer functionality without declaring one !

and with the 3 seconds delay I just get a blank page

kikipoulet commented 6 months ago

I have tested with your code

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object? sender,RoutedEventArgs e)
    {
        _ = NavigationService.Navigate<MenuControl>(true, 0);

    }

}

public static class NavigationService
{
    public static async Task<bool> Navigate<T>(bool preventBack = false, double delay = 0) where T : UserControl, new()
    {
        await Task.Delay((int)(delay * 1000));

        UserControl page = new T();
        MobileNavigation.Push(page, preventBack);
        return true;
    }
}

But I can't reproduce the bug at all on windows, neither on android emulator. However I know that this problem happened few times on my real android device so I know there is something to find.

I update the nuget package to 11.1 beta, maybe it did something actually. I will retry later when I will be able to debug with my android device.

LSXAxeller commented 6 months ago

Maybe it's a problem on my end. I've also encountered issues with MAUI navigation. In the end, I switched to Flutter.