❔ Load async data and display it (WindowsAppSDK / WinUI3)

I am currently developing my first windows app with C#, Windows App SDK and WinUI3. I want to do something very basic. Load a list of items and display it in the UI. My problem is, that the method to load the data has to be executed asynchronously because it would block the UI thread. I didn't find any working information about how to do that. Like, it should be a simple solution right? Can anyone help me with that?
4 Replies
Lexaro
Lexaro2y ago
private async void LoadDataAsync()
{
var data = await LoadData();
MyListView.ItemsSource = data;
}

private async Task<List<string>> LoadData()
{
return await Task.Run(() =>
{
List<string> items = new List<string> { "Item 1", "Item 2", "Item 3" };
return items;
});
}
private async void LoadDataAsync()
{
var data = await LoadData();
MyListView.ItemsSource = data;
}

private async Task<List<string>> LoadData()
{
return await Task.Run(() =>
{
List<string> items = new List<string> { "Item 1", "Item 2", "Item 3" };
return items;
});
}
Just like that
Feuerhamster
FeuerhamsterOP2y ago
Doesn't work. The UI is not responding for a moment... Here is my code
public sealed partial class PlaybackDevices : Page
{
public ObservableCollection<AudioControlModel> Devices = new ObservableCollection<AudioControlModel>();

public PlaybackDevices()
{
this.InitializeComponent();
this.LoadDevices();
}

private async void LoadDevices()
{
IEnumerable<CoreAudioDevice> devices = await App.CoreAudioController.GetPlaybackDevicesAsync();

devices = devices.Where(device => device.State != DeviceState.Active).ToList();

foreach (CoreAudioDevice device in devices)
{
Devices.Add(new AudioControlModel(device.InterfaceName, device.Id, DeviceIcon.Digital, device.Volume));
}
}
}
public sealed partial class PlaybackDevices : Page
{
public ObservableCollection<AudioControlModel> Devices = new ObservableCollection<AudioControlModel>();

public PlaybackDevices()
{
this.InitializeComponent();
this.LoadDevices();
}

private async void LoadDevices()
{
IEnumerable<CoreAudioDevice> devices = await App.CoreAudioController.GetPlaybackDevicesAsync();

devices = devices.Where(device => device.State != DeviceState.Active).ToList();

foreach (CoreAudioDevice device in devices)
{
Devices.Add(new AudioControlModel(device.InterfaceName, device.Id, DeviceIcon.Digital, device.Volume));
}
}
}
And another thing... My app is crashing if I try to use this in xaml
<ScrollViewer x:Name="ScrollView" Padding="16">
<ItemsControl ItemsSource="{x:Bind Devices}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Vertical" Spacing="16" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:AudioControl
Name="{Binding Name}"
Icon="{Binding Icon}"
Id="{Binding Id}"
Volume="{Binding Volume}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
<ScrollViewer x:Name="ScrollView" Padding="16">
<ItemsControl ItemsSource="{x:Bind Devices}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Vertical" Spacing="16" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:AudioControl
Name="{Binding Name}"
Icon="{Binding Icon}"
Id="{Binding Id}"
Volume="{Binding Volume}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
HimmDawg
HimmDawg2y ago
I don't know too much about WinUI3, but you can use the Page.Loaded-Event to at least have an async method, that allows you to call await LoadDevices() Btw LoadDevices should return a Task So that could look like
private async void PlaybackDevices_Loaded(object sender, RoutedEventArgs e)
{
await LoadDevices();
}
private async void PlaybackDevices_Loaded(object sender, RoutedEventArgs e)
{
await LoadDevices();
}
you just need to subscribe to that event somewhere The way you were calling LoadDevices wont work because it'll start executing the method and then, the program will forget about it. It wont wait for any results or changes this method might make because you are not awaiting it. Trying to put await before the call in the constructor will also not work because constructors cannot be async.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server