ravi

ravi

  • NA
  • 13
  • 0

WPF Xaml hyperlink's RequestNavigate event handler not found

May 31 2016 1:25 AM
I have a WPF user control with its code behind file containing the initialization stuff.
I am using the user control as an activeX component inside a different application. 
XAML for the control is constructed within the other application and is applied to the user control programatically.
 
Hyperlinks within the XAML are not working and I found that I need to add the RequestNavigate event handler to get the hyperlink working.
 
I have added the required event handler inside the code behind and added an x:Class attribute to XAML with the namespace and class name.
I get an error message that the RequestNavigate event handler is not found.
 
'Windows.Forms.Integration.WpfControl' does not contain a definition for 'Hyperlink_RequestNavigate' and no extension method 'Hyperlink_RequestNavigate' accepting a first argument of type 'Windows.Forms.Integration.WpfControl' could be found (are you missing a using directive or an assembly reference?) 
 
Can anyone please help?
 
Here is my code behind:
 
  1. namespace Windows.Forms.Integration  
  2. {  
  3.     public partial class WpfControl : UserControl  
  4.     {  
  5.         internal const string PREFIX = "WPFControl-";  
  6.         private string mstrXamlFile, mstrXaml;  
  7.         private bool mblnInitialized;  
  8.         private string mstrUid;  
  9.   
  10.         private static IDictionary<string, WpfControl> sobjWPFControls = new Dictionary<string, WpfControl>(StringComparer.OrdinalIgnoreCase);  
  11.         private static IDictionary<string, ObjectInstance> sobjObjectInstances = new Dictionary<string, ObjectInstance>(StringComparer.OrdinalIgnoreCase);  
  12.   
  13.         public string ErrorMessage { getset; }  
  14.   
  15.         public static string WpfControlAssemblyVersion { get { return AssemblyInfo.VersionNumber; } }  
  16.         public WpfControl()  
  17.         {  
  18.             InitializeComponent();  
  19.             mstrUid = PREFIX + Guid.NewGuid().ToString();  
  20.             sobjWPFControls.Add(mstrUid, this);  
  21.             InitializeXaml();  
  22.               
  23.             mblnInitialized = true;  
  24.             lblErrorMessage.Text = ErrorMessage;  
  25.         }  
  26.   
  27.         public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)  
  28.         {  
  29.             Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));  
  30.             e.Handled = true;  
  31.         }  
  32.           
  33.         private void InitializeXaml()  
  34.         {  
  35.             System.Windows.UIElement objUIElement = null;  
  36.             lblErrorMessage.Visible = false;  
  37.             try  
  38.             {  
  39.                 if (!string.IsNullOrEmpty(mstrXamlFile))  
  40.                     mstrXaml = File.ReadAllText(mstrXamlFile);  
  41.                 if (!string.IsNullOrEmpty(mstrXaml))  
  42.                     objUIElement = XamlReader.Parse(mstrXaml) as System.Windows.UIElement;  
  43.             }  
  44.             catch (XamlParseException exc)  
  45.             {  
  46.                   
  47.             }  
  48.              
  49.   
  50.         }  
  51.   
  52.          
  53.   
  54.     }  
  55.   
  56.       
  57. }  
Here is a sample XAML:
 
  1. <UserControl  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  5.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  6.         xmlns:system="clr-namespace:System;assembly=mscorlib"  
  7.         xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
  8.         xmlns:ti="clr-namespace:Windows.Forms.Integration;assembly=Windows.Forms.Integration"  
  9.         xmlns:inlineImage="clr-namespace:Windows.Forms.Integration;assembly=Windows.Forms.Integration"  
  10.         x:Class="Windows.Forms.Integration.WpfControl"  
  11.         mc:Ignorable="d"  
  12.         Background="White">  
  13.     <UserControl.Resources>  
  14.         <RadialGradientBrush x:Key="LightGrayBackground" GradientOrigin="0.2,0.2" RadiusX="1" RadiusY="1">  
  15.             <GradientStop Color="White"  Offset="0"/>  
  16.             <GradientStop Color="Gray" Offset="1"/>  
  17.         </RadialGradientBrush>  
  18.         <SolidColorBrush x:Key="MarketColor" Color="#FF6BBE6B"/>  
  19.         <SolidColorBrush x:Key="BidColor" Color="LightBlue"/>  
  20.         <SolidColorBrush x:Key="DesignColor" Color="#FF7C7CE2"/>  
  21.         <SolidColorBrush x:Key="BuildColor" Color="#FFCB94CB"/>  
  22.         <SolidColorBrush x:Key="InstallColor" Color="LightCoral"/>  
  23.         <SolidColorBrush x:Key="ServiceColor" Color="LightSalmon"/>  
  24.         <SolidColorBrush x:Key="ActivityBackground" Color="White"/>  
  25.   
  26.         <ControlTemplate x:Key="TitleLabel" TargetType="Label">  
  27.             <Grid Margin="4">  
  28.                 <Rectangle Stroke="Blue" StrokeThickness="0" RadiusX="4" RadiusY="4" Fill="{TemplateBinding Background}">  
  29.                     <Rectangle.Effect>  
  30.                         <DropShadowEffect />  
  31.                     </Rectangle.Effect>  
  32.                 </Rectangle>  
  33.                 <Label Content="{TemplateBinding Content}" Foreground="White" Margin="4" HorizontalAlignment="Center" FontSize="16" />  
  34.             </Grid>  
  35.         </ControlTemplate>  
  36.   
  37.         <ControlTemplate x:Key="ProcessExpander" TargetType="Expander">  
  38.             <Grid Margin="4">  
  39.                 <Rectangle RadiusX="4" RadiusY="4" Fill="{TemplateBinding Background}">  
  40.                     <Rectangle.Effect>  
  41.                         <DropShadowEffect />  
  42.                     </Rectangle.Effect>  
  43.                 </Rectangle>  
  44.                 <Expander Content="{TemplateBinding Content}">  
  45.                     <Expander.Header>  
  46.                         <Label Content="{TemplateBinding Header}" HorizontalAlignment="Center" FontSize="14" />  
  47.                     </Expander.Header>  
  48.                 </Expander>  
  49.             </Grid>  
  50.         </ControlTemplate>  
  51.   
  52.         <ControlTemplate x:Key="ActivityLabel" TargetType="Label">  
  53.             <Border Background="White" Margin="4,1,4,1" Opacity="0.7">  
  54.                 <TextBlock Text="{TemplateBinding Content}" TextWrapping="WrapWithOverflow" Margin="1" TextAlignment="Center" />  
  55.             </Border>  
  56.         </ControlTemplate>  
  57.   
  58.         <ControlTemplate x:Key="ActivityLinkLabel" TargetType="Label">  
  59.             <Border Margin="2,2,2,2">  
  60.                 <TextBlock VerticalAlignment="Center" Text="{TemplateBinding Content}" TextWrapping="Wrap" Margin="1" TextAlignment="Center" FontSize="2.5">  
  61.                     <interactivity:Interaction.Behaviors>  
  62.                         <ti:NavigateBehavior Name="{Binding Path=Name, RelativeSource={RelativeSource TemplatedParent}}" Url="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" />  
  63.                     </interactivity:Interaction.Behaviors>  
  64.                 </TextBlock>  
  65.             </Border>  
  66.         </ControlTemplate>  
  67.   
  68.     </UserControl.Resources>  
  69.     <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">  
  70.         <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">  
  71.             <TextBlock Margin="4,10,4,4" Text="3.3.1.3 Generate sales forecast - A (10136) 3" TextWrapping="Wrap" FontWeight="Bold" FontSize="16" Foreground="#5F5FAF" />  
  72.             <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Width="Auto">  
  73.                 <FlowDocument  TextAlignment="Left" FontFamily'Verdana' FontSize'11' LineHeight'5' PageWidth'1000'>  
  74.                     <Section xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">  
  75.                         <Paragraph>  
  76.                             <Hyperlink NavigateUri="https://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">test url</Hyperlink>  
  77.                         </Paragraph>  
  78.                     </Section>  
  79.                 </FlowDocument>  
  80.             </FlowDocumentScrollViewer>  
  81.             <Viewbox VerticalAlignment="Stretch" HorizontalAlignment="Stretch">  
  82.                 <Grid HorizontalAlignment="Stretch">  
  83.                     <Grid.RowDefinitions>  
  84.                         <RowDefinition Height="Auto" />  
  85.                     </Grid.RowDefinitions>  
  86.                     <Grid.ColumnDefinitions>  
  87.                         <ColumnDefinition Width="*" />  
  88.                     </Grid.ColumnDefinitions>  
  89.                     <StackPanel Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">  
  90.                         <Canvas Grid.Row="1" Grid.Column="0" Margin="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="0.00" Height="0.00">  
  91.                             <Canvas.LayoutTransform>  
  92.                                 <ScaleTransform ScaleX="3.50" ScaleY="3.50" />  
  93.                             </Canvas.LayoutTransform>  
  94.                         </Canvas>  
  95.                         <TextBlock Margin="4" Width="Auto" Text="Version 1.0 | Status Open | Rendered 5/31/2016 07:09:24 am. Click Refresh to update the view." TextWrapping="Wrap" FontSize="12" TextAlignment="Left" />  
  96.                     </StackPanel>  
  97.                 </Grid>  
  98.             </Viewbox>  
  99.         </StackPanel>  
  100.     </ScrollViewer>  
  101. </UserControl>  
 
 
 
 

Answers (2)