晚安,我正在尝试使用HttpWebRequest.BeginGetResponse
方法调用UI线程,在CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
方法内部更新propualty。但它不起作用,我不知道它是如何工作的。有我的代码:
private void DebutReponse(IAsyncResult resultatAsynchrone)
{
HttpWebRequest requete = (HttpWebRequest)resultatAsynchrone.AsyncState;
Stream postStream = requete.EndGetRequestStream(resultatAsynchrone);
string donneesAEnvoyer = "username=" + Name + "&password=" + Password + "&email=" + Email;
string lol = donneesAEnvoyer;
byte[] tableau = Encoding.UTF8.GetBytes(donneesAEnvoyer);
postStream.Write(tableau, 0, donneesAEnvoyer.Length);
postStream.Close();
requete.BeginGetResponse(FinReponse, requete);
}
private void FinReponse(IAsyncResult resultatAsynchrone)
{
HttpWebResponse aResp = null;
try
{
HttpWebRequest requete = (HttpWebRequest)resultatAsynchrone.AsyncState;
WebResponse webResponse = requete.EndGetResponse(resultatAsynchrone);
Stream stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string reponse = streamReader.ReadToEnd();
stream.Close();
streamReader.Close();
webResponse.Close();
//ErrorGridHeight = new GridLength(0.3, GridUnitType.Star);
ErrorMsg = msgSucceed;
}
catch (WebException e)
{
if (e.Response != null)
{
aResp = e.Response as HttpWebResponse;
}
}
if (aResp != null)
await errorFunc();
}
private async Task errorFunc()
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ErrorMsg = msgFailed);
}
我有一个错误告诉我,我只能在await
方法中使用async
运算符,并返回Task
。我不知道它是如何运作的。
编辑: 好的,所以我稍微改变了我的代码以避免这个功能的问题:
requete.BeginGetResponse(FinReponse, requete);
因为她想要一个具有特定原型的功能所以我这样做了:
private async Task lol(IAsyncResult resultatAsynchrone)
{
HttpWebResponse aResp = null;
try
{
HttpWebRequest requete = (HttpWebRequest)resultatAsynchrone.AsyncState;
WebResponse webResponse = requete.EndGetResponse(resultatAsynchrone);
Stream stream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream);
string reponse = streamReader.ReadToEnd();
stream.Close();
streamReader.Close();
webResponse.Close();
//ErrorGridHeight = new GridLength(0.3, GridUnitType.Star);
ErrorMsg = msgSucceed;
}
catch (WebException e)
{
if (e.Response != null)
{
aResp = e.Response as HttpWebResponse;
}
}
if (aResp != null)
await errorFunc();
}
private void FinReponse(IAsyncResult resultatAsynchrone)
{
Task.Run(() => lol(resultatAsynchrone));
}
private async Task errorFunc()
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ErrorMsg = msgFailed);
}
唯一的问题是它不会改变这个属性,它不会进入我的属性集。我有一个例外:
System.NotImplementedExeption
感谢您的帮助。
答案 0 :(得分:0)
尝试将errorFunc()
更改为此。
private void errorFunc()
{
CoreApplication.MainView.CoreWindow.Dispatcher.BeginInvoke(() =>
{
ErrorMsg = msgFailed;
});
}
我还找到了另一个地方,你试图改变ErrorMsg
,并调用UI线程。这是代码ErrorMsg = msgSucceed;
的行。我建议使用相同的方法,所以用这个代替行:
CoreApplication.MainView.CoreWindow.Dispatcher.BeginInvoke(() =>
{
ErrorMsg = msgSucceed;
});
答案 1 :(得分:0)
您必须FinReponse
方法async
才能使用await
运算符:
private async void FinReponse(IAsyncResult resultatAsynchrone)
答案 2 :(得分:0)
所以我找到了访问UI的答案,我必须使用:
Deployment.Current.Dispatcher.BeginInvoke(()=>mym());
它完美无缺。 谢谢你的一切。