Windows Phone:NavigateToAction有时会重新初始化视图,有时则不会

时间:2014-11-26 10:15:43

标签: c# xaml view windows-phone

我已经尝试了几个小时解决以下问题: 我正在开发一款游戏,我必须在几个视图之间切换。当我在游戏视图中时,最后会有一个名为"返回匹配"的按钮。导航回" PlayerSelectView"球员的位置:

<Button Tag="{Binding Source={StaticResource Options}, Path=Effect}" Width="300" MinHeight="70">
            <localControls:DropShadowTextBlock Text="{Binding LocalizedText.GV_ReturnMatch}"/>
            <i:Interaction.Triggers>
                <ic:DataTrigger Binding="{Binding Result, ElementName=MessageBoxBehavior}" Value="OK">
                    <i:InvokeCommandAction Command="{Binding NewGameCommand}" />
                    <ic:NavigateToPageAction TargetPage="/Views/PlayerSelectView.xaml" />
                </ic:DataTrigger>
            </i:Interaction.Triggers>
            <i:Interaction.Behaviors>
                <behaviors:MessageBoxBehavior x:Name="MessageBoxBehavior" Text="{Binding LocalizedText.MAIN_YouWillLoseYourPreviousGame}" IsEnabled="{Binding IsGameInitialized}" />
            </i:Interaction.Behaviors>
        </Button>

NewGameCommand不会重置PlayerSelectViewModel中的玩家列表。 此时,PlayerSelectView的构造函数和PlayerSelectViewModel中的构造函数未被调用,因此它保留了状态,但是,在游戏过程中使用此代码返回菜单:

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        GameViewModel.Instance.GameIsPaused = true;

        NavigationService.Navigate(new Uri("/Views/MainPageView.xaml", UriKind.Relative));
    }

然后用“继续”按钮返回GameView:

    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding IsGameInitialized, Converter={StaticResource BoolToVisibilityConverter}}" Tag="{Binding Source={StaticResource Options}, Path=Effect}" Width="300" MinHeight="70" FontWeight="Normal">
            <localControls:DropShadowTextBlock Text="{Binding LocalizedText.MAIN_Continue}"/>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Tap">
                    <ic:NavigateToPageAction TargetPage="/Views/GameView.xaml" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

当我现在点击&#34;返回匹配&#34;时,PlayerSelectView总是被重新初始化!

为什么现在重新初始化PlayerSelectView?另一件事是,当新玩家再次设置并且游戏开始时,它只会返回Game Over屏幕(这是GameView上的弹出层)。

有没有人知道该怎么做?

更新: 游戏开始时,会显示MainView。从那里用户通过此按钮进入PlayerSelectView:

       <Button Tag="{Binding Source={StaticResource Options}, Path=Effect}" Width="300" MinHeight="70">
            <localControls:DropShadowTextBlock Text="{Binding LocalizedText.MAIN_NewGame}"/>
            <i:Interaction.Triggers>
                <ic:DataTrigger Binding="{Binding Result, ElementName=MessageBoxBehavior}" Value="OK">
                    <i:InvokeCommandAction Command="{Binding NewGameCommand}" />
                    <ic:NavigateToPageAction TargetPage="/Views/PlayerSelectView.xaml" />
                </ic:DataTrigger>
            </i:Interaction.Triggers>
            <i:Interaction.Behaviors>
                <behaviors:MessageBoxBehavior x:Name="MessageBoxBehavior" Text="{Binding LocalizedText.MAIN_YouWillLoseYourPreviousGame}" IsEnabled="{Binding IsGameInitialized}" />
            </i:Interaction.Behaviors>
        </Button>

这里创建和编辑了玩家。游戏从这个按钮开始:

<Button Command="{Binding StartGameCommand}" CommandParameter="{Binding NavigationService, ElementName=PlayerSelectViewControl}"  Tag="{Binding Source={StaticResource Options}, Path=Effect}" Padding="0">
            <localControls:DropShadowTextBlock Text="{Binding LocalizedText.PLSEL_StartGame}"/>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Tap">
                    <ic:NavigateToPageAction TargetPage="/Views/GameView.xaml"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

...也运行此代码:

private void StartGame()
    {
        BusinessLogic.GameLogic.Initialize(new List<Player>(Players));
    }

从那里,用户可以返回MainView(菜单),退出游戏(状态被保留)或更改选项。

0 个答案:

没有答案