【问题标题】:MyCustomPage.xaml is not loading webView in xamarin.Forms portableMyCustomPage.xaml 未在 xamarin.Forms 可移植中加载 webView
【发布时间】:2017-02-07 09:42:38
【问题描述】:

我是 xamarin.forms 便携式的初学者,我正在研究 xamainr.forms 便携式项目,我遇到了问题。我的便携式项目中有一个 .xaml 页面,我正在使用以下代码行从 App.cs 导航到这个 .xaml 页面。

var ep = new CustomWebViewPage(dbPath);
var MainEv = new NavigationPage(ep);

在 CustomWebViewPage 中,我以以下方式使用 WebView 来加载 Url,但在成功执行上述行后,模拟器不会加载 webView。我不知道为什么会这样。 我正在粘贴 CustomWebViewPage 的代码,如下所示。

CustomWebViewPage.xaml.cs

using XamarinDbokReader.SQLite_AppSample;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.IO;

namespace MyDummyProject
{
    public partial class CustomWebViewPage : ContentPage
    {
        public string dbPath = "";
        public CustomWebViewPage(string folderPath)
        {
            this.dbPath = folderPath;
            HttpHelperClass helperClass = new HttpHelperClass();
            InitializeComponent();
            var webView = new WebView();
            UrlWebViewSource urlWebViewSource = new UrlWebViewSource()
            {
                Url = UrlsList.FindUrl("ProEportalLoginPage") + UrlsList.ApiKeys(AppMode.ProductionMode.ToString())
            };
               webView.Source = urlWebViewSource;
               webView.Navigated += (s, e) =>
               {
                   if (e.Url.StartsWith("http://userInfo/?"))
                   {
                       string token = "";
                       try
                       {
                          string value_string = Uri.UnescapeDataString(e.Url.ToString());
                          token = value_string.Split('=')[1];
                          if (!string.IsNullOrEmpty(token))
                          {
                              string path = Path.Combine(dbPath.ToString(), "dBookStore.db3");
                              helperClass.SaveUserInformation(token, path);
                          }
                       }
                       catch (Exception ss)
                       {
                       }
                  }
            };
            wvEportalPage = webView;
        }
        public CustomWebViewPage()
        {

        }
    }
}

CustomWebViewPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XamarinDbokReader.EportalPage">
    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    <WebView x:Name="wvEportalPage"></WebView>
</ContentPage>

App.cs

public App(bool isSqliteDbExist, string sPath)
    {
        isDbExist = isSqliteDbExist;
        dbPath = sPath;
        if (isDbExist)
        {
            if (isLoggedIn)
            {
                NavigationPage navPage = new NavigationPage(new BooksView());
                App.Current.MainPage = navPage;
            }
            else
            {
                var tdlx = new CustomWebViewPage(dbPath);
                var MainNave = new NavigationPage(tdlx);
            }
        }
        else
        {
            //When cursor is coming from MainActivity then following line executes. And then OnStart() method executes.
            ssd.CreateTablesInDb();
            isDbExist = true;
        }
    }

 protected override void OnStart()
    {
        if (isDbExist)
        {
            if (isLoggedIn)
            {
                NavigationPage navPage = new NavigationPage(new BooksView());
                App.Current.MainPage = navPage;
            }
            else
            {
                var ep = new CustomWebViewPage(dbPath);
                var MainEv = new NavigationPage(ep);

            }
        }
    }

MainActivity

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var bookCoverFolder = Path.Combine(documents, "BooksCover");
        var booksCollection = Path.Combine(documents, "Books");
        var bookResource = Path.Combine(documents, "Resource");
        if(!Directory.Exists(bookCoverFolder))
            Directory.CreateDirectory(bookCoverFolder);
        if (!Directory.Exists(booksCollection))
            Directory.CreateDirectory(booksCollection);
        if(!Directory.Exists(bookResource))
            Directory.CreateDirectory(bookResource);
        SQLite_Android androidDb = new SQLite_Android();
        if (androidDb.IsExist())
        {
            LoadApplication(new App(true, androidDb.dbStorePath));
        }
        else
        {
            LoadApplication(new App(false, androidDb.dbStorePath));

        }
    }

【问题讨论】:

  • 这段代码甚至无法编译。你的类名是CustomWebViewPage,构造函数是EportalPage
  • 是的 Stephane,你是对的,我已经正确重命名了。

标签: xaml xamarin webview xamarin.android xamarin.forms


【解决方案1】:

WebView 可能没有得到任何WidthHeight

尝试将VerticalOptionsHorizontalOptions 属性设置为FillAndExpand

像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XamarinDbokReader.EportalPage">
    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
    <WebView x:Name="wvEportalPage" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"></WebView>
</ContentPage>

如果这似乎不起作用,请尝试将其包装在 Grid 中。

【讨论】:

  • 应用网格后,我可以看到页面导航到 CustomWebViewPage 页面但黑屏。我找不到我触发的 url 页面。
  • 我看到您正在尝试做一些自定义 URL 或其他什么?请尝试使用 google.com 之类的微不足道的东西。这说明了什么?
  • 我试过google.co.in 但它没有加载,我仍然可以看到黑屏。
  • 我在 OnStart() 事件上从 App.cs 加载此页面。有什么问题吗?
  • 请用您现在拥有的内容更新问题中的代码。包括您用于导航到页面的代码。
【解决方案2】:

感谢您的建议,但我在代码中发现了我的错误。我在 App.cs 中犯了一个非常小的错误设置导航页面后,我没有将 NavigationPage 设置为 MainPage。它应该如下所示。

var tdlx = new CustomWebViewPage(dbPath);
var MainNave = new NavigationPage(tdlx);
MainPage = MainNave;

效果很好。我知道 MainPage,但由于其他一些地区,我没有写,但最终它正在工作。

【讨论】:

    【解决方案3】:

    您必须为 Xamarin WebView 定义宽度和高度。

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多