Estoy creando una aplicación móvil del sitio web existente. Yo uso HTMLAGILITYPACK para desechar algunos datos del sitio web. Quiero mostrarlos en mi aplicación. Pero no hay resultado y nada se muestra.
Este es mi código de aplicación con "raspador":
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);
}
}
} En la consola devuelve algunos img en html como img src = "link" class = "imageitself" alt = "blablabla"
Aquí está mi página donde estoy tratando de colocar imágenes raspadas:
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;*/
}
}
}
Y el código XAML a esta página se ve así:
<?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>
¿Por qué webview no funciona? ¿Qué debo hacer para mostrar estas imágenes?
Estás creando tu WebView
con el siguiente código
[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;*/
}
De todos modos, no hay ningún lugar donde esté agregando WebView
a su página. Si realmente desea crear el WebView
desde el código, tendrá que darle un nombre al StackLayout
<StackLayout x:Name="StackLayout" ...>
y luego agregue el WebView
desde su constructor de MainPage
.
StackLayout.Children.Add(webView);
De todos modos, no hay nada que le impida crear el WebView
desde XAML
<StackLayout BackgroundColor="#505050"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<WebView x:Name="WebView" VerticalOptions="FillAndExpand />
</StackLayout>
y luego simplemente establece la fuente en el código detrás
public MainPage()
{
InitializeComponent();
WebView.Source = new HtmlWebViewSource
{
Html = @"<!DOCTYPE html><html><head><style>img { width:100%; }</style></head><body>" + App.strona + @"</body></html>",
};
}
En la página donde se declara WebView, cuando se agrega a un stacklayout se deben especificar WidthRequest y HeightRequest de WebView para mostrar, no ocurre cuando se agrega a GridLayout, para este caso
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;*/
}
}
}