WebView Progress环形窗口8.1应用程序

时间:2014-01-23 12:02:17

标签: c# windows-8 webview progress-bar windows-8.1

我有一个问题,我想在webview中使用Progress环,但它没有按照我的意愿去做。即使页面已加载,进度环也会保留。我也想在加载后出现按钮。

这是我的代码:

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

// Die Elementvorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink  /?LinkId=234238 dokumentiert.

namespace Euregio_Systems
    {
    /// <summary>
    /// Eine leere Seite, die eigenständig verwendet werden kann oder auf die     innerhalb eines Rahmens navigiert werden kann.
    /// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.webBrowser.InvokeScript("eval", new[] { "history.go(-1)" });
    }


    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
    }

    private void Button_Click_3(object sender, RoutedEventArgs e)
    {
        this.webBrowser.InvokeScript("eval", new[] { "histroy.go(+1)" });
    }
    private void webBrowser_NavigationCompleted(object sender, NavigatingCancelEventArgs e)
    {
        pr1.IsActive = false;
        pr1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        webBrowser.Visibility = Windows.UI.Xaml.Visibility.Visible;
        fwd.Visibility = Windows.UI.Xaml.Visibility.Visible;
        backButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
        homeButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
    }
}
}

和xaml     

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <WebView Name="webBrowser" Visibility="Collapsed"/>
    <AppBarButton Visibility="Collapsed" Name="backButton" HorizontalAlignment="Left" Margin="-20,-4,0,0" VerticalAlignment="Top" Click="Button_Click_1" Icon="Back"/>
    <AppBarButton Visibility="Collapsed" Name="homeButton" HorizontalAlignment="Left" Margin="56,-4,0,0" VerticalAlignment="Top" Click="Button_Click_2" Icon="Home"/>
    <AppBarButton Visibility="Collapsed" Name="fwd" HorizontalAlignment="Left" Margin="132,-4,0,0" VerticalAlignment="Top" Icon="Forward" Click="Button_Click_3"/>
    <ProgressRing Visibility="Visible" Name="pr1" HorizontalAlignment="Center" VerticalAlignment="Center" IsActive="True" Height="163" Width="170" Margin="0" RequestedTheme="Light"/>

</Grid>
</Page>

2 个答案:

答案 0 :(得分:3)

您必须在代码中的Web浏览器上调用Navigate之前添加此行:

  

this.webBrowser.NavigationCompleted+= webBrowser_NavigationCompleted;

理想情况下,您应该在WebView的加载事件上执行此操作,可以像这样添加:

<WebView Name="webBrowser" Visibility="Collapsed" loaded="WebViewControl_Loaded"/>

答案 1 :(得分:0)

如果您忘记订阅活动,则应打开表单/页面设计器并检查“Lightning”图标以显示您可以订阅的活动,如此图像

enter image description here

只需查看NavigationCompleted事件,并将您的回调方法名称(webBrowser_NavigationCompleted)放入。

尽管现在使用这些事件有点气馁。 MVVM是开发Windows应用商店应用(或任何现代WPF应用)以充分利用WPF绑定功能的首选方法。

<强>更新 或者,如果您愿意,也可以在代码中订阅(只需确保在初始化组件后执行此操作):

this.webBrowser.NavigationCompleted += webBrowser_NavigationCompleted;