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

Cannot {x:Bind} to property of named XAML control in C++/WinRT #2721

Closed
sylveon opened this issue Jun 21, 2020 · 10 comments
Closed

Cannot {x:Bind} to property of named XAML control in C++/WinRT #2721

sylveon opened this issue Jun 21, 2020 · 10 comments
Labels
area-Binding area-C++/WinRT Issues related with C++/WinRT experience area-XamlCompiler product-winui3 WinUI 3 issues team-Markup Issue for the Markup team
Milestone

Comments

@sylveon
Copy link
Contributor

sylveon commented Jun 21, 2020

Describe the bug
The autogenerated binding code tries to find the named XAML control through the projected type rather than the implementation type, resulting in a compile-time error.

Steps to reproduce the bug

  1. Create a new C++/WinRT XAML project
  2. Add <winrt/Windows.UI.Xaml.Media.h> to the precompiled headers (to allow SolidColorBrush to be usable)
  3. Add the following XAML code somewhere in the MainPage
<StackPanel>
    <ColorPicker x:Name="Picker" IsAlphaEnabled="True" IsMoreButtonVisible="True" IsColorPreviewVisible="False" />

    <Rectangle Width="100" Height="100">
        <Rectangle.Fill>
            <SolidColorBrush Color="{x:Bind Picker.Color, Mode = OneWay}" />
        </Rectangle.Fill>
    </Rectangle>
</StackPanel>
  1. Build the code

Expected behavior
The code builds fine.

Version Info

NuGet package version: [Microsoft.Windows.CppWinRT 2.0.200615.7]

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
The fix should be quite simple. Here's the wrong autogenerated code:

void Update_(::winrt::BlankApp1::MainPage obj, int32_t phase)
{
    if (obj)
    {
        if ((phase & (NOT_PHASED | (1 << 0))) != 0)
        {
            Update_Picker(obj.Picker(), phase);
        }
    }
}

A correct version would be:

void Update_(::winrt::BlankApp1::MainPage obj, int32_t phase)
{
    if (obj)
    {
        if ((phase & (NOT_PHASED | (1 << 0))) != 0)
        {
            Update_Picker(obj.as<::winrt::BlankApp1::implementation::MainPage>().Picker(), phase);
        }
    }
}

Alternatively, I believe it would be possible to instead have Update_ directly take the implementation type:

void Update_(::winrt::BlankApp1::implementation::MainPage obj, int32_t phase)
{
    if (obj)
    {
        if ((phase & (NOT_PHASED | (1 << 0))) != 0)
        {
            Update_Picker(obj.Picker(), phase);
        }
    }
}

by adjusting the generated struct declaration to refer to the implementation type, like this:

    template <typename D, typename ... I>
    struct MainPageT<D, I...>::MainPage_obj1_Bindings
        : public ::winrt::BlankApp1::implementation::ReferenceTypeXamlBindings<::winrt::BlankApp1::implementation::MainPage, ::winrt::BlankApp1::implementation::XamlBindingTrackingBase>
{
@msft-github-bot msft-github-bot added the needs-triage Issue needs to be triaged by the area owners label Jun 21, 2020
@sylveon
Copy link
Contributor Author

sylveon commented Jun 21, 2020

Additionally, looks like neither Binding works, the rectangle is just permanently black 😞

@StephenLPeters StephenLPeters added the team-Markup Issue for the Markup team label Jun 22, 2020
@JeanRoca JeanRoca added needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) and removed needs-triage Issue needs to be triaged by the area owners labels Dec 2, 2020
@JeanRoca
Copy link

JeanRoca commented Dec 2, 2020

Hey @sylveon would adding ColorPicker Picker;​​ to your IDL file fix this issue?

@sylveon
Copy link
Contributor Author

sylveon commented Dec 2, 2020

Yes, it does, but I don't want to expose it to my consumers like that.

@JeanRoca
Copy link

JeanRoca commented Dec 3, 2020

Oh I see that makes sense thank you for clarifying. In that case, there are ways to filter metadata, so that you don't expose this to customers. Adding @kennykerr and @BenJKuhn who may be able to help workaround this while we wait on a real fix.

@JeanRoca JeanRoca added the area-C++/WinRT Issues related with C++/WinRT experience label Dec 3, 2020
@evelynwu-msft evelynwu-msft added product-winui3 WinUI 3 issues and removed needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) labels May 16, 2023
@evelynwu-msft evelynwu-msft added the fixed-internally This bug has been fixed, and the fix will be shipped in the next version of WinUI 3. label Jun 13, 2023
@evelynwu-msft
Copy link
Contributor

Fix will be available in WinAppSDK 1.4.

@sylveon
Copy link
Contributor Author

sylveon commented Jun 13, 2023

That's great news! Any chance at backporting to UWP?

@evelynwu-msft
Copy link
Contributor

It's very unlikely given that a workaround (as ugly as it is) exists.

@duncanmacmichael
Copy link
Member

Fixed in 1.4.0. @bpulliam please close this issue as I cannot. :)

@sylveon
Copy link
Contributor Author

sylveon commented Aug 30, 2023

Not fixed in system XAML :(

@bpulliam
Copy link
Contributor

Not fixed in system XAML :(

We are focusing our resources and efforts on WinUI 3. The bar for backporting changes to system Xaml is very high.

@bpulliam bpulliam removed the fixed-internally This bug has been fixed, and the fix will be shipped in the next version of WinUI 3. label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding area-C++/WinRT Issues related with C++/WinRT experience area-XamlCompiler product-winui3 WinUI 3 issues team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

7 participants