Skip to content

Commit fb35f21

Browse files
author
Mauro Bertoli
authored
[WinformsDemo] fix windows forms demo2 DataBinding incompatable data type (#532)
* upgrade to .net core 6 and latest c# functionality, aligned project namespace to folder name, make VS Designer available for UserControl * new winforms demo with unit tests * fix windows forms demo2 incompatable data type Replace IEnumerable with IList to avoid exception "Complex DataBinding accepts as a data source either an IList or an IListSource" * ApplicationHighDpiMode PerMonitorV2
1 parent 23b2b39 commit fb35f21

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

winforms/demo2/WinForms.Reactive.Client/ViewModels/ItemsViewModel.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ItemsViewModel : ReactiveObject, IRoutableViewModel
4747
public IScreen HostScreen { get; protected set; } = null!;
4848

4949
// Commands
50-
public ReactiveCommand<Unit, IEnumerable<ItemDto>> LoadItemsCommand { get; }
50+
public ReactiveCommand<Unit, IList<ItemDto>> LoadItemsCommand { get; }
5151
public ReactiveCommand<Unit, Unit> ShowDetailsCommand { get; }
5252

5353
// Input
@@ -56,9 +56,9 @@ public class ItemsViewModel : ReactiveObject, IRoutableViewModel
5656
[Reactive] public (Guid? tagId, int index) SelectedItem { get; set; }
5757

5858
// Output - OAPH must be initialized via `initialValue` parameter in .ToPropertyEx(...)
59-
[ObservableAsProperty] public IEnumerable<ItemDto> Items { get; }
60-
[ObservableAsProperty] public IEnumerable<ItemDto> ItemsFiltered { get; }
61-
[ObservableAsProperty] public IEnumerable<ItemTagDto> SelectedItemTags { get; }
59+
[ObservableAsProperty] public IList<ItemDto> Items { get; }
60+
[ObservableAsProperty] public IList<ItemDto> ItemsFiltered { get; }
61+
[ObservableAsProperty] public IList<ItemTagDto> SelectedItemTags { get; }
6262
[ObservableAsProperty] public bool IsLoading { get; }
6363
[ObservableAsProperty] public bool HasItems { get; }
6464
[ObservableAsProperty] public bool HasItemSelection { get; }
@@ -85,7 +85,7 @@ public ItemsViewModel(
8585
//LoadItemsCommand.ThrownExceptions.Subscribe(error => { /* Handle errors here */ });
8686
LoadItemsCommand
8787
.ObserveOn(mainThreadScheduler)
88-
.ToPropertyEx(this, x => x.Items, initialValue: Enumerable.Empty<ItemDto>());
88+
.ToPropertyEx(this, x => x.Items, initialValue: new List<ItemDto>());
8989

9090
var interval = TimeSpan.FromMinutes(5);
9191
Observable.Timer(interval, interval, mainThreadScheduler)
@@ -124,9 +124,9 @@ public ItemsViewModel(
124124
.ToList();
125125
return r;
126126
})
127-
.Catch(Observable.Return(Enumerable.Empty<ItemDto>()))
127+
.Catch(Observable.Return(Enumerable.Empty<ItemDto>().ToList()))
128128
.ObserveOn(mainThreadScheduler)
129-
.ToPropertyEx(this, x => x.ItemsFiltered, initialValue: Enumerable.Empty<ItemDto>());
129+
.ToPropertyEx(this, x => x.ItemsFiltered, initialValue: new List<ItemDto>());
130130

131131
this
132132
.WhenAnyValue(x => x.Items)
@@ -149,7 +149,7 @@ public ItemsViewModel(
149149
.Select(x => x.tagId!.Value)
150150
.SelectMany(FetchDetailAsync)
151151
.ObserveOn(mainThreadScheduler)
152-
.ToPropertyEx(this, x => x.SelectedItemTags, initialValue: Enumerable.Empty<ItemTagDto>());
152+
.ToPropertyEx(this, x => x.SelectedItemTags, initialValue: new List<ItemTagDto>());
153153
}
154154

155155
private async Task<Unit> ShowDetails(CancellationToken token)
@@ -172,13 +172,13 @@ private async Task<Unit> ShowDetails(CancellationToken token)
172172
return await Task.FromResult(Unit.Default);
173173
}
174174

175-
private async Task<IEnumerable<ItemDto>> LoadItems()
175+
private async Task<IList<ItemDto>> LoadItems()
176176
{
177177
var items = await _itemsService.GetAll();
178-
return items;
178+
return items.ToList();
179179
}
180180

181-
private async Task<IEnumerable<ItemTagDto>> FetchDetailAsync(Guid id, CancellationToken token)
181+
private async Task<IList<ItemTagDto>> FetchDetailAsync(Guid id, CancellationToken token)
182182
{
183183
var tags = this.Items.Where(x => x.ItemId == id).SelectMany(x => x.Tags).ToList();
184184
return await Task.FromResult(tags);

winforms/demo2/WinForms.Reactive.Client/WinForms.Reactive.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<Nullable>enable</Nullable>
77
<UseWindowsForms>true</UseWindowsForms>
88
<ImplicitUsings>enable</ImplicitUsings>
9+
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
910
</PropertyGroup>
1011

1112
<ItemGroup>

0 commit comments

Comments
 (0)