Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对于Xamarin.Forms应用程序,异步和使用wait Task.Run(()=>;运行后端数据库更新之间有什么区别?_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 对于Xamarin.Forms应用程序,异步和使用wait Task.Run(()=>;运行后端数据库更新之间有什么区别?

C# 对于Xamarin.Forms应用程序,异步和使用wait Task.Run(()=>;运行后端数据库更新之间有什么区别?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我使用的是Xamarin.Forms,所以这个问题特别适用于我使用该应用程序时,因为它可能不同于普通的C#应用程序 当按下按钮时,我使用TapGestureRecognitor调用OnTapped方法。这会更新页面上的按钮颜色,同时更新数据库 要异步执行此操作,我希望使用: 以下是实际代码: async void OnTapped(object sender, System.EventArgs e) { var btn = sender as ButtonTemplate;     i

我使用的是Xamarin.Forms,所以这个问题特别适用于我使用该应用程序时,因为它可能不同于普通的C#应用程序

当按下按钮时,我使用TapGestureRecognitor调用OnTapped方法。这会更新页面上的按钮颜色,同时更新数据库

要异步执行此操作,我希望使用:

以下是实际代码:

async void OnTapped(object sender, System.EventArgs e)
{
    var btn = sender as ButtonTemplate;
    if (Counts.phaseTableSelectedCardCount != 0)
    {
        var canContinue = await this.DisplayAlert("Selector", "Changing this will remove all previously selected cards from the deck", "OK", "Cancel");
        if (canContinue == false)
            return;
     }
     var settingId = SetButtons(btn.Text);
     await Task.Run(() => AddDetailSection(settingId));
 }
    
 public void AddDetailSection(int settingId)
 {
     vm.IsBusy = true;
     App.DB.UpdateData();
     vm.IsBusy = false;
 }

我这样做是否有任何可能的Xamarin/线程问题,或者是否有更好的方法将AddDetailSection标记为异步方法,如果有,如何才能做得更好?

更好的方法是使用
TaskCompletionSource

任务中删除wait。运行
并在等待时使用它
tcs.Task


最好创建异步版本的方法
AddDetailSection

您的
App.DB
是否公开
UpdateData的异步版本?
async void OnTapped(object sender, System.EventArgs e)
{
    var btn = sender as ButtonTemplate;
    if (Counts.phaseTableSelectedCardCount != 0)
    {
        var canContinue = await this.DisplayAlert("Selector", "Changing this will remove all previously selected cards from the deck", "OK", "Cancel");
        if (canContinue == false)
            return;
     }
     var settingId = SetButtons(btn.Text);
     await Task.Run(() => AddDetailSection(settingId));
 }
    
 public void AddDetailSection(int settingId)
 {
     vm.IsBusy = true;
     App.DB.UpdateData();
     vm.IsBusy = false;
 }