在加载之前,需要帮助将文本框值从第一页传递到第二页 [英] Need help passing textbox value from page one to page two before it loads

查看:58
本文介绍了在加载之前,需要帮助将文本框值从第一页传递到第二页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经接管了WPF应用程序,这是一个非常简单的应用程序,它允许用户输入一些数据,并更新站点的Web配置文件.(用于允许用户加密其数据)

I have taken over WPF application, very simple app that allows the user to enter some data and it updates the web config file of the site.(built to allow the user to encrypt their data)

因此,其设置方式如下,

So its setup in the following fashion,

MainWindow.xaml =它包含一个带有框架和导航按钮的窗口

MainWindow.xaml = this contains a window with a frame and navigation buttons

IIS.xaml =它包含一个页面,其中包含所需的字段和标签 对于配置的这一部分

IIS.xaml = this contains a page with the fields and labels needed  for this portion of the configuration

ConnString.xaml =这包含一个页面,其中包含配置的此部分所需的字段和标签

ConnString.xaml = this contains a page with the field and labels needed for this portion of the configuration

AppSetting.xaml =它包含一个页面,其中包含所需的字段和标签.对于配置的这一部分

AppSetting.xaml = this contains a page with the fields and labels needed  for this portion of the configuration

因此,该项目最初只有2页..在主窗口中,加载了本地web.config,您可以填写字段并更新文件..效果很好..但是配置的位置是硬编码的,没有用在所有位置相同的位置 服务器/客户端.

So originally this project only had 2 pages.. in the mainwindow the local web.config was loaded and you could complete the fields and update the file.. that worked great.. but the location to the config was hardcoded and wasnt in the same location on all servers /clients.

因此,我添加了新的第一页IIS.xaml,其中包含可由用户配置的IIS字段.该页面上的字段之一是他们要编辑的站点的名称,所以我需要传递而不是硬编码值 提供给第2页的值,以便可以加载配置文件.

So I added the new first page which is the IIS.xaml and that contains the fields for IIS that can be configured by the user. One of the fields on that page is the name of the site that they want to edit, so instead of the hardcoded value, i need to pass the value provided to page 2 so that the config file can be loaded..

因此,无需在应用程序打开后立即加载配置文件,而是需要在用户导航至应用程序的第2页时加载该文件.

So instead of loading the config file once the application opens, i need to load it when the user navigates to page 2 of the app.

我的页面在相互之间的导航方面都起作用..但是无法弄清楚如何将提供给第2页的值传递给我,因此我可以用它来拉出正确的文件进行编辑.

I have the pages working as far as navigation between each other.. but cant figure out how to pass the value provide to page 2 so i can use that to pull the correct file to edit.

有人可以协助吗?除了下面的代码,我还需要发布什么代码?

Can anyone assist with this? what code would i need to post besides what i have below?

=======================

=========================


MainWindow.xaml
<Window x:Class="ConfigSetup.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ConfigSetup="clr-namespace:ConfigSetup"
        Height="485" Width="560" ResizeMode="CanMinimize" 
 WindowStartupLocation="CenterScreen" WindowStyle="SingleBorderWindow" 
 SizeToContent="WidthAndHeight" Icon="/ConfigSetup;component/Themes/favicon.ico">
    <Window.Background>
        <ImageBrush ImageSource="/ConfigSetup;component/Themes/swoosh2-gray.jpg" />
    </Window.Background>
    <Grid>
        <Frame Name="frameContent" NavigationUIVisibility="Hidden" Navigated="frameContent_Navigated" />        
        <Button Content="Next" Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="btnNext" VerticalAlignment="Bottom" Width="75" Click="btnNext_Click" />
        <Button Content="Cancel" Margin="0,0,186,12" Name="btnCancel" Click="btnCancel_Click" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" />
        <Button Content="Back" Height="23" HorizontalAlignment="Right" Margin="0,0,93,12" Name="btnBack" VerticalAlignment="Bottom" Width="75" Click="btnBack_Click" IsEnabled="False" />        
    </Grid>
</Window>

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web.Configuration;
using System.Configuration;
using System.DirectoryServices;
using System.Web.Administration;

namespace ConfigSetup
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Configuration webcfg;

        //list of pages with verify() method
        LinkedList<DataVerify> listVerify = new LinkedList<DataVerify>();
        LinkedListNode<DataVerify> iter;

        public MainWindow()
        {
            InitializeComponent();

            //uses wwwroot as initial dir, must have copied the web files to the right dir before this
            webcfg = WebConfigurationManager.OpenWebConfiguration(@"/");
            
            //add all of the pages here
            listVerify.AddLast(new IIS());
            listVerify.AddLast(new ConnString());
            listVerify.AddLast(new AppSettings());

            iter = listVerify.First;
            frameContent.Content = iter.Value;            
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (iter.Value.assembleData(webcfg))
            {
                if (iter.Next != null)
                {
                    iter = iter.Next;
                    frameContent.Content = iter.Value;

                    btnBack.IsEnabled = true;
                    if (iter.Next != null)
                    {
                        btnNext.IsEnabled = true;
                    }
                    else
                    {
                        btnNext.Content = "Finish";
                    }
                }
                else
                {
                    webcfg.SaveAs("web.config");
                    Application.Current.Shutdown(0);
                }
            }
            else
            {
                //data is not valid
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to quit the Web.config setup?", "Exit Setup",
                            MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown(1);
            }
        }

        private void frameContent_Navigated(object sender, NavigationEventArgs e)
        {
            frameContent.NavigationService.RemoveBackEntry();
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (iter.Previous != null)
            {
                iter = iter.Previous;
                frameContent.Content = iter.Value;
                if (iter.Previous == null)
                {
                    btnBack.IsEnabled = false;
                }
                btnNext.IsEnabled = true;
                btnNext.Content = "Next";
            }
         }
    }
}


IIS.xaml
<Page x:Class="ConfigSetup.IIS"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      Height="200" Width="560"
	Title="IIS">

    <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="/ConfigSetup;component/Themes/swoosh2-gray.jpg" Stretch="UniformToFill" TileMode="None" />
        </Grid.Background>
        <Label Content="IIS Settings" FontSize="15" Height="25" HorizontalAlignment="Left" Margin="23,19,0,0" Name="label5" Padding="5,5,0,0" VerticalAlignment="Top" Width="181"  />
        <TextBox Height="25" HorizontalAlignment="Left" Margin="290,62,0,0" Name="txtIISName" VerticalAlignment="Top" Width="185" LostFocus="txtIISName_LostFocus" />
        <Label Content="IIS Site Name" Height="25" HorizontalAlignment="Left" Margin="125,62,0,0" Name="lblIISSitename" Padding="5,5,0,0" VerticalAlignment="Top" Width="94" />
        <Label Content="ITEMS IN RED ARE REQUIRED!" Height="28" HorizontalAlignment="Left" Margin="290,95,0,0" Name="lblValidation" VerticalAlignment="Top" Width="185" Visibility="Hidden" Foreground="Red" FontWeight="Bold" ForceCursor="False" />
    </Grid>
</Page>

IIS.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;

namespace ConfigSetup
{
    /// <summary>
    /// Interaction logic for IIS.xaml
    /// </summary>
    public partial class IIS : Page, DataVerify
    {
        public IIS()
        {
            InitializeComponent();
        }

        private bool valid;

        public bool verify()
        {
            valid = true;
            txtIISName_LostFocus(null, null);
            return valid;
        }

        public bool assembleData(Configuration webcfg)
        {
            bool retval = true;
            if (verify())
            {
                retval = true;
            }
            else
            {
                retval = false;
            }
            return retval;
        }

        private void txtIISName_LostFocus(object sender, RoutedEventArgs e)
        {
            if ((txtIISName.Text == string.Empty) || (txtIISName.Text == null))
            {
                valid = false;
                txtIISName.BorderThickness = new Thickness(2.0);
                txtIISName.BorderBrush = Brushes.Red;
                lblIISSitename.Foreground = Brushes.Red;
                lblIISSitename.FontWeight = FontWeights.Bold;
                lblValidation.Visibility = Visibility.Visible;
            }
            else
            {
                txtIISName.BorderThickness = new Thickness(1.0);
                txtIISName.BorderBrush = Brushes.Black;
                lblIISSitename.Foreground = Brushes.Black;
                lblIISSitename.FontWeight = FontWeights.Normal;
                lblValidation.Visibility = Visibility.Hidden;
            }
        }

        private void Textbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
            }
        }
    }
}


ConnString.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Web.Configuration;

namespace ConfigSetup
{
    /// <summary>
    /// Interaction logic for ConnString.xaml
    /// </summary>
    public partial class ConnString : Page, DataVerify
    {
        Configuration webcfg;

        public ConnString()
        {
            InitializeComponent();

            webcfg = WebConfigurationManager.OpenWebConfiguration(@"/",txtIISName.Text);=====THIS IS THE LINE I NEED TO HAVE THE VALUE FROM PAGE 1
        }
}

推荐答案

最好的方法是绑定TextBox的Text属性到后端类(IIS)上的字符串属性.

The best way is to bind your TextBox's Text property to a string property on your backend class (IIS).

这样,您可以从属性访问TextBox的值,其他类可以通过实例将其访问IIS.

That way, you can access the value of the TextBox from the property, which can be accessed by other classes with an instance to IIS.

 

它看起来像:

xaml:

<TextBox Text="{Binding MyValue}"/>

 

cs:

private string mMyValue;
public string MyValue
{
  get {return mMyValue;}
  set
  {
      mMyValue = value;
     // TODO: Implement INotifyPropertyChanged in class and raise the PropertyChanged event here.
  }
}



这篇关于在加载之前,需要帮助将文本框值从第一页传递到第二页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆