Windows 10 Universal Windows Platform – Rich Editor

Step 1

Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.

10-home

Step 2

Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.

10-file-new-project

Step 3

From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.

10-new-project

Step 4

Once done select from the Menu, Project, then Add New Item…

10-project-add-new-item

Step 5

From the Add New Item window select Visual C# 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

10-add-new-item-library

Step 6

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Globalization;

public class Library
{
    private void focus(ref RichEditBox display)
    {
        display.Focus(FocusState.Keyboard);
    }

    private void set(ref RichEditBox display, string value)
    {
        display.Document.SetText(TextSetOptions.FormatRtf, value);
        focus(ref display);
    }

    public string get(ref RichEditBox display)
    {
        string value = string.Empty;
        display.Document.GetText(TextGetOptions.FormatRtf, out value);
        return value;
    }

    public async Task<bool> Confirm(string content, string title, string ok, string cancel)
    {
        bool result = false;
        MessageDialog dialog = new MessageDialog(content, title);
        dialog.Commands.Add(new UICommand(ok, new UICommandInvokedHandler((cmd) => result = true)));
        dialog.Commands.Add(new UICommand(cancel, new UICommandInvokedHandler((cmd) => result = false)));
        await dialog.ShowAsync();
        return result;
    }

    public bool Bold(ref RichEditBox display)
    {
        display.Document.Selection.CharacterFormat.Bold = FormatEffect.Toggle;
        focus(ref display);
        return display.Document.Selection.CharacterFormat.Bold.Equals(FormatEffect.On);
    }

    public bool Italic(ref RichEditBox display)
    {
        display.Document.Selection.CharacterFormat.Italic = FormatEffect.Toggle;
        focus(ref display);
        return display.Document.Selection.CharacterFormat.Italic.Equals(FormatEffect.On);
    }

    public bool Underline(ref RichEditBox display)
    {
        display.Document.Selection.CharacterFormat.Underline =
            display.Document.Selection.CharacterFormat.Underline.Equals(UnderlineType.Single) ?
            UnderlineType.None : UnderlineType.Single;
        display.Document.Selection.CharacterFormat.Italic = FormatEffect.Toggle;
        focus(ref display);
        return display.Document.Selection.CharacterFormat.Underline.Equals(UnderlineType.Single);
    }

    public bool Left(ref RichEditBox display)
    {
        display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Left;
        focus(ref display);
        return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Left);
    }

    public bool Centre(ref RichEditBox display)
    {
        display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        focus(ref display);
        return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Center);
    }

    public bool Right(ref RichEditBox display)
    {
        display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Right;
        focus(ref display);
        return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Right);
    }

    public void Size(ref RichEditBox display, ref ComboBox value)
    {
        if (display != null && value != null)
        {
            string selected = ((ComboBoxItem)value.SelectedItem).Tag.ToString();
            display.Document.Selection.CharacterFormat.Size = float.Parse(selected);
            focus(ref display);
        }
    }

    public void Colour(ref RichEditBox display, ref ComboBox value)
    {
        if (display != null && value != null)
        {
            string selected = ((ComboBoxItem)value.SelectedItem).Tag.ToString();
            display.Document.Selection.CharacterFormat.ForegroundColor = Color.FromArgb(
                Byte.Parse(selected.Substring(0, 2), NumberStyles.HexNumber),
                Byte.Parse(selected.Substring(2, 2), NumberStyles.HexNumber),
                Byte.Parse(selected.Substring(4, 2), NumberStyles.HexNumber),
                Byte.Parse(selected.Substring(6, 2), NumberStyles.HexNumber));
            focus(ref display);
        }
    }

    public async void New(RichEditBox display)
    {
        if (await Confirm("Create New Document?", "Rich Editor", "Yes", "No"))
        {
            set(ref display, string.Empty);
        }
    }

    public async void Open(RichEditBox display)
    {
        try
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            picker.FileTypeFilter.Add(".rtf");
            StorageFile file = await picker.PickSingleFileAsync();
            set(ref display, await FileIO.ReadTextAsync(file));
        }
        catch
        {

        }
    }

    public async void Save(RichEditBox display)
    {
        try
        {
            FileSavePicker picker = new FileSavePicker();
            picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            picker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" });
            picker.DefaultFileExtension = ".rtf";
            picker.SuggestedFileName = "Document";
            StorageFile file = await picker.PickSaveFileAsync();
            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                await FileIO.WriteTextAsync(file, get(ref display));
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }
        catch
        {

        }
    }
}

It should then appear as such:

10-library-richeditor

Step 7

From the Solution Explorer select MainPage.xaml

10-mainpage

Step 8

Select from the Menu, View then Designer

10-designer

Step 9

The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:

<Page.BottomAppBar>
	<AppBar IsOpen="True" IsSticky="True">
		<StackPanel Orientation="Horizontal">
			<AppBarButton Name="New" Icon="Page" Label="New" Click="New_Click"/>
			<AppBarButton Name="Open" Icon="Folder" Label="Open" Click="Open_Click"/>
			<AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/>
		</StackPanel>
	</AppBar>
</Page.BottomAppBar>

While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<StackPanel Grid.Row="0" Orientation="Horizontal">
		<AppBarToggleButton Name="Bold" Icon="Bold" Label="Bold" Click="Bold_Click"/>
		<AppBarToggleButton Name="Italic" Icon="Italic" Label="Italic" Click="Italic_Click"/>
		<AppBarToggleButton Name="Underline" Icon="Underline" Label="Underline" Click="Underline_Click"/>
		<ComboBox Name="Size" VerticalAlignment="Center" SelectionChanged="Size_SelectionChanged">
			<ComboBoxItem Content="8" Tag="8"/>
			<ComboBoxItem Content="10" Tag="10"/>
			<ComboBoxItem Content="12" Tag="12"/>
			<ComboBoxItem Content="16" Tag="16" IsSelected="True"/>
			<ComboBoxItem Content="20" Tag="20"/>
			<ComboBoxItem Content="24" Tag="24"/>
			<ComboBoxItem Content="28" Tag="28"/>
			<ComboBoxItem Content="36" Tag="36"/>
			<ComboBoxItem Content="48" Tag="48"/>
			<ComboBoxItem Content="72" Tag="72"/>
		</ComboBox>
	</StackPanel>
	<StackPanel Grid.Row="1" Orientation="Horizontal">
		<AppBarToggleButton Name="Left" Icon="AlignLeft" Label="Left" Click="Left_Click"/>
		<AppBarToggleButton Name="Centre" Icon="AlignCenter" Label="Centre" Click="Centre_Click"/>
		<AppBarToggleButton Name="Right" Icon="AlignRight" Label="Right" Click="Right_Click"/>
		<ComboBox Name="Colour" VerticalAlignment="Center" SelectionChanged="Colour_SelectionChanged">
			<ComboBoxItem Tag="FF000000" IsSelected="True">
				<Rectangle Width="25" Height="14" Fill="Black"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF808080">
				<Rectangle Width="25" Height="14" Fill="Gray"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF0000">
				<Rectangle Width="25" Height="14" Fill="Red"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFA500">
				<Rectangle Width="25" Height="14" Fill="Orange"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFFFF00">
				<Rectangle Width="25" Height="14" Fill="Yellow"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF008000">
				<Rectangle Width="25" Height="14" Fill="Green"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF00FFFF">
				<Rectangle Width="25" Height="14" Fill="Cyan"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF0000FF">
				<Rectangle Width="25" Height="14" Fill="Blue"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FFFF00FF">
				<Rectangle Width="25" Height="14" Fill="Magenta"/>
			</ComboBoxItem>
			<ComboBoxItem Tag="FF800080">
				<Rectangle Width="25" Height="14" Fill="Purple"/>
			</ComboBoxItem>
		</ComboBox>
	</StackPanel>
	<RichEditBox Grid.Row="2" AcceptsReturn="True" TextWrapping="Wrap" VerticalAlignment="Stretch" FontFamily="Segoe UI" FontSize="16" Name="Display"/>
</Grid>

It should appear as such:

10-xaml-richeditor

Step 10

Select from the Menu, View then Code

10-code

Step 11

Once in the Code View below the public MainPage() { … } the following should be entered:

public Library Library = new Library();

private void Bold_Click(object sender, RoutedEventArgs e)
{
	Bold.IsChecked = Library.Bold(ref Display);
}

private void Italic_Click(object sender, RoutedEventArgs e)
{
	Italic.IsChecked = Library.Italic(ref Display);
}

private void Underline_Click(object sender, RoutedEventArgs e)
{
	Underline.IsChecked = Library.Underline(ref Display);
}

private void Left_Click(object sender, RoutedEventArgs e)
{
	Left.IsChecked = Library.Left(ref Display);
	Centre.IsChecked = false;
	Right.IsChecked = false;
}

private void Centre_Click(object sender, RoutedEventArgs e)
{
	Left.IsChecked = false;
	Centre.IsChecked = Library.Centre(ref Display);
	Right.IsChecked = false;
}

private void Right_Click(object sender, RoutedEventArgs e)
{
	Left.IsChecked = false;
	Centre.IsChecked = false;
	Right.IsChecked = Library.Right(ref Display);
}

private void Size_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	Library.Size(ref Display, ref Size);
}

private void Colour_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
	Library.Colour(ref Display, ref Colour);
}

private void New_Click(object sender, RoutedEventArgs e)
{
	Library.New(Display);
}

private void Open_Click(object sender, RoutedEventArgs e)
{
	Library.Open(Display);
}

private void Save_Click(object sender, RoutedEventArgs e)
{
	Library.Save(Display);
}

It should then appear as such:

10-code-richeditor

Step 12

That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application

10-simulator

Step 13

Once the Simulator has started the Application should then appear

10-simulator-run-richeditor

Step 14

After the Application has started running you can then type in any text then you can Save the document to Open later or start a New document you can also make text Bold, Italic, Underline change the text alignment, font size and colour

10-simulator-ran-richeditor

Step 15

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Step 16

Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application

10-emulator

Step 17

Once the Emulator has started the Application should then appear

10-emulator-run-richeditor

Step 18

After the Application has started running you can then type in any text then you can Save the document to Open later or start a New document you can also make text Bold, Italic, Underline change the text alignment, font size and colour

10-emulator-ran-richeditor

Step 19

To Exit the application select Stop Debugging in Visual Studio Community 2015

10-stop

Creative Commons License

Advertisements

3 thoughts on “Windows 10 Universal Windows Platform – Rich Editor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s