Xamarin表格 - 制作网页视图回去

问题描述 投票:4回答:3

大家早,

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

我尝试了许多不同的方式,但似乎没有被工作特别好。我试图通过以下这个家伙张贴Navigation Toolbar来实现此功能

我会附上我下面的代码,如果有人可以给我个忙或解决方案,将是巨大的!

而且,如果这个问题以前已经由其他用户回答那么我道歉。

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

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

亲切的问候

c# android ios webview xamarin.forms
3个回答
3
投票

编辑您这样的代码。首先换你MainPageNavigationPage使工具条可见。

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

然后创建你ToolbarItems收集一个按钮,可以触发事件。而在这种情况下,你可以呼吁GoBack()已经可用WebView方法。

你可能想检查出Xamarin documentationWebView,这大概是来检查,如果你实际上可以用CanGoBack财产回去是个好主意。

请注意,我已经指定BackIcon.png你可以null或自己当然图标替换它。另外,代码没有经过测试,这只是我的头了,所以也许有缺少一些分号或东西顶部的。

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

1
投票

请尝试以下步骤:在网页视图对象添加导航中的事件

 List<string> history = new List<string>();
 WebView myWebView = new WebView
 {
      Source = "http://www.google.com"
 };
myWebView.Navigated += WebViewPage_Navigated;
Children = myWebView;

然后定义如下功能:

public void WebViewPage_Navigated(object sender, WebNavigatedEventArgs e)
{
    WebNavigationResult result = e.Result;            
    if(result.Equals(WebNavigationResult.Success)){
        currentUrl = e.Url;
       history.Add(currentUrl);
    }            
}

现在,您可以绑定“转发”和基于历史列表计数和当前元素(如移动到后,再加入转发列表)“上一个”元素。希望这可以帮助。


1
投票

通过answerGerald Versluis是巨大的。在此基础上,我想和大家分享我的代码看起来像后,我试图解决以下问题:

  • 试图执行在XAML的工具栏和web视图(使用VS2017)
  • 显示头版时,隐藏的工具栏(无需回)
  • 重写装置返回按钮

App.xaml.cs

// ... using, namespace etc

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            MainPage = new NavigationPage(new MainPage());
        }
    }

// ...

MainPage.xaml中

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp"
             x:Class="MyApp.MainPage"
             NavigationPage.HasNavigationBar="False"
             Title="My App">

    <ContentPage.ToolbarItems>
        <ToolbarItem Name="Back" Clicked="OnBack"></ToolbarItem>
    </ContentPage.ToolbarItems>

    <WebView x:Name="browser" Navigating="OnNavigating"></WebView>
</ContentPage>

MainPage.xaml.cs中

using System;
using Xamarin.Forms;

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        string Url;

        public MainPage()
        {
            InitializeComponent();
            browser.Source = Url = GetUrl();
        }

        void OnBack(object sender, EventArgs args)
        {
            browser.GoBack();
        }

        protected override bool OnBackButtonPressed()
        {
            if (browser.CanGoBack)
            {
                browser.GoBack();
                return true;
            }
            else return base.OnBackButtonPressed();
        }

        void OnNavigating(object sender, WebNavigatingEventArgs args)
        {
            // Checking if we are at the home page url
            // browser.CanGoBack does not seem to be working (not updating in time)
            NavigationPage.SetHasNavigationBar(this, args.Url != Url);
        }

        string GetUrl()
        {
            // Possibly some mechanism to accomoddate for several locales
            return "...";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.