dotnet / dotnet/maui

Generic BindableProperty<T>

Open
#28,100 1 comment 4 reactions 0 assignees View on GitHub
area-xaml proposal/open t/enhancement ☀️
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

```csharp

// proposal

public sealed class BindableProperty : BindableProperty
{
public void Set(BindableObject target, T value);
public T Get(BindableObject target);
}

// actual usage

public static readonly BindableProperty TextColorProperty
= BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(MyObject) , ...);

public Color TextColor {
get => (Color)GetValue(TextColorProperty); // potential type cast exception
set => SetValue(TextColorProperty, value); // also, boxing every time
}

// generic usage

public static readonly BindableProperty TextColorProperty
= BindableProperty.Create(nameof(TextColor) , ... );

// using like this
public Color TextColor {
get => GetValue(TextColorProperty); // type check in compile time
set => SetValue(TextColorProperty, value); // also, no boxing if no changes
}

// or like this
public Color TextColor {
get => TextColorProperty.Get(this);
set => TextColorProperty.Set(this, value);
}

```

Benefits:
1. slightly less code, slightly better readable
2. skip type checking and casting when setting new value
3. less boxing in some cases
4. more type checks in compile time
5. full compatible with actual api

### Public API Changes

```csharp
public sealed class BindableProperty : BindableProperty
{
public void Set(BindableObject target, T value);
public T Get(BindableObject target);
}

class BindableProperty {
// add generics
public static BindableProperty Create(string propName, Type declType, ... etc);
public static BindableProperty Create(string propName, ... etc);
public static BindableProperty CreateAttached(string propName, Type declType, ... etc);
public static BindableProperty CreateAttached(string propName, ... etc);
// etc...
}

class BindableObject {
// add generics
public void SetValue(BindableProperty bo, T value);
public T GetValue(BindableProperty bo);
}
```

### Intended Use-Case

See description...

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the existing BindableProperty and BindableObject APIs referenced in the issue, then compare the proposed generic overloads with the current API surface and compatibility requirements. Done would mean the BindableProperty design, factory overloads, and typed GetValue/SetValue APIs are specified and validated against the stated usage examples.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.