UWP/C#: 如果某个 html 文件在 WebView 中打开,则显示后退按钮
UWP/C#: Display back button if certain html file is opened in a WebView
我正在为通用 Windows 平台从 Cordova 应用程序移植。此时,我基本上创建了一个容器,其中显示了本地 html 文件(它们也是从 Cordova 应用程序移植过来的)。现在,我想在 window 的左上角添加一个后退按钮,就像任何 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#。你知道我该怎么做吗?
谢谢!
您可以使用 webview 的 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;
}
}
@Xavier Xie - MSFT 这是MainPage.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>
这是 MainPage.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;
}
}
}
}
已更新代码,现在一切正常!
1) MainPage.xaml
<WebView Name="OnomeWebContainer" Source="ms-appx-web:///www/index.html" DOMContentLoaded="OnomeWebContainer_DOMContentLoaded"></WebView>
2) MainPage.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;
}
}
感谢Xavier Xie 的帮助! :)
我正在为通用 Windows 平台从 Cordova 应用程序移植。此时,我基本上创建了一个容器,其中显示了本地 html 文件(它们也是从 Cordova 应用程序移植过来的)。现在,我想在 window 的左上角添加一个后退按钮,就像任何 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#。你知道我该怎么做吗?
谢谢!
您可以使用 webview 的 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;
}
}
@Xavier Xie - MSFT 这是MainPage.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>
这是 MainPage.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;
}
}
}
}
已更新代码,现在一切正常!
1) MainPage.xaml
<WebView Name="OnomeWebContainer" Source="ms-appx-web:///www/index.html" DOMContentLoaded="OnomeWebContainer_DOMContentLoaded"></WebView>
2) MainPage.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;
}
}
感谢Xavier Xie 的帮助! :)