我有一个从我编写的Web服务运行的简单插入查询。单击按钮,我让它运行一个调用Web服务并执行插入查询的方法。出于某种原因,在我退出点击事件之前,它实际上并没有插入信息......任何人都可以告诉我为什么?我将在下面发布代码:
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
//inserts new article into DB
Insert();
}
private void InsertNewArticle()
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
ArticleDetails articleInfo = new ArticleDetails();
articleInfo.Title = newsTitle;
articleInfo.Body = newsBody;
articleInfo.Author = newsAuthor;
service.InsertArticleDetailsAsync(articleInfo);
}
我通过调试看到的是,它在退出btnSubmit_Click
事件之前不会执行。这对我来说是个问题的原因是我想要在退出点击事件之前对刚刚提交的信息做些什么。
注意:这是通用Windows应用程序
答案 0 :(得分:1)
您正在调用异步方法,但不会等待它执行。将InsertNewArticle方法更改为
private async void InsertNewArticle()
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
ArticleDetails articleInfo = new ArticleDetails();
articleInfo.Title = newsTitle;
articleInfo.Body = newsBody;
articleInfo.Author = newsAuthor;
await service.InsertArticleDetailsAsync(articleInfo);
}