【问题标题】:Prevent user entered data from clearing on new window load防止用户输入的数据在新窗口加载时清除
【发布时间】:2019-09-12 16:35:36
【问题描述】:

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

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

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

感谢您的帮助!

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

代码分解:

主屏幕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());
        }
    }
}

page1 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>

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

page3 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>

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

【问题讨论】:

  • 我建议使用 MVVM,尽管它是一个非常小且简单的宏类型应用程序。如果您想在不修复架构的情况下解决您的问题,如果您发布 reproducible example,您将获得更好的结果。我不知道你的应用是做什么的,也不知道它是怎么做的。
  • 谢谢埃德。我在原文中添加了一个简单的应用程序细分,希望对您有所帮助。
  • 请看一下您的问题现在是如何进行格式化的。您是说当您创建test.page3 的新实例时,您希望它具有与某个先前实例相同的状态吗?我建议编写视图模型。 Here's a basic MVVM tutorial 用于 UWP。该设计并不特别适用于您的问题,但它说明了如何将您的 数据 放在视图模型中,并且可以在适当视图的任何实例中重新显示。

标签: c# uwp


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    正如@Ed_Plunkett 所说,如果你想保存用户输入的状态,你可以使用MVVM。简单来说,就是将用户的输入保存到一个变量中,供下次页面加载使用。

    但就您当前的代码而言,Page 最好使用 Frame 进行导航。如果需要直接在Page中添加一些控件,可以创建UserControl

    这是关于UserControldocument

    这是关于Navigationdocument

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多