单击后取消HyperlinkBut​​ton事件

时间:2013-08-30 10:02:14

标签: c# windows-runtime winrt-xaml

我有一个HyperlinkBut​​ton。当我点击它时,它会启动互联网浏览器,并在其中显示链接。

我想在某些条件下取消此HyperlinkBut​​ton事件。

例如:

  • 用户点击超链接按钮
  • app检查互联网连接
  • 如果没有互联网连接,请不要启动互联网浏览器, 留在应用程序

示例代码(类似的东西):

<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!!!!!!!!
    }
}

那么,如何在点击后取消HyperlinkBut​​ton事件呢?

3 个答案:

答案 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.

Further Reference

答案 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();
    }
}