microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

C++/WinRT - Can't two-way bind primitive types to IInspectable #7986

Open JaiganeshKumaran opened 1 year ago

JaiganeshKumaran commented 1 year ago

Describe the bug

Can't two-way bind a property of a primitive type to IInspectable because the generated code uses unknown_cast, which doesn't exist, instead of unbox_value.

Steps to reproduce the bug

  1. Create a property of a primitive type in a view model.
  2. Two-way bind it from XAML on a property of type IInspectable (Object).
  3. See error in the generated code.

Expected behavior

Generated code should correctly use unbox_value.

Screenshots

Screenshot 2022-12-03 at 15 50 53

NuGet package version

WinUI 2 - Microsoft.UI.Xaml 2.8.1

Windows version

Windows 11 (21H2): Build 22000

Additional context

Workaround by defining unknown_cast in pch.h:

namespace winrt
{
    template <typename T>
    T unknown_cast(IInspectable const& value)
    {
        return unbox_value<T>(value);
    }
}
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

JaiganeshKumaran commented 1 year ago

This issue needs to be fixed, as it prevents basic tasks with x:Bind in a C++/WinRT application. Please anyone.

JaiganeshKumaran commented 9 months ago

@bpulliam

JaiganeshKumaran commented 9 months ago

Also, consider allowing C++ developers to contribute to the open-source XAML compiler.

JesseCol commented 6 months ago

Hi, does this repro in WinAppSDK / WinUI3? Or just in WinUI2? Thanks!

JaiganeshKumaran commented 6 months ago

@JesseCol Yes, the generated code is still broken in WinUI 3.

Screenshot 2024-03-02 at 10 13 23