2011-08-17 13 views
1

私はWPFでデスクトップアプリケーションを構築しており、ブラウザでハイパーリンクを開きたいと考えています。私はこれを行うには、メソッドをコードの中に入れ、次のようにXAMLから呼び出しますが、このメソッドを複数のXAMLページから呼び出すにはどうすればよいですか?XAMLと異なる名前空間でメソッドを呼び出す方法

XAML

<Hyperlink NavigateUri="http://www.mylink.com" RequestNavigate="Hyperlink_RequestNavigate">My link text</Hyperlink> 

C#

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) 
    { 
     System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri)); 
     e.Handled = true; 
    } 

答えて

3

あなたは、例えば、App.xamlにスタイルにこれを入れることができます

<Application.Resources> 
    <Style x:Key="LaunchLinkStyle" TargetType="{x:Type Hyperlink}"> 
     <EventSetter Event="RequestNavigate" Handler="LaunchLinkStyle_RequestNavigate" /> 
    </Style> 
</Application.Resources> 

ハンドラは、その後、もちろんApp.xaml.csに実装されるだろう)

あなたはその後、スタイルだけを参照することができます。

<Hyperlink Style="{StaticResource LaunchLinkStyle}" ... /> 
0

おかげH.B.あなたの答えは正しい道に私を置いた。私のページで

<Hyperlink NavigateUri="http://www.mylink.com" Style="{StaticResource LaunchLinkStyle}">My Link</Hyperlink> 

App.xaml

<Style x:Key="LaunchLinkStyle" TargetType="{x:Type Hyperlink}"> 
     <EventSetter Event="RequestNavigate" Handler="LaunchLinkStyle_RequestNavigate"/> 
    </Style> 

App.xaml.cs

public void LaunchLinkStyle_RequestNavigate(object sender, RoutedEventArgs e) 
    { 
     /* Function loads URL in separate browser window. */ 
     Hyperlink link = e.OriginalSource as Hyperlink; 
     Process.Start(link.NavigateUri.AbsoluteUri); 
     e.Handled = true; //Set this to true or the hyperlink loads in application and browser windows 
    } 
ここでは、完全なコードです
関連する問題