|
| 1 | +XamlBindingHelper |
| 2 | +=== |
| 3 | + |
| 4 | +# Background |
| 5 | + |
| 6 | +[`XamlBindingHelper`](https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.markup.xamlbindinghelper) |
| 7 | +is a utility class in WinUI 3 that provides static helper methods for setting dependency property |
| 8 | +values without boxing them to `IInspectable`. It already has overloads for primitive types such as |
| 9 | +`Int32`, `Double`, `Boolean`, `String`, and several `Windows.Foundation` structs (`Point`, `Rect`, |
| 10 | +`Size`, `TimeSpan`). |
| 11 | + |
| 12 | +In WinUI 2, developers could use the |
| 13 | +[`XamlDirect`](https://learn.microsoft.com/uwp/api/windows.ui.xaml.core.direct.xamldirect) API to |
| 14 | +set struct-typed property values directly without boxing. For example `SetThicknessProperty`, |
| 15 | +`SetCornerRadiusProperty`, and `SetColorProperty`. `XamlDirect` was intentionally not carried |
| 16 | +forward to WinUI 3 because it is a narrow, low-level API with a limited number of consumers and |
| 17 | +significant maintenance costs. |
| 18 | + |
| 19 | +However, there is a gap: when setting struct-typed dependency properties — |
| 20 | +`Microsoft.UI.Xaml.Thickness`, `Microsoft.UI.Xaml.CornerRadius`, and `Windows.UI.Color` — developers currently have no option other |
| 21 | +than boxing the value to `Object` first: |
| 22 | + |
| 23 | +* C#: |
| 24 | +```csharp |
| 25 | +// WinUI 3 — current approach (boxing required) |
| 26 | +var border = new Border(); |
| 27 | +border.SetValue(Border.BorderThicknessProperty, new Thickness(2, 4, 2, 4)); |
| 28 | +``` |
| 29 | + |
| 30 | +* C++: |
| 31 | +```cpp |
| 32 | +// WinUI 3 — current approach (boxing required) |
| 33 | +auto border = Border(); |
| 34 | +border.SetValue(Border::BorderThicknessProperty(), box_value(Thickness{ 2, 4, 2, 4 })); |
| 35 | +``` |
| 36 | +
|
| 37 | +This spec extends `XamlBindingHelper` with three new struct-typed overloads to close that gap: |
| 38 | +
|
| 39 | +- `SetPropertyFromThickness` |
| 40 | +- `SetPropertyFromCornerRadius` |
| 41 | +- `SetPropertyFromColor` |
| 42 | +
|
| 43 | +With these additions, developers can set struct-typed values on dependency properties without any |
| 44 | +boxing: |
| 45 | +
|
| 46 | +* C#: |
| 47 | +```csharp |
| 48 | +// WinUI 3 — new approach (no boxing) |
| 49 | +var border = new Border(); |
| 50 | +var thickness = new Thickness(2, 4, 2, 4); |
| 51 | +XamlBindingHelper.SetPropertyFromThickness(border, Border.BorderThicknessProperty, thickness); |
| 52 | +``` |
| 53 | + |
| 54 | +* C++: |
| 55 | +```cpp |
| 56 | +// WinUI 3 — new approach (no boxing) |
| 57 | +XamlBindingHelper::SetPropertyFromThickness( |
| 58 | + border, |
| 59 | + Border::BorderThicknessProperty(), |
| 60 | + Thickness{ 2, 4, 2, 4 }); |
| 61 | +``` |
| 62 | +
|
| 63 | +# Conceptual pages (How To) |
| 64 | +
|
| 65 | +## Setting struct-typed dependency properties without boxing |
| 66 | +
|
| 67 | +Before these APIs, setting a struct-typed value on a dependency property required boxing the value |
| 68 | +to `Object` first: |
| 69 | +
|
| 70 | +* C#: |
| 71 | +```csharp |
| 72 | +var border = new Border(); |
| 73 | +border.SetValue(Border.BorderThicknessProperty, new Thickness(2, 4, 2, 4)); |
| 74 | +``` |
| 75 | + |
| 76 | +* C++: |
| 77 | +```cpp |
| 78 | +auto border = Border(); |
| 79 | +border.SetValue(Border::BorderThicknessProperty(), box_value(Thickness{ 2, 4, 2, 4 })); |
| 80 | +``` |
| 81 | +
|
| 82 | +With the new APIs, the struct can be passed directly and no boxing is needed in application code: |
| 83 | +
|
| 84 | +* C#: |
| 85 | +```csharp |
| 86 | +var border = new Border(); |
| 87 | +var thickness = new Thickness(2, 4, 2, 4); |
| 88 | +XamlBindingHelper.SetPropertyFromThickness(border, Border.BorderThicknessProperty, thickness); |
| 89 | +``` |
| 90 | + |
| 91 | +* C++: |
| 92 | +```cpp |
| 93 | +auto border = Border(); |
| 94 | +Thickness thickness{ 2, 4, 2, 4 }; |
| 95 | +XamlBindingHelper::SetPropertyFromThickness(border, Border::BorderThicknessProperty(), thickness); |
| 96 | +``` |
| 97 | +
|
| 98 | +# API Pages |
| 99 | +
|
| 100 | +_(Each level-two section below maps to a docs.microsoft.com API page.)_ |
| 101 | +
|
| 102 | +## XamlBindingHelper.SetPropertyFromThickness method |
| 103 | +
|
| 104 | +Sets a `Microsoft.UI.Xaml.Thickness` value on the specified dependency property of an object, |
| 105 | +without requiring the caller to box the struct to `IInspectable`. |
| 106 | +
|
| 107 | +```idl |
| 108 | +static void SetPropertyFromThickness( |
| 109 | + Object dependencyObject, |
| 110 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 111 | + Microsoft.UI.Xaml.Thickness value); |
| 112 | +``` |
| 113 | + |
| 114 | +### Parameters |
| 115 | + |
| 116 | +| Parameter | Type | Description | |
| 117 | +|---|---|---| |
| 118 | +| `dependencyObject` | `object` | The target object that owns `propertyToSet`. | |
| 119 | +| `propertyToSet` | `DependencyProperty` | The dependency property to assign the value to. | |
| 120 | +| `value` | `Microsoft.UI.Xaml.Thickness` | The `Thickness` value to set. | |
| 121 | + |
| 122 | +### Examples |
| 123 | + |
| 124 | +* C#: |
| 125 | +```csharp |
| 126 | +var border = new Border(); |
| 127 | +var thickness = new Thickness(2, 4, 2, 4); |
| 128 | +XamlBindingHelper.SetPropertyFromThickness(border, Border.BorderThicknessProperty, thickness); |
| 129 | +``` |
| 130 | + |
| 131 | +* C++: |
| 132 | +```cpp |
| 133 | +auto border = Border(); |
| 134 | +Thickness thickness{ 2, 4, 2, 4 }; |
| 135 | +XamlBindingHelper::SetPropertyFromThickness(border, Border::BorderThicknessProperty(), thickness); |
| 136 | +``` |
| 137 | +
|
| 138 | +### Remarks |
| 139 | +
|
| 140 | +* If `dependencyObject` or `propertyToSet` is `null`, an exception is thrown by the WinRT layer from the underlying `E_POINTER` failure. |
| 141 | +
|
| 142 | +## XamlBindingHelper.SetPropertyFromCornerRadius method |
| 143 | +
|
| 144 | +Sets a `Microsoft.UI.Xaml.CornerRadius` value on the specified dependency property of an object, |
| 145 | +without requiring the caller to box the struct to `IInspectable`. |
| 146 | +
|
| 147 | +```idl |
| 148 | +static void SetPropertyFromCornerRadius( |
| 149 | + Object dependencyObject, |
| 150 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 151 | + Microsoft.UI.Xaml.CornerRadius value); |
| 152 | +``` |
| 153 | + |
| 154 | +### Parameters |
| 155 | + |
| 156 | +| Parameter | Type | Description | |
| 157 | +|---|---|---| |
| 158 | +| `dependencyObject` | `object` | The target object that owns `propertyToSet`. | |
| 159 | +| `propertyToSet` | `DependencyProperty` | The dependency property to assign the value to. | |
| 160 | +| `value` | `Microsoft.UI.Xaml.CornerRadius` | The `CornerRadius` value to set. | |
| 161 | + |
| 162 | +### Examples |
| 163 | + |
| 164 | +* C#: |
| 165 | +```csharp |
| 166 | +var border = new Border(); |
| 167 | +var cornerRadius = new CornerRadius(5, 10, 15, 20); |
| 168 | +XamlBindingHelper.SetPropertyFromCornerRadius(border, Border.CornerRadiusProperty, cornerRadius); |
| 169 | +``` |
| 170 | + |
| 171 | +* C++: |
| 172 | +```cpp |
| 173 | +auto border = Border(); |
| 174 | +CornerRadius cornerRadius{ 5, 10, 15, 20 }; |
| 175 | +XamlBindingHelper::SetPropertyFromCornerRadius(border, Border::CornerRadiusProperty(), cornerRadius); |
| 176 | +``` |
| 177 | +
|
| 178 | +### Remarks |
| 179 | +
|
| 180 | +* If `dependencyObject` or `propertyToSet` is `null`, an exception is thrown by the WinRT layer from the underlying `E_POINTER` failure. |
| 181 | +
|
| 182 | +## XamlBindingHelper.SetPropertyFromColor method |
| 183 | +
|
| 184 | +Sets a `Windows.UI.Color` value on the specified dependency property of an object, without |
| 185 | +requiring the caller to box the struct to `IInspectable`. |
| 186 | +
|
| 187 | +```idl |
| 188 | +static void SetPropertyFromColor( |
| 189 | + Object dependencyObject, |
| 190 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 191 | + Windows.UI.Color value); |
| 192 | +``` |
| 193 | + |
| 194 | +### Parameters |
| 195 | + |
| 196 | +| Parameter | Type | Description | |
| 197 | +|---|---|---| |
| 198 | +| `dependencyObject` | `object` | The target object that owns `propertyToSet`. | |
| 199 | +| `propertyToSet` | `DependencyProperty` | The dependency property to assign the value to. | |
| 200 | +| `value` | `Windows.UI.Color` | The `Color` value to set. | |
| 201 | + |
| 202 | +### Examples |
| 203 | + |
| 204 | +* C#: |
| 205 | +```csharp |
| 206 | +var brush = new SolidColorBrush(); |
| 207 | +XamlBindingHelper.SetPropertyFromColor(brush, SolidColorBrush.ColorProperty, Colors.BlueViolet); |
| 208 | +DemoBorder.SetValue(Border.BorderBrushProperty, brush); |
| 209 | +``` |
| 210 | + |
| 211 | +* C++: |
| 212 | +```cpp |
| 213 | +auto brush = SolidColorBrush(); |
| 214 | +XamlBindingHelper::SetPropertyFromColor( |
| 215 | + brush, |
| 216 | + SolidColorBrush::ColorProperty(), |
| 217 | + Colors::BlueViolet()); |
| 218 | +DemoBorder().SetValue(Border::BorderBrushProperty(), brush); |
| 219 | +``` |
| 220 | +
|
| 221 | +### Remarks |
| 222 | +
|
| 223 | +* If `dependencyObject` or `propertyToSet` is `null`, an exception is thrown by the WinRT layer from the underlying `E_POINTER` failure. |
| 224 | +
|
| 225 | +# API Details |
| 226 | +
|
| 227 | +```idl |
| 228 | +namespace Microsoft.UI.Xaml.Markup |
| 229 | +{ |
| 230 | + [webhosthidden] |
| 231 | + [default_interface] |
| 232 | + runtimeclass XamlBindingHelper |
| 233 | + { |
| 234 | + // Existing members omitted for brevity |
| 235 | +
|
| 236 | + { |
| 237 | + static void SetPropertyFromThickness( |
| 238 | + Object dependencyObject, |
| 239 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 240 | + Microsoft.UI.Xaml.Thickness value); |
| 241 | +
|
| 242 | + static void SetPropertyFromCornerRadius( |
| 243 | + Object dependencyObject, |
| 244 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 245 | + Microsoft.UI.Xaml.CornerRadius value); |
| 246 | +
|
| 247 | + static void SetPropertyFromColor( |
| 248 | + Object dependencyObject, |
| 249 | + Microsoft.UI.Xaml.DependencyProperty propertyToSet, |
| 250 | + Windows.UI.Color value); |
| 251 | + } |
| 252 | + }; |
| 253 | +} |
| 254 | +``` |
0 commit comments