我正在使用Azure移动服务创建一个Xamarin.Forms应用程序。目前,我正在做这样的事情: -
App.cs
public const string applicationURL = @"myUrl";
public const string applicationKey = @"myKey";
public static MobileServiceClient client = new MobileServiceClient(applicationURL, applicationKey);
MainPage.cs
public MainViewModel ViewModel
{
get { return this.BindingContext as MainViewModel ; }
set { this.BindingContext = value; }
}
public MainPage()
{
InitializeComponent();
this.ViewModel = new MainViewModel (App.client);}
MainViewModel.cs
public MainViewModel(MobileServiceClient client) : this()
{
this.client = client;
LoadItemsCommand.Execute(null);
}
public ObservableCollection<MainModel> mainDetails{ get; set; }
private Command loadItemsCommand;
/// <summary>
/// Command to load/refresh items
/// </summary>
public Command LoadItemsCommand
{
get { return loadItemsCommand ?? (loadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand())); }
}
private async Task ExecuteLoadItemsCommand()
{
if (IsLoading)
return;
IsLoading = true;
try
{
var details= await client.GetTable<MainModel>().ToListAsync();
MainDetails.Clear();
foreach (var mDetails in details)
{
MainDetails.Add(mDetails);
}
}
catch (Exception ex)
{
var page = new ContentPage();
var result = page.DisplayAlert("Error", "Error loading data. Please check connectivity and try again.", "OK", null);
}
IsLoading = false;
}
当我的调试器到达MainviewModel.cs中的Try块时,它会突然退出而不会执行任何操作。它甚至没有进入Catch块。有什么我做错了吗?我怎样才能找出导致问题的原因?
答案 0 :(得分:2)
当我的调试器到达MainviewModel.cs中的Try块时,它突然退出而没有任何操作。
由于异步方法的性质,一旦命中第一个await
,它们就会将控制权交还给调用方法。这是预期的行为。只需在等待之后在行上设置一个断点,一旦异步操作完成,你就会看到它到达它。