Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserControl code-behind constructor is not called #61

Closed
Henk7 opened this issue Jun 23, 2018 · 1 comment · Fixed by #3279
Closed

UserControl code-behind constructor is not called #61

Henk7 opened this issue Jun 23, 2018 · 1 comment · Fixed by #3279
Assignees
Labels
kind/bug Something isn't working

Comments

@Henk7
Copy link

Henk7 commented Jun 23, 2018

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.

@jeromelaban
Copy link
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.

@jeromelaban jeromelaban changed the title Binding context issue on android UserControl code-behind constructor is not called Jun 26, 2018
@jeromelaban jeromelaban added the kind/bug Something isn't working label Jun 26, 2018
jeromelaban added a commit that referenced this issue Jul 8, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
jeromelaban added a commit that referenced this issue Jul 8, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
jeromelaban added a commit that referenced this issue Jul 9, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
jeromelaban added a commit that referenced this issue Jul 15, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
jeromelaban added a commit that referenced this issue Jul 15, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
NicolasChampagne pushed a commit that referenced this issue Jul 15, 2019
This is a preliminary workaround for #61. This is not enabled by default is may be breaking. The default value will be changed in the future as part of a larger set of breaking changes.
@jeromelaban jeromelaban self-assigned this Jun 1, 2020
jeromelaban added a commit that referenced this issue Jun 1, 2020
BREAKING CHANGE: This update now forces the materialization of UserControl instances in the visual tree.

Fixes #61
@jeromelaban jeromelaban mentioned this issue Jun 1, 2020
6 tasks
jeromelaban added a commit that referenced this issue Jun 1, 2020
BREAKING CHANGE: This update now forces the materialization of UserControl instances in the visual tree.

To revert this change, add the following in the csproj:

```xml
<UnoSkipUserControlsInVisualTree>true</UnoSkipUserControlsInVisualTree>
```

Fixes #61
jeromelaban added a commit that referenced this issue Jun 2, 2020
BREAKING CHANGE: This update now forces the materialization of UserControl instances in the visual tree.

- UserControl no longer incorrectly accepts VisualStateManager, and must be placed on the first child
- To restore the skipping of UserControl instances, add the following in the csproj:

```xml
<UnoSkipUserControlsInVisualTree>true</UnoSkipUserControlsInVisualTree>
```

Fixes #61
lukeblevins pushed a commit to lukeblevins/uno that referenced this issue Jan 17, 2024
Implemented Android splash screen on old templates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants