c# - 申请表未结束

标签 c# wpf

(学习c#)

我有一个 C# WPF 应用程序,当启动某个表单时,该应用程序不允许应用程序进程关闭。调试和发布的 exe 文件中都会出现这种情况。该进程将一直保留,直到通过任务管理器终止。

仅在加载此表单时发生,加载和卸载的其他表单允许应用程序正常关闭。

有人能指出我的错误吗?

XAML:

<Window x:Class="FileDownloadService.FileEditorWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FileEditorWindow" Height="347" Width="618" Name="FileEditor" Background="#FFF1E7E7" ResizeMode="NoResize" Icon="/FileDownloadService;component/Images/Folder%20Concept%20Icons%2034.ico">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="142,36,0,0" Name="txtDownloadName" VerticalAlignment="Top" Width="135" />
        <Label Content="Download Name" Height="28" HorizontalAlignment="Left" Margin="12,36,0,0" Name="labRef" VerticalAlignment="Top" Width="105" />
        <Label Content="Download URL" Height="28" HorizontalAlignment="Left" Margin="12,70,0,0" Name="labURL" VerticalAlignment="Top" Width="105" />
        <TextBox Height="23" Margin="142,70,0,0" Name="txtURL" VerticalAlignment="Top" HorizontalAlignment="Left" Width="373" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="142,103,0,0" Name="txtSaveLoc" VerticalAlignment="Top" Width="340" />
        <Label Content="Save Location" Height="28" HorizontalAlignment="Left" Margin="12,101,0,0" Name="labSaveLoc" VerticalAlignment="Top" Width="105" />
        <Button Content="Save" Margin="0,0,12,23" Name="btnSave" Click="btnSave_Click" Height="22" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="71" />
        <Button Content="Close" Margin="0,0,89,23" Name="btnClose" Click="btnClose_Click" Height="22" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="61" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="338,36,0,0" Name="txtRef" VerticalAlignment="Top" Width="46" IsReadOnly="True" />
        <Label Content="ID Ref" Height="28" HorizontalAlignment="Left" Margin="288,36,0,0" Name="labID" VerticalAlignment="Top" Width="65" />
        <Button Content="..." Height="23" Margin="488,103,0,0" Name="btnBrowse" VerticalAlignment="Top" HorizontalAlignment="Left" Width="27" Click="btnBrowse_Click" />
        <CheckBox Content="Enabled" Height="16" HorizontalAlignment="Left" Margin="142,12,0,0" Name="ckenabled" VerticalAlignment="Top" />
        <Label Content="File Name" Height="28" HorizontalAlignment="Left" Margin="12,132,0,0" Name="lab_filename" VerticalAlignment="Top" Width="105" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="142,132,0,0" Name="txtSaveName" VerticalAlignment="Top" Width="223" />
        <TextBlock Height="17" Margin="3,0,0,1" Name="txtLastStatus" Text="" VerticalAlignment="Bottom" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="142,161,0,0" Name="txtFreqUnit" VerticalAlignment="Top" Width="46" />
        <Label Content="Download Frequency" Height="28" HorizontalAlignment="Left" Margin="12,159,0,0" Name="labFreq" VerticalAlignment="Top" Width="124" />
        <ComboBox x:Name="cmbFreqVal" ItemsSource="{Binding}" DataContext="{Binding}" Height="23" HorizontalAlignment="Left" Margin="194,161,0,0" VerticalAlignment="Top" Width="69">

        </ComboBox>
    </Grid>
</Window>

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.Forms;
using System.IO;
using FileDownloadService.ClassLib;
using MessageBox = System.Windows.Forms.MessageBox;

namespace FileDownloadService
{
    /// <summary>
    /// Interaction logic for FileEditorWindow.xaml
    /// </summary>
    public partial class FileEditorWindow : Window
    {
        public string Mode = "none";

        public class FreqUnit
        {
            public string Name { get; set; }
            public string Value { get; set; }
            public override string ToString() { return this.Name; }
        }


        public FileEditorWindow(string args)
        {

            InitializeComponent();

            //build up freqUnit list
            this.cmbFreqVal.Items.Add(new FreqUnit { Name = "Seconds" });
            this.cmbFreqVal.Items.Add(new FreqUnit { Name = "Minutes" });
            this.cmbFreqVal.Items.Add(new FreqUnit { Name = "Hours" });


            if (args == null)
            {
                //do nothing

                Mode = "add";

                //populate default save location
                XmlInterface getConfig = XmlInterface.DeserializeConfiguration(Globals.ConfigFileName);
                txtSaveLoc.Text = getConfig.defaultSaveLoc;


            }
            else
            {
                Mode = "edit";

                //New instance of the editor class
                Editor oEditor = new Editor();

                //launch method to obtain selected record data
                string[] SelectedRecordData = oEditor.ReadRecord(args);

                //is the item enabled or disabled
                bool enabledResult;
                if (SelectedRecordData[4] == "1")
                {
                    enabledResult = true;
                }
                else
                {
                    enabledResult = false;
                }

                //populate form
                txtRef.Text = SelectedRecordData[0];
                txtDownloadName.Text = SelectedRecordData[1];
                txtURL.Text = SelectedRecordData[2];
                txtSaveLoc.Text = SelectedRecordData[3];
                ckenabled.IsChecked = enabledResult;
                txtSaveName.Text = SelectedRecordData[5];
                txtLastStatus.Text = SelectedRecordData[6];
                txtFreqUnit.Text = SelectedRecordData[7];
                cmbFreqVal.Text = SelectedRecordData[8];



            }


        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            var mainForm = new MainWindow();

            Close();


        }


        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (Mode == "add")
            {
                Editor rEditor = new Editor();


                if (rEditor.WriteRecord(Mode, ckenabled.IsChecked, txtDownloadName.Text, txtRef.Text, txtURL.Text, txtSaveLoc.Text, txtSaveName.Text, txtFreqUnit.Text, cmbFreqVal.SelectedItem.ToString()) == true)
                {
                    MessageBox.Show("Item Added Successfully");
                }



            }
            if (Mode == "edit")
            {

                Editor rEditor = new Editor();



                if (rEditor.WriteRecord(Mode, ckenabled.IsChecked, txtDownloadName.Text, txtRef.Text, txtURL.Text, txtSaveLoc.Text, txtSaveName.Text, txtFreqUnit.Text, cmbFreqVal.SelectedItem.ToString()) == true)
                {
                    MessageBox.Show("Item Saved Successfully");
                }

            }


        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {

            var browseDialog = new FolderBrowserDialog();


            browseDialog.RootFolder = Environment.SpecialFolder.MyComputer;

            if (browseDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtSaveLoc.Text = browseDialog.SelectedPath;
            }

        }



    }
}

最佳答案

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        var mainForm = new MainWindow();  // why ???

        Close();
    }

在 CloseButton 中单击,您将关闭当前表单,但同时也会创建一个新表单...

删除该行,您的应用程序可能会关闭。使用 [x] 按钮也应该可以。

关于c# - 申请表未结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241525/

相关文章:

c# - 调用异步方法的两种方式。 C#

c# - 使用 RegEx 匹配文件名

c# - 如何使用具有空值的 DataReader

wpf - 在内容中包含一个 UserControl

c# - 如何在不阻塞 UI 线程的情况下在执行多个任务后继续?

wpf - 引用的外部命名空间与本地命名空间崩溃

wpf - 装饰元素上的装饰器和事件

c# - 可以在 app.config 中模拟控制台应用程序吗?

c# - Paypal 付款处理

c# - 我们如何使 WPF slider 增加与刻度频率不同的数字?