我正在創建一個現有網站的移動應用程序。我使用HTMLAGILITYPACK來廢棄網站上的一些數據。我想在我的應用中顯示它們。但沒有結果,也沒有任何顯示。
這是我的應用程序代碼“刮刀”:
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);
}
}
在控制台中,它返回一些img in html,如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不起作用?我該怎麼做才能顯示這些照片?
您正在使用以下代碼創建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" ...>
然後從MainPage
構造函數中添加WebView
。
StackLayout.Children.Add(webView);
無論如何,沒有什麼能阻止你從XAML創建WebView
<StackLayout BackgroundColor="#505050"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<WebView x:Name="WebView" VerticalOptions="FillAndExpand />
</StackLayout>
然後簡單地在後面的代碼中設置源代碼
public MainPage()
{
InitializeComponent();
WebView.Source = new HtmlWebViewSource
{
Html = @"<!DOCTYPE html><html><head><style>img { width:100%; }</style></head><body>" + App.strona + @"</body></html>",
};
}
在聲明WebView的頁面上,何時添加到stacklayout中需要指定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;*/
}
}
}