Note: If there's someone more knoweledgeable who can tell me why this isn't good, please do.
After some time trying to figure this out, I realized that the *.g.cs files are generated from my XAML. The warning shows up for events bound to methods in my ViewModel that are not synchronous(?)
Example PageExample.xaml.g.cs:
case 15: // Views\PageExample.xaml line 72
this.obj15 = (global::Windows.UI.Xaml.Controls.ToggleMenuFlyoutItem)target;
this.obj15Click = (global::System.Object p0, global::Windows.UI.Xaml.RoutedEventArgs p1) =>
{
//Warning CS4014 because of line below.
//I can add await before this line all I want,
//but the file gets regenerated anyway.
this.dataRoot.ViewModel.Refresh();
};
((global::Windows.UI.Xaml.Controls.ToggleMenuFlyoutItem)target).Click += obj15Click;
this.bindingsTracking.RegisterTwoWayListener_15(this.obj15);
break;
Example XAML PageExample.xaml:
<ToggleMenuFlyoutItem Text="Toggle" Click="{x:Bind ViewModel.Refresh}" />
Example ViewModel.cs:
//Warning CS4014 on the .g.cs file because this is async
public async Task Refresh()
{
//code you actually want
}
I've tried just changing to async void
on the Refresh
method but it seems to have an effect that caused timing issues in my case.
This is what seems to work. Ugly, but seems to address the warning.
ViewModel.cs:
//No more warning CS4014 since it's async void
public async void Refresh()
{
await RefreshAsync();
}
public async Task RefreshAsync()
{
//code you actually want
}
Per Jon's comment, I suppose, this reasonable:
//no need to be async void
public void Refresh()
{
//discard
_ = RefreshAsync();
}