C# 防止在新窗口加载时清除用户输入的数据

C# 防止在新窗口加载时清除用户输入的数据,c#,uwp,C#,Uwp,我有一个小的桌面应用程序,点击一个按钮就可以将许多窗口和页面加载到主屏幕的网格中。相关页面加载到网格中,然后用户在文本框、选定的无线电问题等中输入数据。然后,他们可以复制输入的数据并将其粘贴到需要粘贴的位置。简单的宏类型桌面应用程序 我的主要问题是当用户从一个加载的窗口单击并转到另一个宏,然后删除以前输入的所有数据。因此,如果用户返回到以前的宏,它将作为新宏重新加载 每个窗口/宏都有一个重置按钮,我最终希望保存所有数据,直到使用该按钮或关闭应用程序 谢谢你的帮助 更新:在下面添加了代码。具有主窗

我有一个小的桌面应用程序,点击一个按钮就可以将许多窗口和页面加载到主屏幕的网格中。相关页面加载到网格中,然后用户在文本框、选定的无线电问题等中输入数据。然后,他们可以复制输入的数据并将其粘贴到需要粘贴的位置。简单的宏类型桌面应用程序

我的主要问题是当用户从一个加载的窗口单击并转到另一个宏,然后删除以前输入的所有数据。因此,如果用户返回到以前的宏,它将作为新宏重新加载

每个窗口/宏都有一个重置按钮,我最终希望保存所有数据,直到使用该按钮或关闭应用程序

谢谢你的帮助

更新:在下面添加了代码。具有主窗口/登录页和网格的简单应用程序。单击按钮时,网格根据单击的按钮加载页面(第2页或第3页)。如果用户在文本框中输入文本,通过单击另一个按钮转到另一页,然后转到带有先前输入数据的按钮的页面

代码分类:

主屏幕xaml

<Page
    x:Class="test.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Width="1001">

    <Grid x:Name="loadgrid" Height="500" Background="White" Margin="423,110,61,110">
        <Button Content="Button" Margin="-298,29,0,0" VerticalAlignment="Top" Width="113" Click="Button_Click"/>
        <Button Content="Button" Margin="-298,97,0,0" VerticalAlignment="Top" Width="113" Click="Button_Click_1"/>
    </Grid>
</Page>

主屏幕CS

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
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;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            loadgrid.Children.Add(new test.BlankPage1());
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            loadgrid.Children.Add(new test.page3());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows基金会;
使用Windows。
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
//空白页项模板被记录在https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
名称空间测试
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
loadgrid.Children.Add(newtest.BlankPage1());
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
loadgrid.Children.Add(newtest.page3());
}
}
}
第1页xaml

<Page x:Name="page2"
    x:Class="test.BlankPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="#FFF57A7A" Width="400" Height="400">

    <Grid>
        <TextBox HorizontalAlignment="Center" Margin="0,53,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178" AllowFocusWhenDisabled="True"/>
        <TextBox HorizontalAlignment="Center" Margin="0,116,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178"/>
        <TextBox HorizontalAlignment="Center" Margin="0,168,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="178"/>

    </Grid>
</Page>

第1页cs

namespace test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class BlankPage1 : Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();
        }
    }
}
名称空间测试
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类空白第1页:第
{
公共网页1()
{
this.InitializeComponent();
}
}
}
第3页xaml

<Page x:Name="page1"
    x:Class="test.page3"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="#FF5C838F" Width="400" Height="400">

    <Grid>
        <TextBox HorizontalAlignment="Left" Margin="123,105,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>
        <TextBox HorizontalAlignment="Left" Margin="123,168,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>
        <TextBox HorizontalAlignment="Left" Margin="123,227,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="173"/>

    </Grid>
</Page>

第3页cs

namespace test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class page3 : Page
    {
        public page3()
        {
            this.InitializeComponent();
        }
    }
}
名称空间测试
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类第3页:第
{
公共网页3()
{
this.InitializeComponent();
}
}
}

正如@Ed_Plunkett所说,如果您想保存用户输入的状态,可以使用MVVM。简单地说,用户的输入被保存到一个变量中,以便下一页加载

但就您当前的代码而言,
页面
最好使用框架进行导航。如果需要将某些控件直接添加到页面,可以创建
UserControl

下面是关于
UserControl

以下是关于
导航的信息


致以最诚挚的问候。

您好,您可以在页面开始时启用页面缓存模式,它将存储整个页面缓存数据复选框数据。
this.NavigationCacheMode=Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled

我建议使用MVVM,尽管它是一个非常小且简单的宏类型应用程序。如果您想在不修复体系结构的情况下解决问题,那么如果您发布一个。我不知道你的应用程序是做什么的,也不知道它是如何做的。谢谢Ed。我在原始应用程序中添加了一个简单的应用程序分解,希望能有所帮助。请在你的问题中看看现在是如何进行格式设置的。您是说,当您创建
test.page3
的新实例时,您希望它具有与以前某个实例相同的状态吗?我建议您编写viewmodels。对于UWP。这种设计并不特别适用于您的问题,但它说明了如何在viewmodel中保存数据,并且可以在适当视图的任何实例中重新显示数据。