我有一个HyperlinkButton。当我点击它时,它会启动互联网浏览器,并在其中显示链接。
我想在某些条件下取消此HyperlinkButton事件。
例如:
示例代码(类似的东西):
<Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid>
<HyperlinkButton NavigateUri="http://stackoverflow.com/" Content="GO TO WEBPAGE" Click="HyperlinkButton_Click_1" />
</Grid>
</Page>
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile == null || connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.LocalAccess || connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.None)
{
NO INTERNET, CANCEL THE EVENT!!!!!!!!
}
}
那么,如何在点击后取消HyperlinkButton事件呢?
答案 0 :(得分:1)
您可以使用return
关键字。
<强> MSDN 强>
The return statement terminates execution of the method in which it
appears and returns control to the calling method.
It can also return the value of the optional expression.
If the method is of the type void, the return statement can be omitted.
答案 1 :(得分:0)
您可以使用return语句。
return;
即,
if (condition)
{
return;
}
答案 2 :(得分:0)
您以错误的方式使用if
。你应该这样做。
private async void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile != null && connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
{
//TODO: open link
}
else
{
await new Windows.UI.Popups.MessageDialog("Internet is not available.").ShowAsync();
}
}