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:Since 2.0.240111.5, I get a compile error with the GetTemplateChild function. #1393

Closed
takatsuka2022 opened this issue Feb 27, 2024 · 5 comments

Comments

@takatsuka2022
Copy link

Version

2.0.240111.5

Summary

I think GetTemplateChild was a public function, but since 2.0.240111.5 it has been changed to protected, so I cannot access it from outside the class.
Is there a workaround for this?

Reproducible example

.xaml
<ColorPicker x:Name="MemoColorPicker" Grid.Row="0" Grid.Column="1" Margin="20,10,30,0"
 ColorSpectrumShape="Box" IsAlphaSliderVisible="False" IsColorPreviewVisible="False"
 IsColorChannelTextInputVisible="False" IsHexInputVisible="False" IsColorSliderVisible="False"/>


.cpp
auto spectrum = MemoColorPicker().GetTemplateChild(L"ColorSpectrum").try_as<Controls::Primitives::ColorSpectrum>();
if (spectrum != nullptr) {
    auto tool_chip = spectrum.GetTemplateChild(L"ColorNameToolTip").try_as<Controls::ToolTip>();
    if (tool_chip != nullptr) tool_chip.Visibility(Visibility::Collapsed);
}

Expected behavior

No response

Actual behavior

No response

Additional comments

No response

@dmachaj
Copy link
Contributor

dmachaj commented Feb 27, 2024

This seems to be related to #1319. Adding a .as or .try_as cast from spectrum to xaml::FrameworkElement before calling GetTemplateChild should fix the break.

Also @sylveon for a second opinion on this topic.

@sylveon
Copy link
Contributor

sylveon commented Feb 27, 2024

GetTemplateChild has always been a protected method in WinRT metadata so this is an intentional result of this change. It was never meant to be public.

You can quickly workaround this issue by doing .as<IControlProtected>().GetTemplateChild(), however the proper way to do what you're trying to do here is to extend ColorPicker and override OnApplyTemplate, or retemplate ColorPicker.

@takatsuka2022
Copy link
Author

auto spectrum = MemoColorPicker().as<IControlProtected>().GetTemplateChild(L"ColorSpectrum").try_as<Controls::Primitives::ColorSpectrum>();

I wrote the above code, but the return value is NULL. Is this wrong?

@sylveon
Copy link
Contributor

sylveon commented Feb 27, 2024

This seems to work as expected for me:
image

Does your code work as expected with the previous versions of cppwinrt? Perhaps it runs before the template gets applied.

@takatsuka2022
Copy link
Author

The problem has been resolved successfully.
Thank you very much.

rozele added a commit to rozele/react-native-windows that referenced this issue Mar 20, 2024
See microsoft/cppwinrt#1393

We need to first convert the WinUI 3 control to IControlProtected to
access GetTemplateChild.
acoates-ms pushed a commit to microsoft/react-native-windows that referenced this issue Mar 28, 2024
* Support WinAppSDK + cppwinrt 2.240111.5

See microsoft/cppwinrt#1393

We need to first convert the WinUI 3 control to IControlProtected to
access GetTemplateChild.

* Change files

* Apply C++/WinRT fix to both UWP and WinAppSDK
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