浏览器控制不放弃第一个访问过的网页 [英] browser control not letting go of the first visited web page

查看:49
本文介绍了浏览器控制不放弃第一个访问过的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中包含指向多个外部网页的链接,我使用Web浏览器控件来呈现网页。我遇到了浏览器控件的问题,缓存了我在我的应用中访问的第一个页面,并且在显示
新网页之前总是显示,当我点击其他链接时。它显示了第一个访问过的页面,当我从任何其他页面通过导航堆栈返回时,在显示我的应用页面之前显示的最后一页是我去过的第一个网页...

I have an app that has links to several external web pages and I am using the web browser control to render the web pages. I am running into the issue of the browser control caching the first page I visit in my app and always showing that before showing the new web page, when I click on other links. It show the first visited page and also when I am going back through the navigation stack from any other page, the last page it shows before showing my app page is the first web page I had gone to...

如何清除缓存,以便直接转到点击链接的网页,而不是暂时显示我已访问过的第一个网页,当我在导航堆栈中移回时也不会显示。

How do I clear the cache so it goes straight away to the clicked link's web page rather than momentarily showing the first web page I had already visited and also not show it when I move back in the navigation stack.

非常感谢任何示例代码。谢谢。我的xaml和C#:

Any sample code is much appreciated. Thanks. my xaml and C#:

<phone:PhoneApplicationPage
    x:Class="O365_Content_App.test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    ApplicationBar = "{StaticResource GlobalAppBar}">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="#FF0072C6">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel Grid.Row="0" Margin="0,0,0,0" Background="#DC3C00">
            <Image x:Name="bannerImage" Source="images/logo_resourceguide.png" Margin="10,0,0,0" Stretch="None" VerticalAlignment="top" HorizontalAlignment="Left"/>
       
        </StackPanel>

      
        <ScrollViewer  VerticalScrollBarVisibility="Visible"> 
            
        <!--ContentPanel - place additional content here-->
         <Grid x:Name="ContentPanel" Margin="0,50,0,0" Background="#FF0072C6" Grid.RowSpan="2" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

            
            <TextBlock x:Name="serviceHealth" Text="link1"  HorizontalAlignment="Left" Margin="10,10,0,0"  Grid.Row="0" TextWrapping="NoWrap"  VerticalAlignment="Bottom" Tap="link1_Tap" />
             <TextBlock Text="link2" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link2_Tap"/>
            <TextBlock Text="link3" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link3_Tap"/>

                <!--<HyperlinkButton NavigateUri="/homepage.xaml" Content="link5"  Grid.Row="3" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"  Style="{StaticResource  ResourceKey=HyperlinkButtonStyle}" />-->

                <!--<HyperlinkButton NavigateUri="HomePage.xaml" Content="link5"  Grid.Row="3" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"/>
                <HyperlinkButton NavigateUri="/HomePage.xaml" Content="link6"  Grid.Row="4" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"/>-->


            </Grid>

        </ScrollViewer>
        <phone:WebBrowser Grid.Row="0" Name="webBrowser1"  Visibility="Collapsed"  Width="auto" Height="auto" Grid.RowSpan="2" IsScriptEnabled="True"/>

    </Grid>


</phone:PhoneApplicationPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System.Net.Http;
using System.IO;
using System.Windows.Media;
using System.ComponentModel;

namespace O365_Content_App
{
    public partial class test : PhoneApplicationPage
    {
        public test()
        {
            InitializeComponent();

        }



        private void appbar()
        {

            ApplicationBar = new ApplicationBar();
            ApplicationBar.Mode = ApplicationBarMode.Default;
            ApplicationBar.Opacity = 1.0;
            ApplicationBar.IsVisible = true;
            ApplicationBar.IsMenuEnabled = true;
            ApplicationBar.BackgroundColor = Color.FromArgb(225, 220, 60, 0);

            ApplicationBarIconButton button1 = new ApplicationBarIconButton();
            button1.IconUri = new Uri("/Images/favs.addto.png", UriKind.Relative);
            button1.Text = "Add as Favorite";
            ApplicationBar.Buttons.Add(button1);
            button1.Click += new EventHandler(button1_Click);

            ApplicationBarIconButton button2 = new ApplicationBarIconButton();
            button2.IconUri = new Uri("/Images/feature.email.png", UriKind.Relative);
            button2.Text = "Email";
            ApplicationBar.Buttons.Add(button2);
            button2.Click += new EventHandler(button2_Click);

            ApplicationBarIconButton button3 = new ApplicationBarIconButton();
            button3.IconUri = new Uri("/Images/homeicon.png", UriKind.Relative);
            button3.Text = "Home";
            ApplicationBar.Buttons.Add(button3);
            button3.Click += new EventHandler(button3_Click);

            ApplicationBarMenuItem menuItem2 = new ApplicationBarMenuItem();
            menuItem2.Text = "Rate the App";
            ApplicationBar.MenuItems.Add(menuItem2);
            menuItem2.Click += new EventHandler(rateapp_Click);


        }


        //method to capture the web page title and  URL once the browser has completed loading
        string WebPageTitle = string.Empty;
        string pageURL = string.Empty;
        
        private void Mywebbrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            WebBrowser webbrowser1 = sender as WebBrowser;
            WebPageTitle = (string)webBrowser1.InvokeScript("eval", "document.title.toString()");
            pageURL = (string)webBrowser1.Source.ToString();
        }


        //method to render web page in web browser control
        private void mybrowser(string site)
        {
            webBrowser1.Visibility = System.Windows.Visibility.Visible;
            //webBrowser1.Source = new Uri(site, UriKind.Absolute);
            webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
        }



        private void link1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            appbar();
            mybrowser("http://www.microsoft.com");

        }

        private void link2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            appbar();
            mybrowser("http://www.intel.com");

        }

        private void link3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            appbar();
            mybrowser("http://www.ibm.com");

        }


        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (webBrowser1.CanGoBack)
            {
                e.Cancel = true;
                webBrowser1.GoBack();

                //webBrowser1.InvokeScript( "eval", "history.go(-1)");
                
                //try
                //{                  
                //    webBrowser1.InvokeScript("eval", "window.location.reload()");
                //}

                //catch (Exception) { } 
           }

            else if (webBrowser1.Visibility == System.Windows.Visibility.Visible)
            {

                e.Cancel = true;
                webBrowser1.Visibility = System.Windows.Visibility.Collapsed;
                
            }
            else
            {
                base.OnBackKeyPress(e);
            }
        }


       


        
        private void button1_Click(object sender, EventArgs e)
        {

            string what = webBrowser1.Source.ToString();

            MessageBox.Show(what);
            EmailComposeTask emailComposeTask = new EmailComposeTask();
            emailComposeTask.Subject = "subject to include web page title";
            emailComposeTask.Body = what;
            emailComposeTask.Show();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {


            //rateapp.Rate();
            MarketplaceReviewTask ratethisapp = new MarketplaceReviewTask();
            ratethisapp.Show();
         }


        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button 3 works!");
        }

        private void rateapp_Click(object sender, EventArgs e)
        {
            //rateapp.Rate();
            MarketplaceReviewTask ratethisapp = new MarketplaceReviewTask();
            ratethisapp.Show();
        }


  

    }
}






mujno



mujno

推荐答案

您无法删除历史记录。

You cannot erase the history.

你有几个选择:

1。通过存储浏览器导航到的URL(导航事件)并手动管理后退按钮来浏览您维护的历史记录,自己管理历史记录。 适当时擦除。

1. Manage the history yourself by storing the URLs your browser navigates to (Navigating event) and manually managing the back button to navigate through the history you maintain.  Erase when appropriate.

2。在后面只需设置:空白并关闭浏览器(以防止上一页显示):

2. On back simply set about:blank and close the browser (to prevent previous page from showing):

  protected override void OnBackKeyPress(CancelEventArgs e)
        {
          if (webBrowser1.Visibility == System.Windows.Visibility.Visible)
            {
                webBrowser1.Source = new Uri("about:blank");
                e.Cancel = true;
                webBrowser1.Visibility = System.Windows.Visibility.Collapsed;

            }
          
        }

3。通过代码创建浏览器控件并在每次完成时销毁它(有效地为每个链接创建一个新的控件实例)。

3. Create the browser control via code and destroy it each time you are done with it (effectively creating a new instance of the control for each link).

杰夫


这篇关于浏览器控制不放弃第一个访问过的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆