我的窗口c#MVVM的“重新启动”按钮



我正试图为当前窗口制作一个重新启动按钮,但当我按下它时,窗口会自动关闭,程序会在gameWindow.Show()上停止,并出现以下错误:PresentationFramework.dll中发生了类型为System.InvalidOperationException的未经处理的异常附加信息:在窗口关闭后,无法设置Visibility或调用Show、ShowDialogWindowInteropHelper.EnsureHandle。我有一个GameWindowGameWindowViewModel,我在其中创建,ICommand用于重新启动按钮和一个函数。

这是我正在使用的代码:

 void restartButtonClickFunction(object obj)
{
    GameWindow gamewindow = new GameWindow();
    foreach (var window in Application.Current.Windows)
    {
        if (window is GameWindow)
        {
            ((Window)window).Close();
        }
    }
    gamewindow.Show();
}

IMO,最好重新设计您的应用程序。我建议您分开视图模型,而不是关闭主窗口并再次显示它。

创建游戏视图模型:

public class GameVm : ViewModelBase
{
    public GameVm()
    {
        var rnd = new Random();
        health = rnd.Next(100);
        armor = rnd.Next(100);
    }
    public int Health
    {
        get { return health; }
        set
        {
            if (health != value)
            {
                health = value;
                OnPropertyChanged();
            }
        }
    }
    private int health;
    public int Armor
    {
        get { return armor; }
        set
        {
            if (armor != value)
            {
                armor = value;
                OnPropertyChanged();
            }
        }
    }
    private int armor;
}

和应用程序视图模型:

public class ApplicationVm : ViewModelBase
{
    public ApplicationVm()
    {
        RestartGameCommand = new RelayCommand(() => Game = new GameVm());
        RestartGameCommand.Execute(null);
    }
    public GameVm Game
    {
        get { return game; }
        private set
        {
            if (game != value)
            {
                game = value;
                OnPropertyChanged();
            }
        }
    }
    private GameVm game;
    public ICommand RestartGameCommand { get; set; }
}

正如您所看到的,应用程序管理游戏生命周期——当RestartGameCommand被触发时,游戏将被重新创建。

现在,这就是它在视图层的工作方式:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="100" Width="200">
    <Window.DataContext>
        <local:ApplicationVm />
    </Window.DataContext>
    <StackPanel>
        <!-- Game view placeholder -->
        <ContentControl Content="{Binding Game}">
            <ContentControl.ContentTemplate>
                <DataTemplate DataType="{x:Type local:GameVm}">
                    <StackPanel>
                        <TextBlock Text="{Binding Health}"/>
                        <TextBlock Text="{Binding Armor}"/>
                    </StackPanel>
                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>
        <!-- Game controls pane -->
        <Button Content="Restart game" Command="{Binding RestartGameCommand}"/>
    </StackPanel>
</Window>

当处理RestartGameCommand时,ContentControl内部视图将使用新的游戏视图模型进行初始化。

没有任何管理窗口的代码,但游戏已重新启动。

试试这是否有效未经测试,只是猜测

void restartButtonClickFunction(object obj)
{
    foreach (var window in Application.Current.Windows)
    {
        if (window is GameWindow)
        {
            ((Window)window).Close();
        }
    }
    GameWindow gamewindow = new GameWindow();
    gamewindow.Show();
}

Error告诉您问题出在哪里。您的主窗口已经关闭,因此在此之后无法执行其他操作。相反,试着重新运行这个过程,而不是

void restartButtonClickFunction(object obj)
{
    Process.Start(Application.ResourceAssembly.Location);
    Application.Current.Shutdown();
}

或者,您可以使用EventAggregaterGameWindowViewModel引发事件,并在MainWindowViewModel中侦听该事件,然后关闭并重新启动GameWindow

我知道我来晚了,但我解决了你的问题。我现在也在做同样的事情,只是在玩战争纸牌游戏。下面的代码经过了测试,它是有效的。

private void Btn_reset_Click(object sender, RoutedEventArgs e)
      var newwindow = new Game_Window();
      newwindow.Show();
      Close();
}

最新更新