0

我有一个使用简单的向前和向后导航方案的网络浏览器,如果用户选择,也可以刷新或停止导航。所有导航都正常工作,但是当我尝试切换“刷新”和“停止”按钮时,会出现我的问题,具体取决于网络浏览器是正在导航还是已导航。只要没有导航发生,我希望将刷新按钮设置为可见,并且在导航发生时将停止按钮设置为可见。我的基本实现如下,但根据这些场景,我似乎无法让按钮从可见切换到折叠。

主页.xaml

<Button x:Name="RefreshButton" Content="" Style="{StaticResource RefreshBtn}" Grid.Column="0" Grid.Row="0" Visibility="Visible" Click="RefreshButton_Click" toolkit:TiltEffect.IsTiltEnabled="True">
<Button x:Name="StopButton" Content="" Style="{StaticResource StopBtn}" Grid.Column="0" Grid.Row="0" Visibility="Collapsed" Click="StopButton_Click" toolkit:TiltEffect.IsTiltEnabled="True"/>

MainPage.xaml.cs

public MainPage()
    {
        InitializeComponent();

        //Change Navigation buttons if the browser is currently Navigating
        if (TheBrowser._IsNavigating == false)
        {
            RefreshButton.Visibility = Visibility.Visible;
            StopButton.Visibility = Visibility.Collapsed;
        }
        else
        {
            RefreshButton.Visibility = Visibility.Collapsed;
            StopButton.Visibility = Visibility.Visible;
        }
        //while (TheBrowser._IsNavigating == true)
        //{
        //    RefreshButton.Visibility = Visibility.Collapsed;
        //    StopButton.Visibility = Visibility.Visible;
        //}
    }

WebBrowser.xaml.cs

//Flag to check if the browser is navigating
    public bool _IsNavigating = false;

void TheWebBrowser_Navigating(object sender,
        Microsoft.Phone.Controls.NavigatingEventArgs e)
    {
        _IsNavigating = true;
    }

    void TheWebBrowser_Navigated(object sender,
        System.Windows.Navigation.NavigationEventArgs e)
    {
        _IsNavigating = false;
    }

我的 WebBrowser.xaml.cs 是嵌入到 MainPage.xaml 并命名为 TheBrowser 的 webbrowser 用户控件。调试时,我可以看到 bool 变量 _IsNavigating 在 true 和 false 之间的变化,但在 MainPage.xaml.cs 中没有检测到这种变化,这就是我认为按钮可见性没有变化的原因。任何有关此问题的帮助将不胜感激。我已经没有如何解决这个问题的想法了!提前致谢。

4

1 回答 1

1

设置可见性的代码放错地方了,现在只在页面构建时调用。

每当您的_isNavigating变量更改时,您都必须调用代码。

请执行下列操作:

 void ChangeVisibility()
 {
    if (TheBrowser._IsNavigating == false)
    {
        RefreshButton.Visibility = Visibility.Visible;
        StopButton.Visibility = Visibility.Collapsed;
    }
    else
    {
        RefreshButton.Visibility = Visibility.Collapsed;
        StopButton.Visibility = Visibility.Visible;
    }
  }

void TheWebBrowser_Navigating(object sender,
    Microsoft.Phone.Controls.NavigatingEventArgs e)
{
    _IsNavigating = true;
    ChangeVisibility();
}

void TheWebBrowser_Navigated(object sender,
    System.Windows.Navigation.NavigationEventArgs e)
{
    _IsNavigating = false;
    ChangeVisibility();
}
于 2012-04-04T21:40:52.970 回答