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

Bug: Unable to Inherit from MUX WinUI 2.x Controls in C# #3502

Open
robloo opened this issue Oct 27, 2020 · 12 comments
Open

Bug: Unable to Inherit from MUX WinUI 2.x Controls in C# #3502

robloo opened this issue Oct 27, 2020 · 12 comments
Labels
area-NugetPackage Issue with the nuget package developer experience (e.g. build error, missing files) area-XamlCompiler bug Something isn't working needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) product-winui3 WinUI 3 issues team-Controls Issue for the Controls team team-Markup Issue for the Markup team wct

Comments

@robloo
Copy link
Contributor

robloo commented Oct 27, 2020

Describe the bug

Unable to create new controls inheriting from a MUX WinUI 2.x base control. The control template will never be applied.

Steps to reproduce the bug

Using the attached repro project below:

InheritTest.zip

  1. Using the WUX namespace set a breakpoint on base.OnApplyTemplate(). When code is run the breakpoint should be hit:
    image
  2. Change to the MUX namespace for the base control and run again with the same breakpoint:
    image
  3. The breakpoint in OnApplyTemplate() is never hit and the control template is never applied.

Note that if you change the DefaultStyleKey = typeof(ColorPicker) (the base type) that will work.

Expected behavior

It should be possible to inherit from a MUX WinUI 2.x base control and have the new control template applied.

Screenshots

Using WUX the template is correctly applied:
image

Using MUX no template is applied:
image

Also see above.

Version Info

NuGet package version:

Microsoft.UI.Xaml 2.4.3

Windows app type:

UWP Win32
Yes N/A
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

This is a pretty major roadblock. I need a work-around or fix pretty quickly...

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Oct 27, 2020
@robloo
Copy link
Contributor Author

robloo commented Oct 27, 2020

Seems I missed a bug report as well: #3499

@ranjeshj ranjeshj added team-Controls Issue for the Controls team area-NugetPackage Issue with the nuget package developer experience (e.g. build error, missing files) and removed needs-triage Issue needs to be triaged by the area owners labels Oct 27, 2020
@stevenbrix
Copy link
Contributor

@robloo can you try adding this in your control as a workaround?

this.DefaultStyleResourceUri = new System.Uri("ms-appx:///Themes/Generic.xaml");

@robloo
Copy link
Contributor Author

robloo commented Nov 12, 2020

@stevenbrix Thanks for that trick, I never knew that property existed allowing you to set the default style location. Always relied on the magic generic.xaml being found behind the scenes. Regardless, this did "fix" the issue or at least provide a work-around. I guess this is one of those things that will just disappear with WinUI 3.0 so the work-around is probably enough for now.

Edit: This might need to be looked at in more detail from a controls library perspective. It should probably automatically resolve correctly by itself somehow even when using a controls library NuGet.

@stevenbrix stevenbrix added team-Markup Issue for the Markup team needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) area-XamlCompiler labels Nov 13, 2020
@stevenbrix
Copy link
Contributor

@robloo, yeah this was something that I just came across a well. I agree, I like this better than the magical generic.xaml.

I don't think WinUI3 will fix this issue. The WCT had the exact same problem with WinUI3.

Edit: This might need to be looked at in more detail from a controls library perspective. It should probably automatically resolve correctly by itself somehow even when using a controls library NuGet.

I agree 100%. I think there are ways we could have our default item templates set this.

@MikeHillberg, do you have any thoughts?

@michael-hawker
Copy link
Collaborator

Any updates on this issue? Is this an inherent limitation in UWP compared to how this worked in WPF? Do we basically have to hard-code and merge the resource from the assembly into the app's resource dictionary? (See our discussion in the Toolkit here: CommunityToolkit/WindowsCommunityToolkit#3603)

@azchohfi
Copy link
Contributor

Any updates on this? We started to get this on our TabbedCommandBar, so maybe we'll need to update every control on the toolkit. This will be a big hassle for any library porting to WinUI3, so we would appreciate a fix.
@codendone Is this being tracked for 1.0?

@robloo
Copy link
Contributor Author

robloo commented Jun 29, 2021

For the Windows Community Toolkit ColorPicker I also had to add the work around.

https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/4010/files

@michael-hawker
Copy link
Collaborator

Just hit this again and forgot about DefaultStyleResourceUri.

However, when the platform can't find the specified style for a control before OnApplyTemplate is called, it feels like this should really be throwing an exception with those types of details like "Can't find resource X from ResourceUri Y" as then it'd be immediately obvious there's a problem. What it's trying to look for, and where it's trying to look (in the example above it'd be the WinUI generic.xaml instead of the Toolkit one as we would have expected).

Otherwise, with just a silent eating it, no OnApplyTemplate and no rendering of the control, it's just a black box and takes forever to debug. So many hours some times to realize what's going on or to find what's causing an issue.

michael-hawker added a commit to yoshiask/WindowsCommunityToolkit that referenced this issue Jul 23, 2021
…s. WinUI

See microsoft/microsoft-ui-xaml#3502

Also update TabbedCommandBarItem DataTemplate to use MUX version of NavigationViewItem
@mrlacey
Copy link
Contributor

mrlacey commented Jul 24, 2021

X-Ref related discussion in #5525

@michael-hawker
Copy link
Collaborator

Hit this again, almost forgot about it again...

michael-hawker added a commit to CommunityToolkit/Labs-Windows that referenced this issue Jun 8, 2022
Added workaround for DefaultStyleResourceUri - see microsoft/microsoft-ui-xaml#3502
Removed contract7Present from Style (our min target anyway) - see microsoft/microsoft-ui-xaml#2556
Tested on UWP, WinUI 3, and WASM!
michael-hawker added a commit to CommunityToolkit/Labs-Windows that referenced this issue Jul 8, 2022
Added workaround for DefaultStyleResourceUri - see microsoft/microsoft-ui-xaml#3502
Removed contract7Present from Style (our min target anyway) - see microsoft/microsoft-ui-xaml#2556
Tested on UWP, WinUI 3, and WASM!
@robloo
Copy link
Contributor Author

robloo commented Jul 19, 2023

Did this get addressed in WinUI 3? Seems pretty fundamental of an issue...

@duncanmacmichael duncanmacmichael added the bug Something isn't working label Nov 3, 2023
@evelynwu-msft evelynwu-msft added the product-winui3 WinUI 3 issues label Jan 27, 2024
@evelynwu-msft
Copy link
Contributor

Looks like this same bug exists in WinUI 3. Repro project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NugetPackage Issue with the nuget package developer experience (e.g. build error, missing files) area-XamlCompiler bug Something isn't working needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) product-winui3 WinUI 3 issues team-Controls Issue for the Controls team team-Markup Issue for the Markup team wct
Projects
None yet
Development

No branches or pull requests

8 participants