Xamarin表格 - 制作网页视图回去 [英] Xamarin Forms - making webview go back

查看:363
本文介绍了Xamarin表格 - 制作网页视图回去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上午所有,

我工作在Xamarin.Forms一个小跨平台的WebView项目。我有工作的WebView但我想其中有一个后退和前进按钮在工具栏中添加。

I am working on a little crossplatform webview project in Xamarin.Forms. I have the webview working but I want to add in a toolbar which has a Back and Forward buttons.

我尝试了许多不同的方式,但似乎没有被工作特别好。我试着按照这个家伙后导航工具栏

I tried a number of different ways but nothing seems to be working particularly well. I tried to implement this feature by following this guys post Navigation Toolbar

我会附上我的code以下,如果有人可以给我一个手这还是一个解决方案,将是巨大的!

I will attach my code below and if someone could give me a hand with this or a solution that would be great!

和,如果这个问题已经被其他用户回答previously那么我道歉。

And if this question has already been answered previously by another user then I apologize.

App.cs

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
        }
    }
}

WebPage.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"
                    }
                }
            };
        }
    }
}

期待得到一个答案,这是香港专业教育学院一直停留在其上很长一段时间了。

Looking forward to getting an answer to this as ive been stuck on it for quite awhile now.

亲切的问候

推荐答案

编辑code这样。
首先包装你的的MainPage NavigationPage 来使工具条可见。

Edit your code like this. First wrap your MainPage in a NavigationPage to make the toolbar visible.

这基本上可以归结为创建一个变量能够访问你的的WebView 更容易。

It basically comes down to create a variable to be able to access your WebView easier.

然后创建你一个按钮 ToolbarItems 集合可以触发事件。而在这事件中,你可以调用上的的WebView已经可用的 GoBack的()

Then create a button in you ToolbarItems collection which can trigger an event. And in that event you can call the already available GoBack() method on the WebView.

您可能想签出的的WebView Xamarin文档,它可能是一个好主意,检查是否可以的居然回去与 CanGoBack 属性。

You probably want to check out the Xamarin documentation on the WebView, it is probably a good idea to check if you can actually go back with the CanGoBack property.

请注意,我已经分配了一个 BackIcon.png 你可以用更换或自己当然图标。
另外,code尚未经过测试,这只是我的头,所以也许有缺少一些分号或顶部的东西。

Note that I have assigned a BackIcon.png you can replace it with null or your own icon of course. Also the code hasn't been tested, this is just out of the top of my head so maybe there are missing some semi-colons or stuff.

App.cs

// ... other code

public App()
{
   // The root page of your application
   MainPage = new NavigationPage(new WebPage());
}
// ... other code

WebPage.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"
                    };

            // Create toolbar items here
            ToolbarItems.Add(new ToolbarItem("Back", "BackIcon.png", () => { _webview.GoBack(); }));

            Content = new StackLayout
            {
                Children = { _webView}
            };
        }
    }
}

这篇关于Xamarin表格 - 制作网页视图回去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆