尝试更改MainPage,但新页面上的WebView无法加载站点。但是当我在应用程序启动时将其设置为主页时成功

Ais*_*iva 6 .net c# android xamarin

我的主页以异步方式发送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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这些是我的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Ais*_*iva 0

看来我得把这笔赏金奖励给自己了……如果可以的话?;)

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

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