Repository navigation
Description
Problem Description
During navigation setup with ViewMap, the DataContext is sometimes null on first application launch, but works correctly after Hot Reload. This suggests a timing race condition during initial Host construction and first navigation.
Environment
- Uno Platform: 6.4.195
- Uno.Extensions.Navigation: 7.0.4
- .NET: 10.0.1
- Target: net10.0-desktop (Windows)
Reproduction Steps
- Create a page with ViewMap registration:
// App.xaml.cs
services.AddTransient<MyViewModel>();
.UseNavigation(new ViewMap<MyPage, MyViewModel>())- Navigate to the page on app startup:
await navigator.NavigateViewModelAsync<MyViewModel>(this);- Check DataContext in Page.Loaded:
public MyPage()
{
InitializeComponent();
this.Loaded += (s, e) =>
{
Console.WriteLine($"DataContext: {DataContext?.GetType().Name ?? "NULL"}");
};
}Expected Behavior
DataContext should be set to the ViewModel instance immediately on first launch.
Actual Behavior
- First launch:
DataContext: NULL(bindings don't work) - After Hot Reload (make any XAML change and save):
DataContext: MyViewModel(everything works)
The ViewModel constructor is never called on first launch, but is called after Hot Reload.
Diagnosis
Verification shows the ViewModel IS registered in DI and the ViewMap IS registered:
// This diagnostic code shows:
var app = Application.Current as App;
var vm = app.Host?.Services.GetService<MyViewModel>();
Console.WriteLine($"ViewModel in DI: {vm != null}"); // True
Console.WriteLine($"DataContext set: {DataContext != null}"); // False on first launchThis confirms the issue is in the ViewMap → DataContext assignment timing, not in registration.
Current Workaround
Manual DataContext fallback in Page.Loaded (ugly but functional):
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
if (DataContext == null && Application.Current is App app)
{
// Fallback: manually resolve from DI if ViewMap failed
DataContext = app.Host?.Services.GetService<MyViewModel>();
Console.WriteLine("[MyPage] Applied DataContext fallback");
}
}Impact
- Breaks MVVM bindings on first application launch
- Requires manual workarounds
- Confusing developer experience (works after Hot Reload but not initially)
- May affect other platforms beyond Windows desktop
Questions
- Is this a known issue in the navigation initialization sequence?
- Should there be a built-in fallback mechanism or helper?
- Is there a recommended pattern to ensure DataContext assignment timing?
Reacted by DevTKSS
Reacted by Copilot
Metadata
Metadata
Assignees
Labels
No labels
Activity
DevTKSS commentedon Dec 20, 2025
@carldebilly I think this issue might be coming from the same Issue source than those:
I would be using the
this.GetServiceProvider().GetRequiredService<Serilog.ILogger>()as workaround forthis.Log()has no compartibility Layer for Serilog, but this is resulting in anNullReferenceExceptiontoo 😓So it's not just limited to failing to resolve the VM, which we also can not "just" get through Page ctor parameter, as INavigator does require Parameterless constructor for our views, like I seen this often beeing introduced in WPF getting started with MVVM + DI tutorials last year before I found out about Uno and was happy to have something way prettier and more modern than WPF 😅 .
I literally see no chance (except from internalizing the Host Object like you suggested) to get any Service into my codebehind 👀 Hoping this could get fixed in any future day too 💭