dotnet / dotnet/maui

[Missing API] BindingContext missing in IView

Open
#28,526 2 comments 0 reactions 0 assignees View on GitHub
area-architecture proposal/open
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

Hello,

In MAUI, `Layout.Children` returns `IList`
In Xamarin, `Layout.Children` returned `IList`

Unfortunately, the `IView `interface does not expose the vital `View`-related property `BindingContext`, where the `View `class does.

Therefore, in Xamarin.Forms, we could eg. execute such code with ease:
`View view = MyStackLayout.Children.FirstOrDefault(c => c.BindingContext == MyViewModel);`

Where in .NET MAUI we have to do small hack:
`View view = MyStackLayout.Children.Cast().FirstOrDefault(c => c.BindingContext == MyViewModel);`

This adds an additional overhead needed for casting and complicates the code. This implies even more hacks when working with indices of `Children `property.

In Xamarin.Forms, we could do this for instance:
`someList.Add(MyStackLayout.Children[i].BindingContext as MyViewModel);`

Where in .NET MAUI we have to do multiple castings this time around, adding even more overhead to the processing:
`someList.Add(MyStackLayout.Children.Cast().ToArray()[i].BindingContext as MyViewModel);`

Please either:

- add `BindingContext `property to IView interface OR
- change signature of `Layout.Children` back to what Xamarin.Forms had - `IList` instead of `IList`

### Public API Changes

- add `BindingContext `property to IView interface OR
- change signature of `Layout.Children` back to what Xamarin.Forms had - `IList` instead of `IList`

`View view = MyStackLayout.Children.FirstOrDefault(c => c.BindingContext == MyViewModel);`

`someList.Add(MyStackLayout.Children[i].BindingContext as MyViewModel);`

### Intended Use-Case

Easier API to work with `BindingContext `that does not require hacky `Cast` in order to access `BindingContext `from `Layout.Children` elements.
This is for both performance and ease of use.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the IView interface and Layout.Children API definitions to compare the current MAUI surface with the Xamarin.Forms behavior described here. Determine which proposed API direction is compatible with the existing design and assess its impact on callers; done means the chosen public API supports accessing BindingContext without the described casts.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend, mobile-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.