2013-08-30 40 views
0

我有一個HyperlinkBut​​ton。當我點擊它時,它會啓動帶有鏈接的互聯網瀏覽器,因爲他應該這樣做。點擊後取消HyperlinkBut​​ton事件

我想在某些情況下取消這個HyperlinkBut​​ton事件。

例如:在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!!!!!!!! 
    } 
} 

那麼,如何取消HyperlinkBut​​ton事件後點擊?

回答

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

+0

+1供參考。 – Jonast92

0

你可以使用一個return語句。

return; 

也就是說,

if (condition) 
{ 
    return; 
} 
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(); 
    } 
} 
相關問題