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

标签 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());
        }
    }
}

第 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();
        }
    }
}

第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();
        }
    }
}

最佳答案

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

关于c# - 防止用户输入的数据在新窗口加载时被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57911137/

相关文章:

c# - 如何在 MonoDevelop/.Net 中自动生成单元测试?

c# - UWP Storyboard 和 CallMethodAction 的仅发布异常(exception)

c# - 无法激活 Windows 应用商店应用程序。激活请求失败,错误为 'Access is denied'

c# - 可用 UWP 菜单控件之间的差异

c# - 为什么在资源字符串中放入\t​​ 不起作用

c# - 内联 block 似乎不起作用

c# - c#、mysql中的日期

c# - 在正常关闭时处理 ServiceBus QueueClient 中预取的消息

c++ - 在 UWP 中使用自定义视频效果

c# - 如何从C++ UWP App引用C#UWP类库