【问题标题】:Windows store app navigationWindows 商店应用导航
【发布时间】:2015-11-02 17:45:47
【问题描述】:

我正在制作 Pacman Windows 商店应用游戏。我使用 win2d 库制作动画。我在页面之间导航时遇到问题。 这是我的主页,它创建了新游戏。

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        //Game gm = new Game();
    }

    private void playButton_Click(object sender, RoutedEventArgs e)
    {
        Game gm = new Game();
    }

    private void exitButton_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Exit();
    }

    private void resultsButton_Click(object sender, RoutedEventArgs e)
    {

    }
}

但是在游戏课结束后,我必须以某种方式回到我的主页。我尝试了很多方法,但它们对我不起作用。 游戏类:

public Game()
{
    this.InitializeComponent();
    Window.Current.Content = this;
}

private void canvas_CreateResources(CanvasAnimatedControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
    args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}

async Task CreateResourcesAsync(CanvasAnimatedControl sender)
{
    ghostImages = new List<CanvasBitmap>();
    ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost1.png")));
    ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost2.png")));
    ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost3.png")));
    ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost4.png")));
    ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/Pacman_25.png")));
    StartNewGame();
}

private void Canvas_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
    Map.drawBorders(args);
    using (var session = args.DrawingSession)
    {
        session.DrawImage(hero.getImage1(), hero.getX(), hero.getY());
        for (int i = 0; i < ghostList.ToArray().Length; i++)
        {
            session.DrawImage(ghostList[i].getImage(), ghostList[i].getX(), ghostList[i].getY());
        }
        int bestScore = 1, score = 3;
        session.DrawText("Rekordas: " + bestScore, Constants.WIDTH / 3 * 1.8f, Constants.HEIGHT + Constants.SHOWINFOSIZE / 2, Windows.UI.Colors.White);
        session.DrawText("Rezultatas: " + score, Constants.BLOCKSIZE, Constants.HEIGHT + Constants.SHOWINFOSIZE / 2, Windows.UI.Colors.White);
        session.DrawText("Gyvybės: ", Constants.BLOCKSIZE, Constants.HEIGHT + Constants.SHOWINFOSIZE / 1, Windows.UI.Colors.White);
        for (int i = 0; i < 3; i++)
            session.DrawImage(hero.getImage1(), Constants.BLOCKSIZE + 150 + (Constants.BLOCKSIZE + 5) * i, (int)Constants.HEIGHT + Constants.SHOWINFOSIZE / 1 - Constants.BLOCKSIZE + 5);
    }
}

public void GameOver()
{
    playing = false;
    //Frame.Navigate(typeof(MainPage));
    //Dispose();
    //this.Dispose();
    //var page = new MainPage();
    //Window.Current.Content = page;
    //MainPage mn = new MainPage();
    //if (name == null)
    //{
    //    name = "Student";
    //}
    //Window.Current.Content = new MainPage();
    //mn.UpdateLayout();
}

如何浏览页面?谢谢。

【问题讨论】:

    标签: c# xaml windows-store-apps windows-8.1 win2d


    【解决方案1】:

    这里有一些你可能会觉得有用的方法(来自我用来包装导航逻辑的类)

    //Better made the class a singleton but I've skipped that part to for brifety
    public class Navigation 
    {
        public bool CanGoBack
        {
            get
            {
                var frame = ((Frame)Window.Current.Content);
                return frame.CanGoBack;
            }
        }
    
        public Type CurrentPageType
        {
            get
            {
                var frame = ((Frame)Window.Current.Content);
                return frame.CurrentSourcePageType;
            }
        }
    
        public virtual void GoBack()
        {
            var frame = ((Frame)Window.Current.Content);
    
            if (frame.CanGoBack)
            {
                frame.GoBack();
            }
        }
    
        public virtual void NavigateTo(Type sourcePageType)
        {
            ((Frame)Window.Current.Content).Navigate(sourcePageType);
        }
    
        public virtual void NavigateTo(Type sourcePageType, object parameter)
        {
            ((Frame)Window.Current.Content).Navigate(sourcePageType, parameter);
        }
    
        public virtual void GoForward()
        {
            var frame = ((Frame)Window.Current.Content);
    
            if (frame.CanGoForward)
            {
                frame.GoForward();
            }
        }
    }
    

    您可以这样使用它(如果我们假设上述方法驻留在您拥有实例的名为 Navigation 的类中):

    //To go to Game page
    Navigation.NavigateTo(typeof(Game));
    
    //To go to Main page and pass some arguments
    Navigation.NavigateTo(typeof(MainPage), winnerId);
    
    //To go back
    Navigation.GoBack();
    

    加法

    您可以像这样在视图中接收传递的参数:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var receivedParameter = e.Parameter as TheTypeOfThePassedParameter;
    }
    

    传递数据的另一个选项是创建一个静态或单例应用程序类(从任何地方都可见),其中包含一些您希望在整个应用程序中可用的值

    【讨论】:

      【解决方案2】:

      我建议您考虑模型视图视图模型模式来管理您的应用程序的导航逻辑和内容。 (Channel9 introductive video)

      为了帮助您进行导航,您可以使用MVVM Light library,它公开了一些有用的导航方法:

      在 ViewModelLocator.cs 中,您可以为要导航的每个页面定义一个字符串:

      static ViewModelLocator()
      {
          ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
      
          var nav = new NavigationService();
          nav.Configure("MainMenu", typeof(MainMenuView));
          nav.Configure("About", typeof(AboutView));
          nav.Configure("Game", typeof(GameView));
      
          SimpleIoc.Default.Register<INavigationService>(() => nav);
      
          SimpleIoc.Default.Register<MainMenuViewModel>();
          SimpleIoc.Default.Register<AboutViewModel>();
          SimpleIoc.Default.Register<GameViewModel>();
      }
      

      一个典型的 ViewModel 可能是:

      public class GameViewModel : ViewModelBase
      {
          private INavigationService _navigationService;
          public GameViewModel(INavigationService NavigationService)
          {
              _navigationService = NavigationService;
          }
      
          // Then, when you want to expose a navigation command:
          private RelayCommand _navigateToMenuCommand;
          public RelayCommand NavigateToMenuCommand
          {
              get
              {
                  return _navigateToMenuCommand
                      ?? (_navigateToMenuCommand = new RelayCommand(
                      () =>
                      {
                          _navigationService.NavigateTo("MainMenu");
                      }
              {
          }
      }
      

      还有 .XAML:

      <Button Content="Back to Main Menu" Command={Binding GameViewModel} />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多