我不知道为什么这些代码不起作用。我知道webservice有效,因为在一个控制台项目中,它可以工作(使用同步),但是在这里它没有。这是我的代码:
public interface IService1
{
[OperationContract]
Task<DataTable> GetDataEzra(string command,DataTable tab);
};
public async Task GetDataEzra(string command,DataTable tab)
{
string ConnectionSQL = "";
SqlConnection myConnection = new SqlConnection(ConnectionSQL);
SqlCommand myCommand = new SqlCommand(command, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
SqlDataAdapter sda = new SqlDataAdapter(myCommand);
sda.Fill(tab);
myConnection.Close();
var task = Task.Factory.StartNew(() => tab);
return await task;
}
public Courses()
{
courses = new ObservableCollection();
ListView lstView = new ListView();
DataTable dt = new DataTable();
var legumes = new ArticleGroupModel() { LongName = "Catégorie Légumes", ShortName = "l" };
var fruits = new ArticleGroupModel() { LongName = "Catégorie Fruits", ShortName = "f" };
var task = Task.Factory.StartNew(() => GetResult("GetArticles", dt));
for (int i = 0; i < dt.Rows.Count; i++)
{
fruits.Add(new ArticleModel { Nom = dt.Rows[i][0].ToString(), Type = "Fruit", Image = "tomato.png", Commentaire = "bien frais" });
}
courses.Add(legumes);
courses.Add(fruits);
lstView.ItemsSource = courses;
lstView.IsGroupingEnabled = true;
lstView.GroupDisplayBinding = new Binding("LongName");
lstView.GroupShortNameBinding = new Binding("ShortName");
lstView.ItemTemplate = new DataTemplate(typeof(Article));
Content = new StackLayout
{
Children = {
lstView
}
};
}
async static Task GetResult(string command, DataTable tab)
{
Service1Client MyClient = new Service1Client();
var task = Task.Factory.StartNew(() => MyClient.GetDataEzraAsync(command,tab));
await Task.WhenAny(task, Task.Delay(3500));
}
你知道吗?我是Xamarin的初学者......编译中没有错误但是就像webservice没有调用一样。
谢谢。