我有一个WPF应用程序,我有MainWindow.xalm,MainWindow.xalm.cs和一个名为Utilities.cs的类文件。在MainWindow.xaml中,我创建了一个带有子菜单的侧面菜单。我根据用户选择的内容启用和禁用侧边菜单。我在Utilities.cs类中创建了代码,以启用或禁用,具体取决于是传递true还是false。在MainWindow.xaml.cs中,我引用了Utilities.cs类,如下所示:
NBFoodPantry.Utilities nbuUtilities = new NBFoodPantry.Utilities();
在Utilities.cs文件中,我引用了MainWindow组件,如下所示:
NBFoodPantry.MainWindow nbMainWindow = new MainWindow();
这是我的MainWindow.xaml.cs文件的开头:
namespace NBFoodPantry
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
SqlConnection sqlConn;
string strErrorLogPath, strErrorLogFile, strVCClientIDSelected, strFCClientIDSelected = string.empty;
string strVCName, strFCName, strVCButtonType, strDBInstance = string.empty;
bool blnRowSelected, blnUpdateGridLoaded;;
bool[] blnUpdateFields = new bool[9];
DataRowView drvRow;
PrintDocument printDocument1 = new PrintDocument();
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
bool isWeeklyReportViewerLoaded, isPhoneReportViewerLoaded, isDependentAgeReportViewerLoaded;
BindingSource dataBindingSource = new BindingSource();
NBFoodPantry.Utilities nbuUtilities = new NBFoodPantry.Utilities();
public class Client
{
public string Name { get; set; }
public DataGridRow DGRow { get; set; }
}
#region MainWindow
public MainWindow()
{
这是我的Utilities.cs文件的开头:
namespace NBFoodPantry
{
public class Utilities
{
NBFoodPantry.MainWindow nbMainWindow = new MainWindow();
以下是我为从MainWindow引用子菜单而创建的几个例程:
public void EnableAddMenu(bool blnStatus)
{
nbMainWindow.miAdd.IsEnabled = blnStatus;
}
public void EnableAddSubMenus(bool blnStatus)
{
nbMainWindow.smiAddCheckinDate.IsEnabled = blnStatus;
nbMainWindow.smiAddMonthlyVisitDate.IsEnabled = blnStatus;
nbMainWindow.smiAddCardIssueDate.IsEnabled = blnStatus;
nbMainWindow.smiAddDependent.IsEnabled = blnStatus;
nbMainWindow.smiAddNote.IsEnabled = blnStatus;
}
现在,当我尝试运行该程序时,我收到此消息:
"An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"
在这行代码上:
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
知道为什么我现在得到这个错误?我该如何处理这些错误?代码工作得很好,直到我将重复的代码移到Utilities.cs。
答案 0 :(得分:0)
您可以在Utilities类中注入对已经初始化的MainWindow类实例的引用,而不是在Utilities类中创建MainWindow类的新实例。您将需要对此特定实例的引用,以便能够启用和禁用其中的任何控件。请参阅以下示例代码。
<强> MainWindow.xaml.cs:强>
public partial class MainWindow : Window
{
SqlConnection sqlConn;
string strErrorLogPath, strErrorLogFile, strVCClientIDSelected, strFCClientIDSelected = string.empty;
string strVCName, strFCName, strVCButtonType, strDBInstance = string.empty;
bool blnRowSelected, blnUpdateGridLoaded;;
bool[] blnUpdateFields = new bool[9];
DataRowView drvRow;
PrintDocument printDocument1 = new PrintDocument();
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
bool isWeeklyReportViewerLoaded, isPhoneReportViewerLoaded, isDependentAgeReportViewerLoaded;
BindingSource dataBindingSource = new BindingSource();
public class Client
{
public string Name { get; set; }
public DataGridRow DGRow { get; set; }
}
NBFoodPantry.Utilities nbuUtilities;
public MainWindow()
{
InitializeComponent();
nbuUtilities = new NBFoodPantry.Utilities(this);
}
}
<强> Utilities.cs:强>
public class Utilities
{
NBFoodPantry.MainWindow nbMainWindow;
public Utilities(NBFoodPantry.MainWindow mainWindow)
{
nbMainWindow = mainWindow;
}
}
答案 1 :(得分:0)
我正在采用#BradleyDotNET的方法。我在Utilities.cs文件中将我的代码更改为:
public void EnableAddMenu(bool blnStatus)
{
((MainWindow)System.Windows.Application.Current.MainWindow).miAdd.IsEnabled = blnStatus;
}
public void EnableAddSubMenus(bool blnStatus)
{
((MainWindow)System.Windows.Application.Current.MainWindow).smiAddCheckinDate.IsEnabled = blnStatus;
((MainWindow)System.Windows.Application.Current.MainWindow).smiAddMonthlyVisitDate.IsEnabled = blnStatus;
((MainWindow)System.Windows.Application.Current.MainWindow).smiAddCardIssueDate.IsEnabled = blnStatus;
((MainWindow)System.Windows.Application.Current.MainWindow).smiAddDependent.IsEnabled = blnStatus;
((MainWindow)System.Windows.Application.Current.MainWindow).smiAddNote.IsEnabled = blnStatus;
}
这就是现在,我也在研究MVVC的方法来学习正确的方法。