microsoft / microsoft/microsoft-ui-xaml
x:Bind with function binding requires the binding source to be redeclared in idl in C++winrt
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
In winui3, `x:Bind` binding to an object's property is now fixed. The binding source does NOT need to be redeclared in idl. (This is an issue dates back from UWP)
However, `x:Bind` binding to an object's **method** still requires redeclaring it in idl (otherwise fails to build), which is inconsistent and causes confusion.
### Why is this important?
This is a xaml codegen issue in C++WinRT
### Steps to reproduce the bug
1. Create a new winui3 C++ project
2. Create a ViewModel with this idl. Implement the method with stub value
```
runtimeclass ViewModel
{
ViewModel();
Int32 MyProperty;
void Print();
}
```
3. In `MainWindow.xaml`, use this code
```xml
```
### Actual behavior
Build error:
```
C:\Users\Peter\Desktop\WinUI-Bug\35-xBindToFunction\Generated Files\MainWindow.xaml.g.hpp(108,51): error C2039: 'ViewModel': is not a member of 'winrt::_35_xBindToFunction::MainWindow'
01:54:31:318 1> C:\Users\Peter\Desktop\WinUI-Bug\35-xBindToFunction\Generated Files\MainWindow.xaml.g.hpp(110,51): error C2039: 'ViewModel': is not a member of 'winrt::_35_xBindToFunction::MainWindow'
```
You have to redeclare `ViewModel` in `MainWindow.idl` like this
```idl
runtimeclass MainWindow : Microsoft.UI.Xaml.Window
{
MainWindow();
ViewModel ViewModel{ get; };
}
```
### Expected behavior
It should not needing to redeclare it in idl. Like this property binding
```xml
```
### Screenshots
_No response_
### NuGet package version
2.3.0
### Windows version
_No response_
### Additional context
[Repro here](https://github.com/HO-COOH/WinUI-Bug/tree/main/35-xBindToFunction)
Contributor guide
Research direction
Reproduce the issue with the linked 35-xBindToFunction project, then inspect MainWindow.xaml, MainWindow.idl, and the generated MainWindow.xaml.g.hpp around the reported errors. Compare method binding with the working property binding; done means the method binding builds without redeclaring ViewModel in MainWindow.idl.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100