2015-11-02 181 views
0

我正在製作Pacman Windows應用商店應用遊戲。我使用win2d庫來製作動畫。我在頁面之間導航時遇到問題。 這是我的主頁,它創建了新的遊戲。Windows商店應用導航

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(); 
} 

我該如何瀏覽頁面?謝謝。

回答

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(); 
     } 
    } 
} 

你使用這樣的(如果我們假設上述方法存在於一個名爲類你有實例導航):

//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(); 

加成

你可以收到通過看齊ameters在你的看法是這樣的:

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

來傳遞數據的其他選擇是在你的應用程序

0

我創建一個靜態或單應用的明智類包含您要使用一些值(從隨處可見)建議您考慮模型視圖視圖模型模式來管理您的應用的導航邏輯和內容。 (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>(); 
} 

一典型的視圖模型可能是:

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} />