Skip to content

x:Bind is evaluated too early causing changes in Page.OnNavigatedTo to be ignored #2872

Open

Description

@MartinZikmund

Current behavior

When await is used before first navigation in App.xaml.cs, x:Bind suddenly behaves "weirdly" - it binds data which are available during page constructor, but does not consider any updates during OnNavigatedTo (which is when MVVM frameworks like MvvmCross set the DataContext).

Expected behavior

x:Bind behavior should not change if user awaits in OnLaunched.

How to reproduce it (as minimally and precisely as possible)

  1. Create a new Uno Platform app
  2. Set MainPage.xaml as follows:
<Page
    x:Class="BindBreak.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BindBreak"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
		<TextBlock Text="{x:Bind Text}" Margin="20" FontSize="30" />
    </Grid>
</Page>
  1. Set code-behind as follows:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace BindBreak
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.DataContextChanged += MainPage_DataContextChanged;
        }

        private void MainPage_DataContextChanged(DependencyObject sender, DataContextChangedEventArgs args)
        {
            Text = "In data context changed";
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            DataContext = new object();
        }

        public string Text { get; set; } = "Should not display";
    }
}
  1. Run the app on UWP, Android. Notice in both cases app says "In data context changed"
  2. In App.xaml.cs change OnLaunched to be async:
protected override async void OnLaunched(LaunchActivatedEventArgs e)
  1. Add delay before navigating in OnLaunched:
await Task.Delay(1000);
rootFrame.Navigate(typeof(MainPage), e.Arguments);
  1. Launch app on UWP. Observe there is no change in behavior.
  2. Launch app on Android. Observe it now shows "Should not display".

Workaround

Add the following statement at the end of the OnNavigatedTo method:

Bindings.Update();

Environment

Nuget Package: latest stable + latest dev

Package Version(s):

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Windows
  • Build tasks
  • Solution Templates

Visual Studio:

  • 2017 (version: )
  • 2019 (version: )
  • for Mac (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

Activity

  1. added
    kind/bug
    triage/untriaged
    platform/android 🤖
    platform/ios 🍎
    platform/wasm 🌐
    and removed
    triage/untriaged
    on Mar 23, 2020
  2. MartinZikmund commented on Mar 23, 2020

    @MartinZikmund
    MemberAuthor

    After a bit further digging, the async is not the main issue - when we add another page with the same logic and add a button to MainPage to navigate to it, the second page shows "Should not display" even if OnLaunched is not async. So it seems for any page that not loaded immediately after start of the application x:Bind behaves "incorrectly"

  3. MartinZikmund commented on Mar 23, 2020

    @MartinZikmund
    MemberAuthor

    Added higher priority, as this is important for MvvmCross apps to work seamlessly when Uno support is added.

  4. jeromelaban commented on Mar 24, 2020

    @jeromelaban
    Member

    It seems one of the problems is the ordering of page Loaded and navigation events and overrides, which may be related to #2008.

  5. MartinZikmund commented on Mar 24, 2020

    @MartinZikmund
    MemberAuthor

    @jeromelaban Oh, I must ping him.
    I have also noticed the reason is probably the event ordering - for subsequent navigations, Loaded event runs before OnNavigatedTo, which means view model/DataContext is not set yet and hence things like ViewAppearing/ViewAppeared in MvvmCross stop working.

  6. MartinZikmund commented on Mar 27, 2020

    @MartinZikmund
    MemberAuthor

    This is caused by #2895.

  7. 10 remaining items

  8. added
    area/xBind 🪢
    on Mar 11, 2022
  9. changed the title [-]Async OnLaunched magically breaks x:Bind[/-] [+]x:Bind is evaluated too early causing changes in Page.OnNavigatedTo to be ignored[/+] on Mar 11, 2022
  10. hawkerm commented on Mar 14, 2022

    @hawkerm

    Not sure if related or slightly different, but I'm using the common behavior/load pattern to do async initialization of my ViewModel:

        <i:Interaction.Behaviors>
            <act:EventTriggerBehavior EventName="Loaded">
                <act:InvokeCommandAction Command="{x:Bind ViewModel.InitializeCommand, Mode=OneWay}" />
            </act:EventTriggerBehavior>
        </i:Interaction.Behaviors>

    So while Bindings.Update() appears to work the first load, this trigger for the Loaded is being missed on subsequent page navigations in WPF/WASM, where it works fine in UWP.

    I may have to see if I can just move this behavior to the OnPageNavigated override instead though, but is another quirk of the loading order being out of order. Will have to test that out later.

  11. changed the title [-]x:Bind is evaluated too early causing changes in Page.OnNavigatedTo to be ignored[/-] [+]`x:Bind` is evaluated too early causing changes in `Page.OnNavigatedTo` to be ignored[/+] on Jun 12, 2023
  12. added
    project/navigation-lifecycle 🧬
    on Jun 12, 2023
  13. MartinZikmund commented on Jul 17, 2025

    @MartinZikmund
    MemberAuthor

    This is likely still relevant for Skia targets

  14. added theissue type on Jul 17, 2025
  15. added
    triage/potentially-fixed
    on Jan 22, 2026
  16. MartinZikmund commented on Apr 7, 2026

    @MartinZikmund
    MemberAuthor

    Repro generator

    Runtime test(s) added in branch dev/mazi/repro/2872:

    • src/Uno.UI.RuntimeTests/Tests/BindingTests/Given_XBind_NavigatedTo.cs → When_XBind_Updated_After_DataContext_Set_In_OnNavigatedTo

    ⚠️ The test(s) pass on current master (Skia Desktop) — after a 200ms delay before navigation (simulating the await in OnLaunched), navigating to a page that sets DataContext in OnNavigatedTo (which calls Bindings.Update()) correctly updates the {x:Bind} bound property. This is consistent with the triage/potentially-fixed label. The branch provides a regression guard for the managed path. The original bug was reported on iOS/Android/WASM.

  17. MartinZikmund commented on Apr 8, 2026

    @MartinZikmund
    MemberAuthor

    Repro generator

    A PR has been created to add the passing regression test(s) from branch dev/mazi/repro/2872:

    The test(s) pass on current master (Skia target), suggesting the reported issue may be fixed. The issue was reported on iOS, Android, WebAssembly — verification on native targets is still recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/xBind 🪢difficulty/challenging 🤯has-repro-branchkind/bugplatform/android 🤖platform/ios 🍎platform/wasm 🌐project/binding 🪢project/navigation-lifecycle 🧬triage/potentially-fixed

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions