July 8, 2013

WPF Hyperlink Sample

Here are a couple of examples
WPF Application Hyperlink (in this case handle the RequestNavigate)
<TextBlock>Files found using the given <Hyperlink 
private void HyperlinkRequestNavigate(object sender, RequestNavigateEventArgs e)
   // What to do with a hyperlink navigation request?
   // If it is an internet URI this will invoke the default browser
   // Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));

   // Otherwise show a dialog or whatever you want
   e.Handled = true;
Also check this out this link for a reusable hyperlink navigation routine using dependent properties.

How about using a WPF hyperlink in MVVM. Here is how it is done.
In the XAML add the textblock with the hyperlink and bind it to a command ('AboutCommand' in this case). Note in this sample the text of the hyperlink is also bound to some property 'Version'
    <Hyperlink Command="{Binding AboutCommand}"><TextBlock Text="{Binding Version}" /></Hyperlink>
The command is implemented in the ViewModel
private RelayCommand aboutCommand;

public RelayCommand AboutCommand
  return aboutCommand ?? (aboutCommand = new RelayCommand(ExecuteAboutCommand));

private void ExecuteAboutCommand()
 IAboutDialogService aboutDialogService = ServiceLocator.Current.GetInstance<IAboutDialogService>();

Monotonic Variable

Made this definition up myself. This is effectively a one way variable that starts off in one state and switches to another alternate state. It always starts off in a known state and during the period when it can be accessed by multiple threads it can only be switched to the other alternate state. This has repercussions for thread safe access to the variable with .NET. In certain cases, it can be that none is required. TODO: Explain in more detail.

Generally I use a boolean for my monotonic variable but it is not necessarily a boolean, I have used a reference type in a monotonic manner where the variable has a null reference to begin with, but at some point the reference is assigned.