none
Can an object that has been dependency injected stopped from be disposed from an IoC? RRS feed

  • Question

  • So, I have been playing around with Windows forms in .NET Core 3.0. I have a MainView and a PayRollView and both are  form objects that have been instanced in Services an IoC. The PayRollView is DI into the Mainform and the PayRollView.Show() is done on a button_click_event in the MainView. So when I close PayRollView, the form object is being disposed on the form_unload. How can I stop PayRollView object from being destroyed so that in MainView when I click on the Payroll button again PayrollView object is there to show the form again?

    using System;
    using System.Windows.Forms;
    
    namespace PubCompanyWinCore
    {
        public partial class MainView : Form
        {
            private PayRollView _payRollView;
            public MainView(PayRollView payRollView)
            {
                _payRollView = payRollView;
                InitializeComponent();
            }
    
           
            private void btnAuthor_Click(object sender, EventArgs e)
            {
                throw new NotImplementedException();
            }
    
            private void dtbAriticle_Click(object sender, EventArgs e)
            {
                throw new NotImplementedException();
            }
    
            private void btnPayroll_Click(object sender, EventArgs e)
            {
                _payRollView.Show();
            }
    

    Wednesday, December 4, 2019 1:10 AM

Answers

  • So I found two simple solutions for the problem of the PayRollView form object  being disposed on form unload.

    1) ShowDialog() keeps the object instance of PayRollView and the object is not disposed when form unload is done

    2) _payRollView.Show() does destroy the instance of PayRollView on form unload, but the held instance of PayRollView  the _payRollViewhold populates _payRollView.

    Either way when the button is clicked again,

    the object is there so that the PayRollView can be shown again and again.

    That's my out on this. :)

    Actually, the Core 3.0 Windows form thing is a mess, becuase the Preview Designer doesn't work, and I have to resort to building the form controls and events manually.

    using System;
    using System.Windows.Forms;
    
    namespace PubCompanyWinCore
    {
        public partial class MainView : Form
        {
            private PayRollView _payRollView;
            private PayRollView _payRollViewhold;
            public MainView(PayRollView payRollView)
            {
                _payRollView = payRollView;
                _payRollViewhold = payRollView;
    
                InitializeComponent();
            }
    
           
            private void btnAuthor_Click(object sender, EventArgs e)
            {
                throw new NotImplementedException();
            }
    
            private void dtbAriticle_Click(object sender, EventArgs e)
            {
                throw new NotImplementedException();
            }
    
            private void btnPayroll_Click(object sender, EventArgs e)
            {
                _payRollView.ShowDialog();
                //_payRollView.Show();
                //_payRollView = _payRollViewhhold;
            }
    
            
        }
    }




    • Marked as answer by DA924x Wednesday, December 4, 2019 2:34 AM
    • Edited by DA924x Wednesday, December 4, 2019 2:36 AM
    Wednesday, December 4, 2019 2:33 AM