Content Link shows how to create an application which will show how to use Content Link with the RichEditBox Control
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using Windows.UI; using Windows.UI.Text; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Documents; using Windows.UI.Xaml.Media; public class Library { public delegate void NavigatedHandler(Uri uri); public event NavigatedHandler ContextLinkNavigated; private void ContentLinkInvoked(RichEditBox sender, ContentLinkInvokedEventArgs args) { ContextLinkNavigated?.Invoke(args.ContentLinkInfo.Uri); args.Handled = true; } public void Init(ref RichEditBox input) { input.ContentLinkInvoked += ContentLinkInvoked; input.ContentLinkBackgroundColor = new SolidColorBrush(Colors.Transparent); input.ContentLinkForegroundColor = new SolidColorBrush(Colors.Blue); input.ContentLinkProviders = new ContentLinkProviderCollection { new PlaceContentLinkProvider() }; } public void New(ref RichEditBox input, ref WebView display) { input.Document.SetText(TextSetOptions.FormatRtf, string.Empty); display.NavigateToString(string.Empty); } }
In the Code File for Library there are using statements to include the necessary functionality and in the Library Class there is a NavigatedHandler for ContextLinkNavigated and a ContentLinkInvoked Ebent Handler which will Invoke the ContextLinkNavigated Event. The Init Method sets up Properties and Event of the Content Link for the RichEditBox and set the ContentLinkProviderCollection to PlaceContentLinkProvider. The New Method is used to clear the RichEditBox and WebView.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in in this between the Grid and /Grid elements, enter the following XAML:
<Grid Margin="50"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*"/> <ColumnDefinition Width="50*"/> </Grid.ColumnDefinitions> <RichEditBox Name="Input" Grid.Column="0"/> <WebView Name="Display" Grid.Column="1"/> </Grid> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Page2" Label="New" Click="New_Click"/> </CommandBar>
Within the main Grid Element, the first block of XAML is an Grid Control with two Columns, the first is a RichEditBox and the second is a WebView. The second block of XAML is a CommandBar with AppBarButton for New which calls New_Click.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); protected override void OnNavigatedTo(NavigationEventArgs e) { library.Init(ref Input); library.ContextLinkNavigated += (Uri uri) => { Display.Navigate(uri); }; } private void New_Click(object sender, RoutedEventArgs e) { library.New(ref Input, ref Display); }
There is an OnNavigatedTo Event Handler which calls the Init Method in the Library Class and sets up the ContextLinkNavigated Event Handler to call the Navigate Method of the WebView with the given Uri and there is an New_Click Event Handler which will call the New Method in the Library Class.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
Once the Application has started running you can start typing into the RichEditBox then if you type an @ you can then type in the name of a Location whic you can then select to create the Content Link which can be tapped to display details of this in the WebView
Step 15
To Exit the Application select the Close button in the top right of the Application
Content Link is a simple application that shows how easy it is to use Content Link items that can used in a RichEditBox, it’s possible to use these for People and Places.