MVVM-Samples
MVVM-Samples copied to clipboard
Load Async data in a UserControl with SetPropertyAndNotifyOnCompletion instead of NotifyTaskCompletion
Hello,
I'm currently modifying a UserControl to remove NotifyTaskCompletion class and use directly the Task with SetPropertyAndNotifyOnCompletion .
I am working on a .NET 4.8 project in WPF and I have the following problem: Constructor
public ArticleCategoryUserControlModel()
{
_serviceProvider = ApplicationBootstrapper.ServiceProvider;
AllArticleCategoriesTask = RequestValuesAsync();
}
private async Task<ObservableCollection<IArticleCategoryBusinessModel>> RequestValuesAsync()
{
using var scope = _serviceProvider.CreateScope();
var articleCategoryReader = scope.ServiceProvider.GetRequiredService<IArticleCategoryReader>();
await Task.Delay(3000); // for debug purpose
var res = await articleCategoryReader.GetAllAsync();
return new ObservableCollection<IArticleCategoryBusinessModel>(res);
}
Currently I am still using the following result wrapper:
public ObservableCollection<IArticleCategoryBusinessModel> AllArticleCategoriesResult =>
AllArticleCategoriesTask.Status == TaskStatus.RanToCompletion
? AllArticleCategoriesTask.Result
: null;
Corresponding Xaml
<Grid Visibility="{Binding AllArticleCategoriesTask.IsCompleted, Converter={StaticResource BooleanToVisibilityFbConverter}}"
Background="Pink">
<dxe:ComboBoxEdit ItemsSource="{Binding AllArticleCategoriesResult}">
<dxe:ComboBoxEdit.StyleSettings>
<dxe:TokenComboBoxStyleSettings />
</dxe:ComboBoxEdit.StyleSettings>
</dxe:ComboBoxEdit>
</Grid>
My question is the following: Is it possible to do the same behavior without the AllArticleCategoriesResult property?
In the Xaml, I can't directly use AllArticleCategories.Result because it causes me to freeze the UI. I have to use the AllArticleCategoriesResult property.
Thanks