Skip to content

UserControl code-behind constructor is not called #61

Closed

Description

@Henk7

I have found a issue in the {Binding} system on android.
I created a example under link

The problem is the following.
Usercontrol1 is a parent of Usercontrol2
Usercontrol1 has the Datacontext not set from code behind. (Datacontext == null)
Usercontrol2 has the Datacontext set from code behind.
Binding for sub-controls in Usercontrol2 doesn't work

WORKAROUND:
Setting the Datacontext of Usercontrol1 in code behind makes the bindings in Usercontrol2 to work

Workaround (2020-01-20)

Add the following property to all the project heads, in the first <PropertyGroup>:

<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>

This will ensure that the UserControl is kept in the visual tree.

Activity

  1. jeromelaban commented on Jun 26, 2018

    @jeromelaban
    Member

    This issue is actually not a binding issue, but rather a UserControl specific feature, for which the constructor is not called on purpose.

    I'll update the issue to reflect the actual problem.

    Workaround

    In Level1.xaml.cs, name the first StackPanel as root, then do this instead:

    partial void OnInitializeCompleted()
    {
       root.DataContext = new TestMeModel();
    }

    Details

    Currently, the constructor of a user controls is not called directly, but rather calls a secondary constructors that allows for skipping the first level of a UserControl Xaml file. This choice was arbitrarily made when Android 4.4 was still in full swing, because the stack size on those android version for the main looper is very small (<32K).

    This Xaml user control interpretation relaxation allowed for richer UIs for those older Android versions, because there were less UI elements in the visual tree, and that we tend not to use code behind as well.

    To fix

    Add a feature flag that disables this behavior, while keeping the call to OnInitializeCompleted for backward compatibility.

  2. changed the title [-]Binding context issue on android[/-] [+]UserControl code-behind constructor is not called[/+] on Jun 26, 2018
  3. added
    kind/bug
    on Jun 26, 2018
  4. self-assigned this
    on Jun 1, 2020
  5. added 3 commits that reference this issue on Jun 1, 2020
    d8881fe
    3b18129
    481f20c
  6. added a commit that references this issue on Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions