代码之家  ›  专栏  ›  技术社区  ›  Ivan S.

UWP/C#:如果在Web视图中打开了某个html文件,则显示后退按钮

  •  0
  • Ivan S.  · 技术社区  · 6 年前

    我正在从通用Windows平台的Cordova应用程序进行移植。此时,我基本上创建了一个容器,其中显示本地html文件(它们也是从Cordova应用程序移植的)。现在,我想在窗口的左上角添加一个后退按钮,就像任何UWP应用程序一样。为此,我使用以下说明:

    SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
    

    它工作得很好,但我希望只有当某个html文件显示在webview中时,此按钮才会出现。应该是这样的逻辑:

    1) If certain_file.html is loaded
    1.1) Show the back button
    2) Else
    2.1) Hide the back button
    

    我正在使用Visual Studio和Visual C#。你知道我该怎么做吗?

    谢谢

    3 回复  |  直到 6 年前
        1
  •  2
  •   Xie Steven    6 年前

    您可以使用 DOMContentLoaded 事件来决定是否显示“后退”按钮。

    例如:

    <WebView DOMContentLoaded="WebView_DOMContentLoaded" Source="ms-appx-web:///HTMLPage1.html"></WebView>
    
    private void WebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
    {
        if (args.Uri.LocalPath == "/HTMLPage1.html")
        {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        }
        else
        {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
        }
    }
    

    enter image description here

    enter image description here

        2
  •  0
  •   Ivan S.    6 年前

    @谢泽维尔-MSFT这是主页。xaml

    <Page
    x:Class="Project_Onome.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Project_Onome"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
    <Grid>
        <WebView x:Name="OnomeWebContainer"/>
        <WebView HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <WebView Source="ms-appx-web:///www/index.html"/>
        <WebView DOMContentLoaded="OnomeWebContainer_DOMContentLoaded"/>
    </Grid>
    

    这是主页。xaml。cs公司

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Core;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    namespace Project_Onome
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                WebView OnomeWebContainer = new WebView();
                OnomeWebContainer.DOMContentLoaded += OnomeWebContainer_DOMContentLoaded;
            }
        private void OnomeWebContainer_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
        {
            if (args.Uri.LocalPath == "/security_auth.html")
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
            }
        }
    }
    

    }

        3
  •  0
  •   Ivan S.    6 年前

    更新的代码,现在一切正常!

    1) 主页面。xaml

    <WebView Name="OnomeWebContainer" Source="ms-appx-web:///www/index.html" DOMContentLoaded="OnomeWebContainer_DOMContentLoaded"></WebView>
    

    2) 主页面。xaml。cs公司

    private void OnomeWebContainer_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
        {
            if (args.Uri.LocalPath == "/www/security_auth.html")
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
            }
        }
    

    感谢谢泽维尔的帮助!:)