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

[Desktop] x:Bind local does not work in Custom Control #18404

Closed
kucint opened this issue Oct 8, 2024 · 10 comments · Fixed by #18695
Closed

[Desktop] x:Bind local does not work in Custom Control #18404

kucint opened this issue Oct 8, 2024 · 10 comments · Fixed by #18695
Assignees
Labels
area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind kind/bug Something isn't working

Comments

@kucint
Copy link

kucint commented Oct 8, 2024

Current behavior

Issue 1
binding to local constant works in Page but not working in Control

<Grid
  Width="{x:Bind local:MyControl.MyWidth}"
  Height="{x:Bind local:MyControl.MyHeight}" />

Code behind:

public sealed partial class MyControl : Control
{
    public const double MyWidth = 200;
    public const double MyHeight = 200;
}

Issue 2

Discrepancy between WinAppSDK and Desktop: WinAppSDK works fine with private const while Desktop requires public const

WinAppSDK

public sealed partial class MyControl : Control
{
    private const double MyWidth = 200;
    private const double MyHeight = 200;
}

Desktop

public sealed partial class MyControl : Control
{
    public const double MyWidth = 200;
    public const double MyHeight = 200;
}

Expected behavior

binding to local constant shall work in Control

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

MINIMAL REPRO PROJECT: XBindLocalApp.zip

STEPS TO REPRODUCE:
run the app on Windows with WinAppSDK framework selected: all works fine:
image

run the app on Windows with Desktop framework selected: binding works in Page but fails in Control
image

Workaround

define constant in XAML rather than in C# code behind

Works on UWP/WinUI

Yes

Environment

No response

NuGet package version(s)

"Uno.Sdk": "5.4.8"

Affected platforms

Skia (WPF)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@kucint kucint added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Oct 8, 2024
@Youssef1313 Youssef1313 added the area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind label Oct 8, 2024
@MartinZikmund MartinZikmund self-assigned this Oct 16, 2024
@kucint
Copy link
Author

kucint commented Nov 5, 2024

@MartinZikmund: still not working on "Uno.Sdk": "5.5.32"

@MartinZikmund MartinZikmund removed triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Nov 5, 2024
@MartinZikmund
Copy link
Member

@kucint This was more complex than I originally hoped. I think the PR I opened has the fix, but will need to validate it against all tests.

In the meantime a good way to workaround this could be to define the constants in XAML insted, e.g. in a resource dictionary you can do:

<x:Double x:Name="MyControlSize">200</x:Double>

And then use the resource in the template:

Width="{StaticResource MyControlSize}"

Ultimately the result should be equivalent, even performance-wise (and could actually even be more performant).

@kucint
Copy link
Author

kucint commented Nov 5, 2024

@MartinZikmund : defining constants in resources is XAML is not an option to me.
What could be an option to me is to add constant values to the resource dictionary from code behind.
Would it be as performant as adding them directly in XAML?

@MartinZikmund
Copy link
Member

@kucint hopefully my PR will avoid the need for this, but yes, you could set the resources early on app startup from C#, it would have the same end result, while keeping the consts in C#

@kucint
Copy link
Author

kucint commented Nov 15, 2024

I upgraded my test app to the newest dotnet and newest "Uno.Sdk": "5.5.43": XBindLocalApp.zip
and it is still not working.

@kucint
Copy link
Author

kucint commented Nov 15, 2024

@MartinZikmund
Unfortunately, I ask you to reopen this issue, because the attached test app is still not working under newest dotnet and newest uno.sdk "Uno.Sdk": "5.5.43"

@jeromelaban
Copy link
Member

jeromelaban commented Nov 15, 2024

The change is not present in uno 5.5, it will only be in Uno 5.6 at this time.

@kucint
Copy link
Author

kucint commented Nov 15, 2024

I see, is it present in dev branch?

@jeromelaban
Copy link
Member

Not yet, we're currently ingesting the .NET 9 changes. It will be available in the dev branch most likely in the next few days.

@kucint
Copy link
Author

kucint commented Nov 15, 2024

Thanks @jeromelaban for info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/xBind 🪢 Categorizes an issue or PR as relevant to x:Bind kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants