How to instantiate in MainWindow and use it in another class

I have a serious problem, I am trying to instantiate in the MainWindow class like this:

public MainWindow()
{
     InitializeComponent();
     AppWindow = this;
     CalenderBackground background = new CalenderBackground(Calendar);
}

      

I need this istance in MainWindow because the CalenderBackground class has a method to update the previous date inserted in the Calender, I am using this resource.

I want to use an object background

in a class Fixtures

:

class Fixtures
{
     MainWindow.Calendar.Background = background.GetBackground();
}

      

But actually I can't create this because I can't see the variable background

, why?

+3


source to share


2 answers


Pass a Background object to Fixtures via the constructor ?:



CalenderBackground background = new CalenderBackground(Calendar);
Fixtures fixtures;

public MainWindow()
{
     InitializeComponent();
     AppWindow = this;
     fixtures = new Fixtures(background);
}

class Fixtures
{
    public Fixtures(Background background)
    {
        MainWindow.Calendar.Background = background.GetBackground();
    }
}

      

0


source


You have specified the background as a variable within the MainWindow method. To be able to access it in the Fixtures class, you need to pass it as an argument to the constructor and then use it to set the field, for example:

private CalenderBackground _background;

public Fixtures(CalenderBackground background)
{
   _background = background;
}

      



Or you can create a public property on your MainWindow and access it from the Fixtures class.

public CalenderBackground Background {get; set;}

      

0


source







All Articles