EsAMe
EsAMe

Reputation: 305

Writing to a .txt file from TextBox when suspending or closing app

There is a TextBox where user can enter some data. When the app is closed (by clicking the X button on top right of the window), the app needs to take whatever was typed in the TextBox and save it to a .txt file before closing the app.

This is my code in App.xaml.cs file.

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    //TODO: Save application state and stop any background activity
    await WriteTextToFile();

    deferral.Complete();
}

private async Task WriteTextToFile()
{
    try
    {
        string text = textBox.Text;
        StorageFile textFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///TextFile1.txt"));
        await FileIO.WriteTextAsync(textFile, text);
    }
    catch
    {

    }
}

I am getting a red underline under textBox in the try block.

It says,

The name 'textBox' does not exist in the current context

I'm guessing it's not possible to access the TextBox control from App.xaml.cs file.

Upvotes: 3

Views: 177

Answers (1)

OMANSAK
OMANSAK

Reputation: 1332

in App.xaml.cs

    private MainPage mainFrame;
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            rootFrame.NavigationFailed += OnNavigationFailed;
            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }
            Window.Current.Content = rootFrame;
        }

        if (e.PrelaunchActivated == false)
        {
            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
                mainFrame = (MainPage) rootFrame.Content; //*
            }
            Window.Current.Activate();
        }
    }

    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        string text = mainFrame.Main_TextBox.Text;
        string text2 = MainPage.rootPage.Main_TextBox.Text;
        Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        Windows.Storage.StorageFile sampleFile = await storageFolder.CreateFileAsync("hello.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
        await Windows.Storage.FileIO.WriteTextAsync(sampleFile , text);
        deferral.Complete();
    }

in Main.xaml.cs (TextBox_1 => Name of TextBox in xaml file)

public sealed partial class MainPage : Page
{
    public TextBox Main_TextBox => TextBox_1; //*
    public static MainPage rootPage; //*
    public MainPage()
    {
        this.InitializeComponent();
        if (rootPage == null)
        {
            rootPage = this;
        }
    }

}

you can access with rootFrame in App.xaml.cs if textbox in main frame. other way you can create a static variable in your page then you can access with any property

Edit : You can see your file in C:\Users\{UserName}\AppData\Local\Packages\{PackageName}\LocalState\hello.txt

Upvotes: 1

Related Questions