To start Im sorry if this is wrong place to post this and Im just starting to teach myself UWP !
So that being said I have made a text editor in UWP and it all works except that after I type some text in the RichEditBox and then click on any of my buttons on the right the text in the RichEditBox disappears but when I click back in RichEditBox its back. It has to be something I have learned yet. Can anyone help?
Here is my XAML ...
Code:
<Page x:Name="MainPage1"
x:Class="Decster.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Decster"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<SplitView x:Name="MySplitView" DisplayMode="CompactOverlay" IsPaneOpen="False"
CompactPaneLength="50" OpenPaneLength="150">
<SplitView.Pane>
<StackPanel Background="Gainsboro">
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click"/>
<StackPanel x:Name="FileStackPanel" Orientation="Horizontal">
<Button x:Name="btnFile" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" ToolTipService.ToolTip="File Menu">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="New" Click="MenuFlyoutItem_Click"/>
<MenuFlyoutItem Text="Open" Click="MenuFlyoutItem_Click_1"/>
<MenuFlyoutSeparator/>
<MenuFlyoutItem Text="Save" Click="MenuFlyoutItem_Click_2"/>
<MenuFlyoutItem Text="Save As ..."/>
<MenuFlyoutSeparator/>
<MenuFlyoutItem Text="Page Setup"/>
<MenuFlyoutItem Text="Print"/>
<MenuFlyoutSeparator/>
<MenuFlyoutItem Text="Exit"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<TextBlock Text="File" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
<StackPanel x:Name="EditStackPanel" Orientation="Horizontal">
<Button x:Name="btnEdit" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" ToolTipService.ToolTip="Edit Menu">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="Undo"/>
<MenuFlyoutItem Text="Redo"/>
<MenuFlyoutSeparator/>
<MenuFlyoutItem Text="Cut"/>
<MenuFlyoutItem Text="Copy"/>
<MenuFlyoutItem Text="Paste"/>
<MenuFlyoutSeparator/>
<MenuFlyoutItem Text="Select All"/>
<MenuFlyoutItem Text="Delete"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<TextBlock Text="Edit" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
<StackPanel x:Name="ViewStackPanel" Orientation="Horizontal">
<Button x:Name="btnView" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" ToolTipService.ToolTip="View Menu"/>
<TextBlock Text="View" FontSize="18" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
<Grid>
<RichEditBox x:Name="display" FontSize="21.333" Foreground="White" Margin="10,10,10,0" Height="620" VerticalAlignment="Top"/>
</Grid>
</SplitView.Content>
</SplitView>
</Page>
Here is my VB ...
Code:
Public NotInheritable Class MainPage
Inherits Page
Public Library As New Library()
Private Sub HamburgerButton_Click(sender As Object, e As RoutedEventArgs)
MySplitView.IsPaneOpen = Not MySplitView.IsPaneOpen
End Sub
Private Sub MenuFlyoutItem_Click(sender As Object, e As RoutedEventArgs)
Library.[New](display)
End Sub
Private Sub MenuFlyoutItem_Click_1(sender As Object, e As RoutedEventArgs)
Library.Open(display)
End Sub
Private Sub MenuFlyoutItem_Click_2(sender As Object, e As RoutedEventArgs)
Library.Save(display)
End Sub
End Class
And my Library.vb ...
Code:
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Provider
Imports Windows.UI
Imports Windows.UI.Popups
Imports Windows.UI.Text
Imports Windows.UI.Xaml
Imports Windows.UI.Xaml.Controls
Imports System.Globalization
Public Class Library
Private Sub focus(ByRef display As RichEditBox)
display.Focus(FocusState.Keyboard)
End Sub
Private Sub [set](ByRef display As RichEditBox, value As String)
display.Document.SetText(TextSetOptions.FormatRtf, value)
focus(display)
End Sub
Public Function [get](ByRef display As RichEditBox) As String
Dim value As String = String.Empty
display.Document.GetText(TextGetOptions.FormatRtf, value)
Return value
End Function
Public Async Function Confirm(content As String, title As String, ok As String, cancel As String) As Task(Of Boolean)
Dim result As Boolean = False
Dim dialog As New MessageDialog(content, title)
dialog.Commands.Add(New UICommand(ok, New UICommandInvokedHandler(Function(cmd) InlineAssignHelper(result, True))))
dialog.Commands.Add(New UICommand(cancel, New UICommandInvokedHandler(Function(cmd) InlineAssignHelper(result, False))))
Await dialog.ShowAsync()
Return result
End Function
Public Function Bold(ByRef display As RichEditBox) As Boolean
display.Document.Selection.CharacterFormat.Bold = FormatEffect.Toggle
focus(display)
Return display.Document.Selection.CharacterFormat.Bold.Equals(FormatEffect.[On])
End Function
Public Function Italic(ByRef display As RichEditBox) As Boolean
display.Document.Selection.CharacterFormat.Italic = FormatEffect.Toggle
focus(display)
Return display.Document.Selection.CharacterFormat.Italic.Equals(FormatEffect.[On])
End Function
Public Function Underline(ByRef display As RichEditBox) As Boolean
display.Document.Selection.CharacterFormat.Underline = If(display.Document.Selection.CharacterFormat.Underline.Equals(UnderlineType.[Single]), UnderlineType.None, UnderlineType.[Single])
display.Document.Selection.CharacterFormat.Italic = FormatEffect.Toggle
focus(display)
Return display.Document.Selection.CharacterFormat.Underline.Equals(UnderlineType.[Single])
End Function
Public Function Left(ByRef display As RichEditBox) As Boolean
display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Left
focus(display)
Return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Left)
End Function
Public Function Centre(ByRef display As RichEditBox) As Boolean
display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Center
focus(display)
Return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Center)
End Function
Public Function Right(ByRef display As RichEditBox) As Boolean
display.Document.Selection.ParagraphFormat.Alignment = ParagraphAlignment.Right
focus(display)
Return display.Document.Selection.ParagraphFormat.Alignment.Equals(ParagraphAlignment.Right)
End Function
Public Sub Size(ByRef display As RichEditBox, ByRef value As ComboBox)
If display IsNot Nothing AndAlso value IsNot Nothing Then
Dim selected As String = DirectCast(value.SelectedItem, ComboBoxItem).Tag.ToString()
display.Document.Selection.CharacterFormat.Size = Single.Parse(selected)
focus(display)
End If
End Sub
Public Sub Colour(ByRef display As RichEditBox, ByRef value As ComboBox)
If display IsNot Nothing AndAlso value IsNot Nothing Then
Dim selected As String = DirectCast(value.SelectedItem, ComboBoxItem).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(display)
End If
End Sub
Public Async Sub [New](display As RichEditBox)
If Await Confirm("Create New Document?", "Rich Editor", "Yes", "No") Then
[set](display, String.Empty)
End If
End Sub
Public Async Sub Open(display As RichEditBox)
Try
Dim picker As New FileOpenPicker()
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
picker.FileTypeFilter.Add(".rtf")
Dim file As StorageFile = Await picker.PickSingleFileAsync()
[set](display, Await FileIO.ReadTextAsync(file))
Catch
End Try
End Sub
Public Async Sub Save(display As RichEditBox)
Try
Dim picker As New FileSavePicker()
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
picker.FileTypeChoices.Add("Rich Text", New List(Of String)() From {
".rtf"
})
picker.DefaultFileExtension = ".rtf"
picker.SuggestedFileName = "Document"
Dim file As StorageFile = Await picker.PickSaveFileAsync()
If file IsNot Nothing Then
CachedFileManager.DeferUpdates(file)
Await FileIO.WriteTextAsync(file, [get](display))
Dim status As FileUpdateStatus = Await CachedFileManager.CompleteUpdatesAsync(file)
End If
Catch
End Try
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class