我想实现一个ContentPage,它有一个WebView(html文本)和按钮“我同意”和“取消”
问题:我想要一个webView插入到堆栈中,因此,我必须声明Width和Height。 高度是我的主要问题,因为我想跨越或FillAndExpend WebView。
所以我追求webView.Eval(“一些js代码”)函数,但我不能得到一个返回值(html的高度?) 另外,我以不同的方式接近,在webView onBottom时获取事件,然后得到一些布尔值。
以下是代码:
StackLayout mainStackLayout = new StackLayout { VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center };
Frame f = new Frame { BackgroundColor = Constants.iBackgroundWhite };
StackLayout motherStack = new StackLayout { Margin = new Thickness(15, 30) };
StackLayout buttonStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 10)
};
Label captionLabel = new Label
{
Text = "Terms and Conditions",
TextColor = Constants.iPink,
FontAttributes = FontAttributes.Bold,
FontSize = 25,
Margin = new Thickness(10, 0, 0, 15)
};
var body = new HtmlWebViewSource();
body.Html = @"<html><body>
Lorem ipsum dolor sit amet, utinam tritani interpretaris mel ei, perfecto
instructior ut vim. Usu ad erant maiorum, tollit latine ei ius, ea eos
vitae nominati iracundia. Consul laboramus eam ad, eu est nisl expetendis
philosophia. Doctus ceteros eu per, ut vel fierent similique accommodare,
ius similique consectetuer et.
Te nec vero quas utamur, in possim nonumes inimicus his, an animal recusabo
per. Eos ut natum bonorum legimus. Nihil liberavisse no usu. Sea dignissim
mnesarchum cu, et per simul persius, constituto definitionem per in. No vix
novum verear apeirian, aliquam accumsan definitionem mel at. Mea et autem
phaedrum iracundia, reque vidisse at usu.
Ad mea commodo oportere. Quo ad debitis accusata atomorum. His dicit
labores et, in dicat dolorum abhorreant est, ex qui voluptua oportere. Eum
ex nonumy cotidieque signiferumque. Laudem commodo omittantur quo in, voluptua
reformidans nam ad, ignota dicunt sed an. Mei ad sonet nonumes, no sit detracto
officiis.
Cibo exerci duo eu. Has habeo atomorum disputationi te, ne has idque delicata.
Cibo nihil euismod qui ut. Dicit ubique maiorum duo et, eam ad probo percipit
sententiae. Quas lucilius vis ne. Id nec fabellas consetetur, sit id melius
impetus cotidieque, eum fierent praesent rationibus id. Qui et liber offendit
verterem, ad eripuit eruditi qui.
Vis affert instructior cu, cum id principes adolescens. Per omnes nominati
salutatus at, ne audiam noluisse mel, aeque docendi fierent pro ne. At putant
voluptua vis, duo ne etiam tritani. Ut solet laudem qualisque usu, dicunt
adipisci his cu. Ea labore convenire corrumpit nec. Ne est quando virtute,
virtute adipiscing an ius, an mei iracundia posidonium
</body> </html>";
_webView = new WebView
{
Source = body,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
WidthRequest = App.ScreenWidth * 0.8,
HeightRequest = 500
};
_webView.IsEnabled = false;
#region Buttons
Button okButton = new Button();
okButton.SetWhiteButton();
okButton.Text = "I ACCEPT";
okButton.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) - 5;
okButton.WidthRequest = Device.GetNamedSize(NamedSize.Large, typeof(Button)) * 5;
okButton.BorderRadius = Constants.BorderRadius + 5;
Button cancelButton = new Button();
cancelButton.SetDefaultButton();
cancelButton.Text = "CANCEL";
cancelButton.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) - 5;
cancelButton.WidthRequest = Device.GetNamedSize(NamedSize.Large, typeof(Button)) * 5;
cancelButton.BorderRadius = Constants.BorderRadius + 5;
bool isChekedTaC = false;
okButton.Clicked += (s, e) =>
{
isChekedTaC = true;
Navigation.PushAsync(new RegisterOnePageNew(isChekedTaC, registrationRequestModel, "1234"));
};
#endregion
//var a = _webView.Eval("window.onscroll = function(ev) { if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight){alert('youre on the bottom');}};"
//_webView.Navigated += (o, s) => {
// _webView.Eval("alert('text')");
//};
buttonStack.Children.Add(cancelButton);
buttonStack.Children.Add(okButton);
buttonStack.HorizontalOptions = LayoutOptions.CenterAndExpand;
buttonStack.VerticalOptions = LayoutOptions.Start;
grid.Children.Add(_webView, 0, 0);
//grid.Children.Add(buttonStack, 1, 0);
StackLayout sl = new StackLayout();
//sl.Children.Add(contentLabel);
sl.Children.Add(_webView);
sl.Children.Add(buttonStack);
mainStackLayout.Children.Add(captionLabel);
mainStackLayout.Children.Add(new ScrollView { Content = sl });
//mainStackLayout.Children.Add(buttonStack);
f.Content = mainStackLayout;
motherStack.Children.Add(f);
Content = sl;
}
protected override bool OnBackButtonPressed()
{
Navigation.PopAsync();
return true;
}
当HTML WebView到达底部时,我只想呈现我的两个按钮。我怎样才能做到这一点?
#Option 1:在底部时返回true / false 我使用XLabs HybridWebView完成了一段时间。这是代码,可能对某些人有用。
public class HybridView : ContentPage
{
HybridWebView hybrid;
public HybridView()
{
var stack = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.White
};
var body = new HtmlWebViewSource();
body.Html = @"<html><body>
Lorem ipsum dolor sit amet, utinam tritani interpretaris mel ei, perfecto instructior ut vim. Usu ad erant maiorum, tollit latine ei ius, ea eos vitae nominati iracundia. Consul laboramus eam ad, eu est nisl expetendis philosophia. Doctus ceteros eu per, ut vel fierent similique accommodare, ius similique consectetuer et.
Te nec vero quas utamur, in possim nonumes inimicus his, an animal recusabo per. Eos ut natum bonorum legimus. Nihil liberavisse no usu. Sea dignissim mnesarchum cu, et per simul persius, constituto definitionem per in. No vix novum verear apeirian, aliquam accumsan definitionem mel at. Mea et autem phaedrum iracundia, reque vidisse at usu.
Ad mea commodo oportere. Quo ad debitis accusata atomorum. His dicit labores et, in dicat dolorum abhorreant est, ex qui voluptua oportere. Eum ex nonumy cotidieque signiferumque. Laudem commodo omittantur quo in, voluptua reformidans nam ad, ignota dicunt sed an. Mei ad sonet nonumes, no sit detracto officiis.
Cibo exerci duo eu. Has habeo atomorum disputationi te, ne has idque delicata. Cibo nihil euismod qui ut. Dicit ubique maiorum duo et, eam ad probo percipit sententiae. Quas lucilius vis ne. Id nec fabellas consetetur, sit id melius impetus cotidieque, eum fierent praesent rationibus id. Qui et liber offendit verterem, ad eripuit eruditi qui.
Vis affert instructior cu, cum id principes adolescens. Per omnes nominati salutatus at, ne audiam noluisse mel, aeque docendi fierent pro ne. At putant voluptua vis, duo ne etiam tritani. Ut solet laudem qualisque usu, dicunt adipisci his cu. Ea labore convenire corrumpit nec. Ne est quando virtute, virtute adipiscing an ius, an mei iracundia posidonium.
</body> </html>";
hybrid = new HybridWebView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.White,
WidthRequest = 300,
HeightRequest = 600,
Source = body
};
stack.Children.Add(hybrid);
Content = stack;
}
protected override void OnAppearing()
{
base.OnAppearing();
hybrid.InjectJavaScript("window.onscroll = function(ev) { if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight){alert('youre on the bottom');}};");
}
}
现在我必须弄清楚如何从js
获取xamarin表单的信息答案 0 :(得分:1)
您可以将“我同意”和“取消”按钮放入HTML中,然后注入旧源。通过这种方式,您可以使用单个WebView显示所有。