Xamarin.forms-使用WebView显示内容

时间:2018-07-25 08:44:31

标签: c# webview xamarin.forms html-agility-pack

我正在创建现有网站的移动应用程序。我使用HTMLAGILITYPACK从网站上抓取了一些数据。我想在我的应用程序中显示它们。但是没有结果,什么也没显示。

这是我的带有“ scraper”的应用代码:

namespace Apka
{
public partial class App : Application
{
    public static string DocumentPath = 
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    public static string strona = "";


    public static NavigationPage NavigationPage { get; private set; }
    WebRequest request = HttpWebRequest.Create("www.wiocha.pl");
    WebResponse response;

    public App()
    {


        InitializeComponent();

        starthttp();





        NavigationPage = new NavigationPage(new MainPage());
        RootPage rootPage = new RootPage();
        MenuPage menuPage = new MenuPage(rootPage.vm);

        rootPage.Master = menuPage;
        rootPage.Detail = NavigationPage;
        MainPage = rootPage;

    }


    private async void starthttp()
    {

        response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);

        var html = new HtmlDocument();
        html.Load(response.GetResponseStream());
        var nodes = html.DocumentNode.Descendants("img")
            .Where(node => node.GetAttributeValue("class", "")
               .Equals("imageitself")).ToList();





        foreach (var node in nodes)
        {

            strona = strona + node.OuterHtml;

        }

        System.Diagnostics.Debug.WriteLine(strona);




    }
}

} 在控制台中,它以html形式返回一些img,例如img src =“ link” class =“ imageitself” alt =“ blablabla”

这是我要在其中放置刮擦图片的页面:

namespace Apka.View.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {



        public MainPage()
        {
            InitializeComponent();

            WebView webView = new WebView
            {
                Source = new HtmlWebViewSource
                {
                    Html = @"<!DOCTYPE html><html><head><style>img { width:100%; }</style></head><body>" + App.strona + @"</body></html>",
                },
                VerticalOptions = LayoutOptions.FillAndExpand

            };



            /* ViewModel.Pages.MainPageViewModel vm = new ViewModel.Pages.MainPageViewModel();
             this.BindingContext = vm;*/
        }





    }
}

此页面的XAML代码如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="" (cant place links)
         xmlns:x="" (here too)
         x:Class="Apka.View.Pages.MainPage">


<ContentPage.ToolbarItems>
    <ToolbarItem Command="{Binding MenuItem1Command}" />
</ContentPage.ToolbarItems>
<ContentPage.Content>

    <StackLayout BackgroundColor="#505050" VerticalOptions="FillAndExpand" 
HorizontalOptions="FillAndExpand">



    </StackLayout>

</ContentPage.Content>
</ContentPage>

为什么Webview不起作用?我应该怎么显示这些图片?

2 个答案:

答案 0 :(得分:0)

您正在使用以下代码创建select incidentid count(VehicleAllocationSequenceNumber) as num_assigned, count(VehicleArrivalAtSceneDateTime ) as num_oncscene from AmbulanceLinkedDatasetForModelling group by incidentid

WebView

无论如何,您无法在页面上添加 [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); WebView webView = new WebView { // elided }; /* ViewModel.Pages.MainPageViewModel vm = new ViewModel.Pages.MainPageViewModel(); this.BindingContext = vm;*/ } 。如果您真的想通过代码创建WebView,则必须为WebView

指定一个名称
StackLayout

,然后从<StackLayout x:Name="StackLayout" ...> 构造函数中添加WebView

MainPage

无论如何,没有什么可以阻止您从XAML创建他StackLayout.Children.Add(webView);

WebView

然后在

后面的代码中简单地设置源
<StackLayout BackgroundColor="#505050" 
             VerticalOptions="FillAndExpand"  
             HorizontalOptions="FillAndExpand">
    <WebView x:Name="WebView" VerticalOptions="FillAndExpand />
</StackLayout>

答案 1 :(得分:0)

在声明WebView的页面上,当将其添加到堆栈布局中时,需要指定要显示的WebView的WidthRequest和HeightRequest,在这种情况下,添加到GridLayout时不会发生

namespace Apka.View.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{



    public MainPage()
    {
        InitializeComponent();

        WebView webView = new WebView
        {
            Source = new HtmlWebViewSource
            {
                Html = @"<!DOCTYPE html><html><head><style>img { width:100%; }</style></head><body>" + App.strona + @"</body></html>",
            },
            VerticalOptions = LayoutOptions.FillAndExpand,
            WidthRequest = 1000,
            HeightRequest=1000

        };



        /* ViewModel.Pages.MainPageViewModel vm = new ViewModel.Pages.MainPageViewModel();
         this.BindingContext = vm;*/
    }
}
}