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

Using static styles in templated control works incorrectly #2399

Closed
MartinZikmund opened this issue Jun 27, 2024 Discussed in #2349 · 7 comments
Closed

Using static styles in templated control works incorrectly #2399

MartinZikmund opened this issue Jun 27, 2024 Discussed in #2349 · 7 comments
Assignees

Comments

@MartinZikmund
Copy link
Member

Discussed in #2349

Originally posted by kucint June 13, 2024
This issue is a continuation of #2316.

Repro steps:
Create a simple control with two predefined static styles: MyStyleA and MyStyleB
Assign MyStyleA to your control's style.


Issue 1:

this issue can be observed on windows:
TargetFramework: net8.0-desktop;

Expected behavior: MyStyleA is used,
Actual behavior: MyStyleB is used,

internal sealed class MyControl : Control
{
    public static readonly Style<MyControl> _styleA = new MyStyleA();   // red
    public static readonly Style<MyControl> _styleB = new MyStyleB();   // blue

    public MyControl()
    {
        DefaultStyleKey = typeof(MyControl);

        Style = _styleA;              // uses style B: error, expected style A
        //Style = _styleB;            // uses style B
        //Style = new MyStyleA();     // uses style A
        //Style = new MyStyleB();     // uses style B
    }
}

internal class MyStyleA : Style<MyControl>
{
    public MyStyleA() => Setters(s => s
        .Template(ctrl =>
            new Frame()
            .Background(Colors.LightCoral)
        ));
}

internal class MyStyleB : Style<MyControl>
{
    public MyStyleB() => Setters(s => s
        .Template(ctrl =>
            new Frame()
            .Background(Colors.LightSkyBlue)
        ));
}

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this
            .Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
            .Content(new StackPanel()
            .VerticalAlignment(VerticalAlignment.Center)
            .HorizontalAlignment(HorizontalAlignment.Center)
            .Children(
                new TextBlock()
                .Text("Hello Uno Platform!"),
                new MyControl()
                .Width(200)
                .Height(200)
            ));
    }
}

Issue 2:

this issue can be observed on windows:
TargetFramework: net8.0-windows10.0.19041

Application is unstable:
One of following may happen:

  • app starts without errors but MyControl is not presented, terminates with error code = 0
  • app starts without errors but MyControl is not presented, terminates with error code = 0xc0000374
  • app fail to start throwing Exception = {"No installed components were detected.\r\n\r\nCannot apply a Style with TargetType 'Microsoft.UI.Xaml.Controls.ControlTemplate' to an object of type 'UnoChangingControlStyleApp.Controls.MyControl'."}

See #2311 for similar behavior


"Uno.Sdk": "5.2.161"
repro: UnoChangingControlStyleApp.zip

@Youssef1313
Copy link
Member

C# Markup wasn't setting TargetType correctly, this is fixed in 5.4.0-dev.27. Once a package is released, I'll re-test the behavior on WinUI, and also check the other issues.

@Youssef1313 Youssef1313 self-assigned this Jul 9, 2024
@Youssef1313
Copy link
Member

Youssef1313 commented Jul 10, 2024

For the wrong style issue, the remaining issue is also a C# Markup bug which we will track.

As a workaround, create extra empty classes:

public partial class FrameA : Frame { }
public partial class FrameB : Frame { }

And use them in the template of the styles. So, instead of new Frame(), do new FrameA() in StyleA and new FrameB() in StyleB.

And yes, I know it doesn't make any sense that my suggestion can have an impact on behavior, but it does due to some implementation details of C# Markup.

Alternative workaround: In only one of the templates, cast the Frame to Control

@Youssef1313
Copy link
Member

Re-opening to validate the recent fixes and investigate more

@Youssef1313 Youssef1313 reopened this Jul 11, 2024
@Youssef1313
Copy link
Member

This is now fixed in C# Markup 5.4.0-dev.30.

@kucint
Copy link

kucint commented Jul 22, 2024

I tested it with the newest branch "Uno.Sdk": "5.4.0-dev.145" and it does not work

@Youssef1313
Copy link
Member

Youssef1313 commented Jul 22, 2024

@kucint Can you verify you are not overriding UnoCSharpMarkupVersion with an old version?

If you have <UnoCSharpMarkupVersion>SomeVersion</UnoCSharpMarkupVersion> somewhere (e.g, Directory.Build.props), remove that line

@kucint
Copy link

kucint commented Jul 22, 2024

you are right, I forgot that this section is no longer necessary starting from "Uno.Sdk": "5.4.0"

  <PropertyGroup>
    <UnoExtensionsVersion>4.1.24</UnoExtensionsVersion>
    <UnoToolkitVersion>6.0.24</UnoToolkitVersion>
    <UnoThemesVersion>5.0.13</UnoThemesVersion>
    <UnoCSharpMarkupVersion>5.2.14</UnoCSharpMarkupVersion>
  </PropertyGroup>

My bad, sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants