#

UWP: Eine Text-Datei in einen Text-Editor laden

 

Der folgende C# Code zeigt, wie man eine Text-Datei in ein ein Anzeigeelement unter UWP Universal Windows Platform App lädt.

Hierzu benötigt man ein RichEdit Text Control

<RichEditBox x:Name="ctlEdit_Text" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ></RichEditBox>

 

Anschliessend öffnet man unter der Code-Behind seite in C# die Datei als Storagefile und hieraus einen Filestream.

Diesen Filestream kann man direkt dem RichTextEditor mit LoadFromSteam(..) übergeben

//< get File >

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(this.BaseUri, sFilename ));

//</ get File >

 

//< open file >

Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

//</ open file >

 

//< load File into Text-Editor >

ctlEdit_Text.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, fileStream);

//</ load File into Text-Editor >

 

 

 

 

Video Anleitung:

 

 

C# Sample Code zum Laden der Datei in das TextEditor Control

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;

using Windows.Foundation.Collections;

//< add namespaces >

using Windows.Storage;      //*file access

using Windows.UI.Popups;    //*Messagebox

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

//</ add namespaces >

 

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

 

namespace uwp_Advertiser

{

 

    public sealed partial class TextPage : Page

    {

#region page

        public TextPage()

        {

            this.InitializeComponent();

        }

        #endregion /page

 

        #region Buttons

        private void btnBack_Click(object sender, RoutedEventArgs e)

        {

            //----< btnBack_Click() >----

            //*navigate back to mainpage

            this.Frame.GoBack();

            //----</ btnBack_Click() >----

        }

 

 

        private  void btnLoad_XAML_Click(object sender, RoutedEventArgs e)

        {

            //load xaml file

            load_RichText_Editor("_files/Code_AdControl.xaml.txt");

        }

 

        private void btnLoad_C_Code_Click(object sender, RoutedEventArgs e)

        {

            //load C# File

            load_RichText_Editor("_files/Code_AdControl.xaml.cs.txt");

        }

        #endregion /Buttons

 

 

        private async void load_RichText_Editor(string sFilename)

        {

            //----< load_RichText_Editor() >----

            //*load text-file into a RichText Editor

 

            //< get File >

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(this.BaseUri, sFilename ));

            //</ get File >

 

            if (file != null)

            {

                try

                {

                    //< open file >

                    Windows.Storage.Streams.IRandomAccessStream randAccStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                    //</ open file >

 

                    //< load File into Text-Editor >

                    ctlEdit_Text.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);

                    //</ load File into Text-Editor >

                }

                catch (Exception)

                {

                    MessageDialog dialog = new MessageDialog("could not load the file", "Load File into Text Editor");

                    await dialog.ShowAsync();

                }

            }

 

            //----</ load_RichText_Editor() >----

        }

    }

}

 

 

 

Und in XAML

<Page

    x:Class="uwp_Advertiser.TextPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:uwp_Advertiser"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid.RowDefinitions>

            <RowDefinition Height="70"/>

            <RowDefinition Height="889*"/>

        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal" >

            <Button x:Name="btnBack" Click="btnBack_Click" VerticalAlignment="Stretch" >Back</Button>

            <Button x:Name="btnLoad_XAML" Click="btnLoad_XAML_Click" VerticalAlignment="Stretch"  Margin="10,0,0,0">Load XAML</Button>

            <Button x:Name="btnLoad_C_Code" Click="btnLoad_C_Code_Click" VerticalAlignment="Stretch"  Margin="10,0,0,0">Load C# Code</Button>

        </StackPanel>

       

        <RichEditBox x:Name="ctlEdit_Text" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ></RichEditBox>

    </Grid>

</Page>

 

 

Mobile

.

0123movie.net