繁体   English   中英

如何在单独的类中访问MainWindow XAML组件?

[英]How do I access MainWindow XAML components in a separate class?

我有一个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之前,该代码非常有效。

可以在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;
    }
}

我正在采用#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方法,以学习正确的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM