尝试更改主页,但新页面上的 Web 视图无法加载网站.但是当我在应用程序启动时将其设置为主页时,它是成功的



我的主页以异步方式发送API请求,然后将主页设置为其他页面,如下所示:

async private void GetContacts()
{
try
{
activityIndicator.IsVisible = true;
activityIndicator.IsRunning = true;
var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();
var contactsWithPhone = contacts != null && contacts.Count > 0 ?
contacts.Where(c => c.Number != null && c.Number.Trim().Length > 0) : contacts;
if (contactsWithPhone.Count() > 0)
{
Application.Current.Properties["FirstTime"] = false;
activityIndicator.IsVisible = false;
activityIndicator.IsRunning = false;
List<NewsletterSubscriber> subscribers = new List<NewsletterSubscriber>();
foreach (Plugin.ContactService.Shared.Contact contact in contactsWithPhone)
{
subscribers.Add(new NewsletterSubscriber() { Name = contact.Name != null && contact.Name.Trim().Length > 0 ? contact.Name : "Unknown", Phone = contact.Number });
}
SendContacts(subscribers);
}
Application.Current.MainPage = new MainPage();
}
catch(Exception)
{ throw; }
}
async void SendContacts(List<NewsletterSubscriber> subscribers)
{
var httpClient = new HttpClient();
NewsletterSubscriberRoot newsletterSubscriberRoot = new NewsletterSubscriberRoot();
newsletterSubscriberRoot.newsletterSubscribers = subscribers.OrderBy(c => c.Name).ToList();
var content = newsletterSubscriberRoot.AsJson();
try
{
var result = await httpClient.PostAsync(url, content);
}
catch (Exception)
{
throw;
}
}

这些是我的 WebView 事件:

private void MainPage_Appearing(object sender, EventArgs e)
{
webView.Navigated += WebView_Navigated;
webView.Navigating += WebView_Navigating;
webView.Source = "https://appsoln.com";
}
private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
activityIndicator.IsRunning = true;
activityIndicator.IsVisible = true;
webView.IsVisible = false;
}
protected override bool OnBackButtonPressed()
{
if (webView.CanGoBack)
{
webView.GoBack();
return true ;
}
base.OnBackButtonPressed();
return false;
}
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
{
if (e.Result == WebNavigationResult.Success)
{
activityIndicator.IsRunning = false;
activityIndicator.IsVisible = false;
webView.IsVisible = true;
}
else
{
DisplayAlert("No connection (ER:CON2)", "Please check your Internet Connection.", "OK");
var closer = DependencyService.Get<ICloseApplication>();
closer?.closeApplication();
}
}

现在的问题是,当包含WebView的页面在应用程序启动时设置为主页时,它打开网站正常。但是当它从另一个主页设置时,它会进入导航事件的失败部分。我在输出窗口中没有看到任何异常。

看来我必须把这个赏金奖励给自己......如果可以的话?;)

我没有这个问题的确切原因。如果有人能告诉我,那就太好了。

我为解决这个问题所做的是我删除了else {...}事件的一部分WebView_Navigated。也许 WebView 在失败后再次尝试,我通过关闭应用程序在其他部分停止它。该页面需要一些时间才能首次加载,但最终会打开。

最新更新