简体   繁体   English

暂停或关闭应用时,从TextBox写入.txt文件

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

There is a TextBox where user can enter some data. 有一个TextBox ,用户可以在其中输入一些数据。 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. 当应用程序关闭时(通过单击窗口右上角的X按钮),应用程序需要获取TextBox中键入的内容并将其保存到.txt文件,然后再关闭应用程序。

This is my code in App.xaml.cs file. 这是我在App.xaml.cs文件中的代码。

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. 我在try块中的textBox下面得到一个红色下划线。

It says, 它说,

The name 'textBox' does not exist in the current context 当前上下文中不存在名称“textBox”

I'm guessing it's not possible to access the TextBox control from App.xaml.cs file. 我猜测无法从App.xaml.cs文件访问TextBox控件。

in App.xaml.cs 在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) 在Main.xaml.cs中(TextBox_1 => xaml文件中TextBox的名称)

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. 如果主框架中的文本框,您可以使用App.xaml.cs中的rootFrame访问。 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 编辑:您可以在C:\\Users\\{UserName}\\AppData\\Local\\Packages\\{PackageName}\\LocalState\\hello.txt查看您的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM