windows Phone8.1:UI 由于异步等待,未显示下一页
windows Phone8.1:UI not showing next page because of async await
我想导航到下一页,但问题是我正在使用 async
和等待方法。
所以在后台 frame.navigate(typeof(nextpage))
将我导航到下一页,但 UI 线程不导航并且同一页面保留在屏幕上,但我可以在控制台中看到 frame.navigate
正在工作,因为我正在传递参数。
下面是我的代码:
public async void ContinueWithWebAuthenticationBroker(WebAuthenticationBrokerContinuationEventArgs args)
{
string[] abc = new string[2];;
System.Threading.CancellationTokenSource cts;
cts = new System.Threading.CancellationTokenSource();
string[] abc;
ToastPrompt toast = new ToastPrompt();
toast.Title = "Loading";
toast.Message = "Please Wait";
toast.Show();
using (HttpClient client = new HttpClient())
{
var response = client.GetAsync(new Uri("http://localhost:8080/impulse/total.jsp"));
string result = await responsefb.Content.ReadAsStringAsync();
try
{
Windows.Data.Json.JsonObject root = Windows.Data.Json.JsonValue.Parse(result).GetObject();
if (root["result"].GetString() == "true")
{
abc[0] = root["result"].GetString();
abc[1] = root["comment"].GetString();
Frame.Navigate(typeof(BasicPage1), abc);
System.Diagnostics.Debug.WriteLine("In IF");
}
else if(root["result"].GetString() == "false")
{
System.Diagnostics.Debug.WriteLine(root["result"].GetString());
abc[0] = root["comment"].GetString();
System.Diagnostics.Debug.WriteLine(abc);
toast.Hide();
cts.Cancel();
Frame.Navigate(typeof(signin),"hello");
}
else
{
}
}
catch (Exception ex1)
{
System.Diagnostics.Debug.WriteLine("string is empty", ex1);
}
}
我的下一页数据
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// base.OnNavigatedTo(e);
var lastPage = Frame.BackStack.Last().SourcePageType;
System.Diagnostics.Debug.WriteLine(lastPage);
if (lastPage.ToString() == "App13.fb")
{
try
{
var a = e.Parameter.ToString();
System.Diagnostics.Debug.WriteLine(a);
}
catch (Exception Ex)
{
System.Diagnostics.Debug.WriteLine(Ex.ToString());
}
}
else
{
}
}
运行 UI 线程中的页面导航代码
var dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame.Navigate(typeof(BasicPage1), abc);
});
我想导航到下一页,但问题是我正在使用 async
和等待方法。
所以在后台 frame.navigate(typeof(nextpage))
将我导航到下一页,但 UI 线程不导航并且同一页面保留在屏幕上,但我可以在控制台中看到 frame.navigate
正在工作,因为我正在传递参数。
下面是我的代码:
public async void ContinueWithWebAuthenticationBroker(WebAuthenticationBrokerContinuationEventArgs args)
{
string[] abc = new string[2];;
System.Threading.CancellationTokenSource cts;
cts = new System.Threading.CancellationTokenSource();
string[] abc;
ToastPrompt toast = new ToastPrompt();
toast.Title = "Loading";
toast.Message = "Please Wait";
toast.Show();
using (HttpClient client = new HttpClient())
{
var response = client.GetAsync(new Uri("http://localhost:8080/impulse/total.jsp"));
string result = await responsefb.Content.ReadAsStringAsync();
try
{
Windows.Data.Json.JsonObject root = Windows.Data.Json.JsonValue.Parse(result).GetObject();
if (root["result"].GetString() == "true")
{
abc[0] = root["result"].GetString();
abc[1] = root["comment"].GetString();
Frame.Navigate(typeof(BasicPage1), abc);
System.Diagnostics.Debug.WriteLine("In IF");
}
else if(root["result"].GetString() == "false")
{
System.Diagnostics.Debug.WriteLine(root["result"].GetString());
abc[0] = root["comment"].GetString();
System.Diagnostics.Debug.WriteLine(abc);
toast.Hide();
cts.Cancel();
Frame.Navigate(typeof(signin),"hello");
}
else
{
}
}
catch (Exception ex1)
{
System.Diagnostics.Debug.WriteLine("string is empty", ex1);
}
}
我的下一页数据
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// base.OnNavigatedTo(e);
var lastPage = Frame.BackStack.Last().SourcePageType;
System.Diagnostics.Debug.WriteLine(lastPage);
if (lastPage.ToString() == "App13.fb")
{
try
{
var a = e.Parameter.ToString();
System.Diagnostics.Debug.WriteLine(a);
}
catch (Exception Ex)
{
System.Diagnostics.Debug.WriteLine(Ex.ToString());
}
}
else
{
}
}
运行 UI 线程中的页面导航代码
var dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame.Navigate(typeof(BasicPage1), abc);
});