You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从另一个C#脚本更新WPF主窗口的Status属性

从第二个窗口修改主窗口的Status属性的实现方法

方法一:直接传递主窗口实例

这种方式简单直接,适合小型项目:

  1. 在Window2中添加主窗口的引用字段,并修改构造函数接收实例:
public partial class Window2 : Window
{
    private MainWindow _mainWindow;

    // 接收MainWindow实例的构造函数
    public Window2(MainWindow mainWindow)
    {
        InitializeComponent();
        _mainWindow = mainWindow;
    }

    // 示例:在某个按钮点击事件中修改主窗口状态
    private void SetReadyButton_Click(object sender, RoutedEventArgs e)
    {
        _mainWindow.Status = LauncherStatus.ready;
    }
}
  1. 在MainWindow中打开Window2时传入自身实例:
private void OpenWindow2Button_Click(object sender, RoutedEventArgs e)
{
    Window2 window2 = new Window2(this);
    window2.Show();
}

注意:如果Window2和MainWindow不在同一个程序集,需要把Status属性的访问修饰符从internal改为public,或者调整程序集的内部访问权限。

方法二:通过事件解耦(推荐)

这种方式避免窗口间直接引用,降低耦合度:

  1. 在Window2中定义触发状态修改的事件:
public partial class Window2 : Window
{
    // 定义事件,通知主窗口设置ready状态
    public event Action RequestSetReadyStatus;

    private void TriggerSetReady_Click(object sender, RoutedEventArgs e)
    {
        // 触发事件,通知订阅者
        RequestSetReadyStatus?.Invoke();
    }
}
  1. 在MainWindow中订阅Window2的事件,自行修改状态:
private void OpenWindow2Button_Click(object sender, RoutedEventArgs e)
{
    Window2 window2 = new Window2();
    // 订阅事件,当事件触发时修改Status
    window2.RequestSetReadyStatus += () => Status = LauncherStatus.ready;
    window2.Show();
}

方法三:使用共享ViewModel(MVVM场景)

如果你的项目采用MVVM模式,推荐将状态逻辑抽离到ViewModel中,多窗口共享同一实例:

  1. 创建共享的ViewModel类(实现INotifyPropertyChanged以支持UI绑定):
public class LauncherViewModel : INotifyPropertyChanged
{
    private LauncherStatus _status;
    public LauncherStatus Status
    {
        get => _status;
        set
        {
            _status = value;
            OnPropertyChanged(nameof(Status));
            // 这里可以保留原来的按钮文本切换逻辑,或者通过绑定实现
            switch (_status)
            {
                case LauncherStatus.ready:
                    PlayButtonText = "Launch";
                    break;
                case LauncherStatus.failed:
                    PlayButtonText = "Update Failed - Try again";
                    break;
                case LauncherStatus.downloadingGame:
                    PlayButtonText = "Downloading";
                    break;
                case LauncherStatus.downloadingUpdate:
                    PlayButtonText = "Downloading Update";
                    break;
                case LauncherStatus.UpdateAvailable:
                    PlayButtonText = "Update Available";
                    break;
                case LauncherStatus.Install:
                    PlayButtonText = "Install";
                    break;
                default:
                    break;
            }
        }
    }

    private string _playButtonText;
    public string PlayButtonText
    {
        get => _playButtonText;
        set
        {
            _playButtonText = value;
            OnPropertyChanged(nameof(PlayButtonText));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 在MainWindow中初始化ViewModel并绑定到DataContext:
public MainWindow()
{
    InitializeComponent();
    DataContext = new LauncherViewModel();
}
  1. 在Window2中获取共享的ViewModel实例并修改状态:
private void SetReadyButton_Click(object sender, RoutedEventArgs e)
{
    if (DataContext is LauncherViewModel vm)
    {
        vm.Status = LauncherStatus.ready;
    }
}

注意:打开Window2时要将MainWindow的ViewModel传递给它,确保两个窗口共享同一实例:

// 在MainWindow中打开Window2
private void OpenWindow2Button_Click(object sender, RoutedEventArgs e)
{
    Window2 window2 = new Window2();
    window2.DataContext = this.DataContext;
    window2.Show();
}

内容的提问来源于stack exchange,提问作者Marco N.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:55:18