(学习c#)
我有一个C#WPF应用程序,当启动某个表单时,不允许应用程序进程关闭。这在Debug和已发布的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;
}
}
}
}
答案 0 :(得分:3)
private void btnClose_Click(object sender, RoutedEventArgs e)
{
var mainForm = new MainWindow(); // why ???
Close();
}
在CloseButton中单击,您将关闭当前表单,但您也在创建一个新表单...
删除该行,您的应用可能会关闭。使用[x]按钮也应该有效。
答案 1 :(得分:2)
好吧,根据我原来的评论,我建议从close事件中删除窗口的创建。 :)