Dug*_*gan 7 c# android webview ios xamarin.forms
大家早,
我正在 Xamarin.Forms 中开发一个小的跨平台 webview 项目。我有 webview 工作,但我想添加一个工具栏,它有一个后退和前进按钮。
我尝试了许多不同的方法,但似乎没有什么特别有效。我尝试通过关注这些人发布导航工具栏来实现此功能
我将在下面附上我的代码,如果有人可以帮我解决这个问题或解决方案,那就太好了!
如果另一个用户之前已经回答了这个问题,那么我深表歉意。
应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace DisplayWebPage
{
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new WebPage();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
Run Code Online (Sandbox Code Playgroud)
网页.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
namespace DisplayWebPage
{
public class WebPage : ContentPage
{
public WebPage()
{
Content = new StackLayout
{
Children = {
new WebView
{
Source = "http://www.google.com"
}
}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
期待得到这个答案,因为我已经坚持了很长一段时间了。
亲切的问候
像这样编辑你的代码。首先将您包裹MainPage在 a 中NavigationPage以使工具栏可见。
它基本上归结为创建一个变量以便能够WebView更轻松地访问您的变量。
ToolbarItems然后在您的集合中创建一个可以触发事件的按钮。在这种情况下,您可以GoBack()调用WebView.
您可能想查看有关 的Xamarin 文档WebView,检查您是否真的可以返回该CanGoBack属性可能是个好主意。
请注意,我已经分配了一个您当然BackIcon.png可以用您自己的图标替换它的图标。null另外,代码还没有经过测试,这只是我的想法,所以可能缺少一些分号或东西。
应用程序.cs
// ... other code
public App()
{
// The root page of your application
MainPage = new NavigationPage(new WebPage());
}
// ... other code
Run Code Online (Sandbox Code Playgroud)
网页.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
namespace DisplayWebPage
{
public class WebPage : ContentPage
{
private WebView _webView;
public WebPage()
{
// Assign a WebView to a global variable
_webView = new WebView
{
Source = "http://www.google.com",
HeightRequest="1000",
WidthRequest="1000"
};
// Create toolbar items here
ToolbarItems.Add(new ToolbarItem("Back", "BackIcon.png", () => { _webview.GoBack(); }));
Content = new StackLayout
{
Children = { _webView}
};
}
}
}
Run Code Online (Sandbox Code Playgroud)