Ich erstelle eine mobile App der bestehenden Website. Ich verwende HTMLAGILITYPACK, um einige Daten von der Website zu verschrotten. Ich möchte sie in meiner App anzeigen. Aber es gibt kein Ergebnis und nichts wird angezeigt.
Das ist mein App-Code mit "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);
}
}
} In der Konsole gibt es einige img in HTML zurück wie img src = "link" class = "imageitself" alt = "blablabla"
Hier ist meine Seite wo ich versuche geschabte Bilder zu platzieren:
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;*/
}
}
}
Und XAML-Code zu dieser Seite sieht so aus:
<?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>
Warum funktioniert die Webansicht nicht? Was muss ich tun, um diese Bilder anzuzeigen?
Sie erstellen Ihr WebView
mit dem folgenden Code
[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;*/
}
Wie auch immer, es gibt keinen Ort, wo Sie das WebView
Ihrer Seite hinzufügen . Wenn Sie das WebView
wirklich aus Code erstellen WebView
, müssen Sie dem StackLayout
einen Namen StackLayout
<StackLayout x:Name="StackLayout" ...>
und fügen Sie das WebView
von Ihrem MainPage
Konstruktor hinzu.
StackLayout.Children.Add(webView);
Wie auch immer, nichts hindert Sie daran, das WebView
von XAML zu WebView
<StackLayout BackgroundColor="#505050"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<WebView x:Name="WebView" VerticalOptions="FillAndExpand />
</StackLayout>
und dann einfach die Quelle im Code hinterlegen
public MainPage()
{
InitializeComponent();
WebView.Source = new HtmlWebViewSource
{
Html = @"<!DOCTYPE html><html><head><style>img { width:100%; }</style></head><body>" + App.strona + @"</body></html>",
};
}
Auf der Seite, auf der das WebView deklariert wird, wenn das Hinzufügen zu einem Stacklayout die Angabe von WidthRequest und HeightRequest des WebView zur Anzeige erfordert, tritt es nicht auf, wenn es in einem GridLayout für diesen Fall hinzugefügt wird
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;*/
}
}
}