Home > OS >  How to update a Value from another script (CS & WPF)
How to update a Value from another script (CS & WPF)

Time:09-07

I'm using the following code to check which status the launcher that I'm writing is

    enum LauncherStatus
    {
        ready,
        failed,
        downloadingGame,
        downloadingUpdate,
        UpdateAvailable,
        Install
    }
    
    private LauncherStatus _status;
        internal LauncherStatus Status
        {
            get => _status;
            set
            {
                _status = value;
                switch (_status)
                {
                    case LauncherStatus.ready:
                        PlayButton.Content = "Launch";
                        break;
                    case LauncherStatus.failed:
                        PlayButton.Content = "Update Failed - Try again";
                        break;
                    case LauncherStatus.downloadingGame:
                        PlayButton.Content = "Downloading";
                        break;
                    case LauncherStatus.downloadingUpdate:
                        PlayButton.Content = "Downloading Update";
                        break;
                    case LauncherStatus.UpdateAvailable:
                        PlayButton.Content = "Update Available";
                        break;
                    case LauncherStatus.Install:
                        PlayButton.Content = "Install";
                        break;
                    default:
                        break;
                }
            }
        }

Now I want to make the call Status = LauncherStatus.ready; to set it to Ready but I want to call it from another C# script attached to a new WPF window (If i stated that correctly)

How can I change the value of Status of mainWindow from my secondwindow window2?

CodePudding user response:

You need to get a reference to the MainWindow one way or another.

One way to do it is to use the Application.Current.Windows property:

var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if (mainWindow != null)
    mainWindow.Status = LauncherStatus.ready;
  • Related