切换按钮可见性不起作用
本文关键字:不起作用 可见性 按钮 | 更新日期: 2023-09-27 18:30:16
我有一个使用简单向前和向后导航方案的网络浏览器,如果用户选择,也可以刷新或停止导航。所有导航都正常工作,但是当我尝试切换"刷新"和"停止"按钮时,我的问题会出现,具体取决于网络浏览器是导航还是已导航。我希望只要没有导航,刷新按钮就设置为可见,并且在导航发生时将停止按钮设置为可见。我的基本实现如下,但我似乎无法根据这些情况让按钮从可见切换到折叠。
MainPage.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"/>
主页.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;
}
My WebBrowser.xaml.cs 是一个嵌入到 MainPage.xaml 中的 Web 浏览器用户控件,并命名为 TheBrowser。调试时,我可以看到布尔变量的变化_IsNavigating true 和 false 之间,如上所示,但在 MainPage.xaml 中未检测到此更改.cs这就是为什么我认为按钮可见性不会改变。对此问题的任何帮助将不胜感激。我已经没有了如何解决这个问题的想法!提前谢谢。
设置可见性的代码在错误的位置,现在仅在页面构建时调用它。
每当_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();
}