【问题标题】:Simple navigation in Windows 8 web viewWindows 8 Web 视图中的简单导航
【发布时间】:2014-06-03 18:13:39
【问题描述】:

我正在将 Windows Phone 8 应用程序移植到平板电脑,但遇到了WebView API 的问题。在 Windows Phone 8 和 Windows 8.1 中,WebBrowserWebView 控件都有一个 GoBack() 方法。但是,我需要我的应用程序与 Windows 8 兼容,其WebView API 没有这种方法。是否有任何人用于 Windows 8 应用的替代方案/解决方法?

【问题讨论】:

    标签: windows windows-8 webview windows-store-apps


    【解决方案1】:

    最后,我只是为 WebView 编写了一个包装器来管理导航堆栈。这是相关代码,任何有兴趣的人。请注意,我只需要处理向后导航,所以我使用了Stack。如果还需要前向导航,则将Stack 替换为List 并存储当前页面的索引可能更有意义。

    public class WebViewWrapper
    {
        private Stack<Uri> _navigationStack;
        private Uri _currentUri;
    
        public WebView WebView { get; private set; }
        public bool CanGoBack
        {
            get { return _navigationStack.Count > 0; }
        }
    
        public WebViewWrapper(WebView _webView)
        {
            _navigationStack = new Stack<Uri>();
            WebView = _webView;
            WebView.LoadCompleted += (object s, NavigationEventArgs e) => {
                if (_currentUri != null)
                {
                    _navigationStack.Push(_currentUri);
                }
                _currentUri = e.Uri;
            };
        }
    
        public void GoBack()
        {
            if (CanGoBack)
            {
                _currentUri = null;
                WebView.Navigate(_navigationStack.Pop());
            }
        }
    }
    

    使用示例如下:

    // Code behind for a view called WebBrowserPage
    public sealed partial class WebBrowserPage : Page 
    {
        private WebViewWrapper _webViewWrapper;
    
        public WebBrowserPage()
        {
            // webView is a WebView in the xaml with x:Name="webView"
            _webViewWrapper = new WebViewWrapper(webView);  
        }
    
        // Other code for navigating to a Uri specified in a ViewModel.
    
        // Event handler for a back button press
        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (_webViewWrapper.CanGoBack)
            {
                _webViewWrapper.GoBack();
            }
            else 
            {
                // Code that executes a command in the ViewModel to leave the WebBrowserPage
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      WinRT XAML Toolkit 有一个 WebBrowser 控件可以完成其中的一些工作,但我没有在任何应用程序中使用它,因此我无法保证它的质量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        • 2020-10-10
        • 2013-06-11
        • 1970-01-01
        相关资源
        最近更新 更多