首先,我是C#和.NET开发的新手。我当时正在开发一个控制台应用程序,并决定使用WPF切换到图形化。控制台应用程序一切都很好,但是我现在遇到了麻烦。所以基本上,我有这个窗口:Window 1

当我单击“添加任务”按钮时,将打开以下新窗口:Window 2。我要执行一系列的保存任务(使该应用程序可以复制目录,并在用户希望的情况下最终对其进行加密),为此,我将所有复制参数保存在字符串列表的第三部分中类,其中有一种方法可以遍历它们并执行副本。

我想做的是在数据网格中显示用户在Window1中输入的所有信息,选择我要执行的保存任务,然后在我单击“启动保存”按钮但无法确定时调用要复制的方法怎么样。我无法从Window1中使用Window2检索存储在第三类列表中的数据,似乎我无法一次将多个保存参数存储在我创建的列表中。 Tbh idk该怎么办,我一直在寻找几个小时的方法,但是我没有找到任何线索。我非常确定我的编码方式是错误的,并且我缺少一些重要的逻辑/理由(或者只是缺乏知识的idk),所以这就是我来这里寻求帮助的原因。

这是window1的代码:

    public partial class NewSave : Window
{
    SeqSave sqSv1 = new SeqSave();
    public NewSave()
    {
        InitializeComponent();
        List<SeqSave> caracSave = new List<SeqSave>();
        if (sqSv1.savedNamesList.Count > 0)
        {
            for (int i = 0; i < sqSv1.savedNamesList.Count; i++)
            {
                caracSave.Add(new SeqSave() { SaveTaskName = sqSv1.savedNamesList[i], SourcePath = sqSv1.srcPathList[i], DestinationPath = sqSv1.dstPathList[i], SaveType = sqSv1.saveTypeList[i], Backup = sqSv1.didBackupList[i] });
            }
            saveListsDisplay.ItemsSource = caracSave;
        }
    }
    private void BussinessSoftware(object sender, RoutedEventArgs e)
    {

    }
    private void AddTask(object sender, RoutedEventArgs e)
    {
        AddTask aT1 = new AddTask();
        aT1.Show();
    }
    private void saveListsDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }
    private void LaunchSave(object sender, RoutedEventArgs e)
    {
        //for(int i = 0; i < sqSv1.savedNamesList.Count; i++)
        //{
        //    Console.WriteLine(sqSv1.savedNamesList[i] + "\n"
        //        + sqSv1.srcPathList[i] + "\n"
        //        + sqSv1.dstPathList[i] + "\n"
        //        + sqSv1.saveTypeList[i] + "\n"
        //        + sqSv1.didBackupList[i]);
        //}
        sqSv1.launchSave();
    }
}


这是Window2的代码:

    public partial class AddTask : Window
{

    List<string> listExtension = new List<string>();
    SeqSave sqSv1 = new SeqSave();
    public AddTask()
    {
        InitializeComponent();
    }
    private void GetSourcePath(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog sourcePath = new FolderBrowserDialog();
        DialogResult result = sourcePath.ShowDialog();
        string strSourcePath = sourcePath.SelectedPath;
        sqSv1.srcPathList.Add(strSourcePath);

        DirectoryInfo dirInfo = new DirectoryInfo(strSourcePath);
        foreach (FileInfo f in dirInfo.GetFiles())
        {
           if (!listExtension.Contains(f.Extension))
           {
               listExtension.Add(f.Extension);
           }
        }

        for(int i = 0; i < listExtension.Count; i++)
        {
            lstB1.Items.Add(listExtension[i]);
        }
    }

    private void GetDestinationPath(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog destinationPath = new FolderBrowserDialog();
        DialogResult result = destinationPath.ShowDialog();
        string strDestinationPath = destinationPath.SelectedPath;
        sqSv1.dstPathList.Add(strDestinationPath);
    }

    private void Encrypt(object sender, RoutedEventArgs e)
    {

    }
    private void Return(object sender, RoutedEventArgs e)
    {

    }

    private void Confirm(object sender, RoutedEventArgs e)
    {
        sqSv1.savedNamesList.Add(taskNameProject.Text);

        if (RadioButton1.IsChecked == true)
        {
            sqSv1.saveTypeList.Add("1");

        }else if(RadioButton2.IsChecked == true)
        {
            sqSv1.saveTypeList.Add("2");
        }
        if (Checkbox1.IsChecked == true)
        {
            sqSv1.didBackupList.Add(true);
        }
        else
        {
            sqSv1.didBackupList.Add(false);
        }
        MessageBox.Show("Task successfully added.");
        this.Visibility = Visibility.Hidden;
    }
}

最佳答案

您需要以第二种形式创建公共属性。我在您的代码中添加了public这样。我还添加了对话框结果到两种形式。

public partial class AddTask : Window
{

    public List<string> ListExtension { get; } = new List<string>();
    public SeqSave SqSv1 { get; }= new SeqSave();

    public AddTask()
    {
        InitializeComponent();
    }
    private void GetSourcePath(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog sourcePath = new FolderBrowserDialog();
        DialogResult result = sourcePath.ShowDialog();
        string strSourcePath = sourcePath.SelectedPath;
        SqSv1.srcPathList.Add(strSourcePath);

        DirectoryInfo dirInfo = new DirectoryInfo(strSourcePath);
        foreach (FileInfo f in dirInfo.GetFiles())
        {
           if (!ListExtension.Contains(f.Extension))
           {
               ListExtension.Add(f.Extension);
           }
        }

        for(int i = 0; i < ListExtension.Count; i++)
        {
            lstB1.Items.Add(ListExtension[i]);
        }
    }

    private void GetDestinationPath(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog destinationPath = new FolderBrowserDialog();
        DialogResult result = destinationPath.ShowDialog();
        string strDestinationPath = destinationPath.SelectedPath;
        SqSv1.dstPathList.Add(strDestinationPath);
    }

    private void Encrypt(object sender, RoutedEventArgs e)
    {

    }
    private void Return(object sender, RoutedEventArgs e)
    {

    }

    private void Confirm(object sender, RoutedEventArgs e)
    {
        SqSv1.savedNamesList.Add(taskNameProject.Text);

        if (RadioButton1.IsChecked == true)
        {
            SqSv1.saveTypeList.Add("1");

        }else if(RadioButton2.IsChecked == true)
        {
            SqSv1.saveTypeList.Add("2");
        }
        if (Checkbox1.IsChecked == true)
        {
            SqSv1.didBackupList.Add(true);
        }
        else
        {
            SqSv1.didBackupList.Add(false);
        }
        MessageBox.Show("Task successfully added.");
        this.Close();
        DialogResult = DialogResult.OK;
    }
}


然后按以下原始格式访问它们:

    private void AddTask(object sender, RoutedEventArgs e)
    {
        AddTask aT1 = new AddTask();
        DialogResult results = aT1.ShowDialog();
        if(results == DialogResult.OK)
        {
            List<string> listExt = aT1.ListExtension;
        }
    }

09-06 03:14