기존 웹 사이트의 모바일 앱을 만들고 있습니다. 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처럼 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가 작동하지 않습니까? 이 사진을 표시하려면 어떻게해야합니까?
다음 코드로 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
<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를 선언하는 페이지에서 스택 레이 아웃에 추가되는 시간이 지정된 경우 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;*/
}
}
}