fix: SettingsPage NativeAOT crash — use BindableSelectionItem with [GeneratedBindableCustomProperty]#118
fix: SettingsPage NativeAOT crash — use BindableSelectionItem with [GeneratedBindableCustomProperty]#118
Conversation
…d SelectedIndex Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot How can I add GeneratedBindableCustomProperty to these nickvision.desktop properties? It's not a WinUI project Nickvision.Destkop. Can we add extensions here to support it, instead of removing it. I want to use itemsource and not create new items |
… for NativeAOT XAML binding Co-authored-by: nlogozzo <17648453+nlogozzo@users.noreply.github.com> Agent-Logs-Url: https://github.com/NickvisionApps/Application/sessions/105ab25b-0724-40f1-ac0e-3ca2799eb7f8
You cannot add The solution (commit // Models/BindableSelectionItem.cs
[GeneratedBindableCustomProperty]
public sealed partial class BindableSelectionItem(string label)
{
public string Label { get; } = label;
}The SettingsPage then builds a Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot I own Nickvision.Desktop and Nickvision.Desktop.WinUI. Would it work if I added this to the WinUI project: using Nickvision.Desktop.Application;
using WinRT;
namespace Nickvision.Desktop.WinUI.Helpers;
[GeneratedBindableCustomProperty]
public sealed partial class BindableSelectionItem<T>
{
private readonly SelectionItem<T> _selectionItem;
public BindableSelectionItem(SelectionItem<T> selectionItem)
{
_selectionItem = selectionItem;
}
public static implicit operator SelectionItem<T>(BindableSelectionItem<T> bindableSelectionItem) => bindableSelectionItem;
public static implicit operator BindableSelectionItem<T>(SelectionItem<T> selectionItem) => new BindableSelectionItem<T>(selectionItem);
} |
... Yes, that approach would work in
Here's what it would look like: using Nickvision.Desktop.Application;
using WinRT;
namespace Nickvision.Desktop.WinUI.Helpers;
[GeneratedBindableCustomProperty]
public sealed partial class BindableSelectionItem
{
private readonly ISelectionItem _selectionItem;
public BindableSelectionItem(ISelectionItem selectionItem)
{
_selectionItem = selectionItem;
}
public string Label => _selectionItem.Label;
public bool ShouldSelect => _selectionItem.ShouldSelect;
public static implicit operator BindableSelectionItem(SelectionItem<object> item) => new(item);
}
Once you publish a new |
The previous NativeAOT fix introduced
x:DataType="desktopapp:ISelectionItem"compiled bindings for the theme/languageComboBoxes.SelectionItem<T>andISelectionItemfromNickvision.Desktoplack[GeneratedBindableCustomProperty], causing WinUI's binding infrastructure to throwSystem.ArgumentException: 'Value does not fall within the expected range.'atItemsSourceassignment time.[WinRT.GeneratedBindableCustomProperty]cannot be added to external package types — it must be applied to apartialtype declaration that the CsWinRT source generator owns at compile time. The solution is to define a localBindableSelectionItemclass in the WinUI project itself.Changes
Models/BindableSelectionItem.cs: Asealed partial classdecorated with[WinRT.GeneratedBindableCustomProperty], exposing astring Labelproperty. The CsWinRT source generator (included viaMicrosoft.WindowsAppSDK) emits the WinRT vtable at compile time, making it fully NativeAOT-safe for XAML compiled bindings.SettingsPage.xaml: Addedxmlns:modelsnamespace; restoredItemTemplateDataTemplates on bothComboBoxes usingx:DataType="models:BindableSelectionItem"with{x:Bind Label}compiled binding.SettingsPage.xaml.cs:ItemsSourcetoList<BindableSelectionItem>(wrapping labels fromSelectionItem<T>) — NativeAOT-compatible, no WMC1510 warnings.SelectSelectionItem()extension calls inPage_Loadedwith explicit index loops onShouldSelect.SelectedItem as SelectionItem<T>casts inApplyChangesAsyncwithSelectedIndex-based indexing into the controller's original collections.SelectedIndex < 0guard inApplyChangesAsync.usingdirectives (Nickvision.Desktop.Application,Nickvision.Desktop.WinUI.Helpers,Nickvision.Application.Shared.Models).Eliminates
WMC1510trimming warnings on the ComboBoxes —{x:Bind Label}on a[GeneratedBindableCustomProperty]type is fully compiled and AOT-safe.⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.