问题描述
在单击按钮时,我希望禁用单击的按钮,并在执行过程后重新启用它。
虽然看起来很简单,但由于整个过程成功执行的原因,但并未禁用该按钮。在特定情况下会禁用它,而不是在单击按钮开始时禁用它。
下面是按钮的XAML代码
<Button
x:Name="PreviewReportButton"
Click="PreviewExcelReportButton_Click"
Background="{StaticResource ExportExcelButtonColor}"
BorderBrush="{StaticResource ExportExcelButtonColor}"
Focusable="False"
IsEnabled="False"
Width="80"
Height="Auto"
Margin="450,0"
FontSize="9"
FontWeight="DemiBold"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Column="3"
Grid.Row="5"
Cursor="Hand"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center">
<TextBlock TextAlignment="Center">Button 1</TextBlock>
</Button>
private void PreviewExcelReportButton_Click(object sender,RoutedEventArgs e)
{
var btn_excel = (Button)sender;
btn_excel.IsEnabled = false;
//PreviewReportButton.IsEnabled = false; This is the same button as btn_excel (the button I want to click)
Debug.WriteLine("Button must be disabled");
try
{
//Check if file is open
int IsMacroFileOpen = CheckFileIsOpen($@"{path}file_1.xlsm");
int IsReportFileOpen = CheckFileIsOpen($@"{path}file_2.xlsm");
int IsXLSXFileOpen = CheckFileIsOpen($@"{path}file_3.xlsx");
Debug.WriteLine(IsMacroFileOpen);
Debug.WriteLine(IsReportFileOpen);
Debug.WriteLine(IsXLSXFileOpen);
if (new[] { 1,2 }.Contains(IsMacroFileOpen) || new[] { 1,2 }.Contains(IsReportFileOpen) || new[] { 1,2 }.Contains(IsXLSXFileOpen))
{
btn_excel.IsEnabled = true;
return;
}
Mouse.OverrideCursor = Cursors.Wait;
//Step 1: Create copy of standard report file
CreatecopyreportserverNameDB($@"{path}file_1.xlsm");
//Step 2: Run macro
ExecuteExcelMacro($@"{path}file_2.xlsm");
//Step 3: Open a copy of the xlsx updated file
//Approach 1
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.displayAlerts = false;
ExcelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Add($"{path}file_3.xlsx");
}
catch (Exception)
{
this.Effect = new BlurEffect();
bool? Result = new CustomMessageBox($"Unable to execute Excel button.\nPlease contact application support","Error produced",MessageType.Error,MessageButtons.Ok).ShowDialog();
if (Result.Value)
{
this.Effect = null;
return;
}
else
{
this.Effect = null;
return;
}
}
finally
{
Mouse.OverrideCursor = null;
btn.IsEnabled = true;
}
}
不要对每种方法(CreatecopyreportserverNameDB,ExecuteExcelMacro)的功能关注太多,因为它与问题无关。整个功能正常。不起作用的是我的代码顶部的按钮禁用。当我单击按钮时,由于我使用Mouse.OverrideCursor = Cursors.Wait;
,因此光标会变为 Wait 。奇怪的是,仅当文件被CheckFileIsOpen
捕获时,该按钮才被禁用。如果没有打开文件,则永远不会禁用该按钮。而且我敢肯定,我只能在班级顶部禁用该按钮,而只能在课程结束时启用它。
此外,您还会注意到,如果Debug.Writeline
成功执行并且确实将该行写为输出,则我要写一个IsEnabled = false;
。尽管除非打开文件并被方法CheckFileIsOpen
捕获,否则从不禁用该按钮。
很抱歉,我已经提出了这样一个“虚拟”想法的问题,因为对我而言这很明显。但是我不明白为什么按钮的IsEnabled会发生这种情况。抱歉,这太简单了,但我无法弄清楚是怎么回事。
问题似乎出在我执行第二种方法ExecuteExcelMacro
时
该方法的代码
public void ExecuteExcelMacro(string sourceFile)
{
var destinationFile = @"file_1";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);
string macro = "ThisWorkbook.Run_Code";
try
{
ExcelApp.Run(macro);
Debug.WriteLine("Macro: " + macro + " executed successfully");
}
catch (Exception)
{
this.Effect = new BlurEffect();
bool? Result = new CustomMessageBox($"Unable to Run Macro: {macro}","Cannot execute Macro",MessageButtons.Ok).ShowDialog();
if (Result.Value)
{
this.Effect = null;
return;
}
else
{
this.Effect = null;
return;
}
//Debug.WriteLine("Unable to Run Macro: " + macro + " Exception: " + ex.Message);
}
ExcelApp.displayAlerts = false;
ExcelApp.Visible = false;
ExcelWorkBook.SaveAs($@"{path}{destinationFile}",Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook,Type.Missing);
ExcelWorkBook.Close(0);
ExcelApp.Quit();
if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}
解决方法
在创建ExcelApp实例后,在ExecuteExcelMacro()方法中
制作 ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false;
。
未创建ExcelApp.Visible=false;
然后在PreviewExcelReportButton_Click()方法的最后一步中,执行 Mouse.OverrideCursor = Cursors.Arrow;