0

My ViewModel contains a variable that is initialized in a Command and I want to use it in another command. There are 2 Windows: the first window contains a button that launches the first command and open a second window. The second window contains a button that launches the second command. This second command needs the variable initialized by the first command. Both commands are implemented my ViewModel.

Problem: When the second window opens, the variable that has been initialized by the first Command has lost its value (empty string) and I dont understand why.

How can I realize it?

Guilian
  • 129
  • 1
  • 11
  • What do you mean by "Command"? – byxor Oct 21 '16 at 18:59
  • C# doesn't have global variables. You could use a class member property, though, or a `static` property. Couldn't tell you which would be better without knowing some basic information about your code. Are the commands in different classes? – 15ee8f99-57ff-4f92-890c-b56153 Oct 21 '16 at 19:00
  • 2
    "the variable that has been initialized by the first Command has lost its value (empty string) and I dont understand why." -- ahhh, so you're asking us to tell you what your code does there. Without having seen it. Let me see if nuget still has the Tarot Card and Mind Reading packages... – 15ee8f99-57ff-4f92-890c-b56153 Oct 21 '16 at 19:02
  • OK, so I'm not sure about how to resolve the problem with your code, but *do not* go on any long journeys under a full moon. Cheers! – 15ee8f99-57ff-4f92-890c-b56153 Oct 21 '16 at 19:05
  • what is static? –  Oct 21 '16 at 19:19
  • Firstly, you should paste your code in the same question area, this is not a forum. Secondly, your ViewModel looked tightly coupled with the Views (Windows in your case) which is against MVVM principles. You have to consider changing a bit your structure. You may need to create separate ViewModel for each logical view. ViewModels are responsible for communicating domain data.consider this [answer](http://stackoverflow.com/a/25845946/1131905) as a guidline for opening windows in MVVM – HichemSeeSharp Oct 22 '16 at 07:02

1 Answers1

0

Here is my code (from my ViewModel class):

// TextForThisWindow is a global variable declared in this class.

...

void firstCommandExecute()
    {
        TextForThisWindow = "Text";
        System.Windows.Application.Current.MainWindow.Hide();
        secondWindow sw = new secondWindow(Text);
        sw .WindowStartupLocation = WindowStartupLocation.CenterScreen;
        sw .Show();
    }

    bool CanfirstCommandExecute()
    {
        return true;
    }


    public ICommand firstCommand{ get { return new RelayCommand(firstCommandExecute, CanfirstCommandExecute); } }


    void secondCommandExecute()
    {
        Info = "info";

        if (TextForThisWindow.Contains("X"))
        {
            Selected = "X";
        }

        PathOfSelectedInfo = img.getPathOfSelectedInfo(ImagePathM1, Info);
        path = PathOfSelectedInfo;
        thirdWindow tw= new thirdWindow("Text" + Selected);
       tw.WindowStartupLocation = WindowStartupLocation.CenterScreen;
       tw.Show();
    }

    CanthirdWindowExecute()
    {
        return true;
    }


    public ICommand secondCommand{ get { return new RelayCommand(secondCommandExecute, CansecondCommandbExecute); } }
Guilian
  • 129
  • 1
  • 11