2016-04-14 8 views
0

私はC#でWumpus Worldを作成しようとしています。更新UI UWPページアプリケーション

MainPage.xamlを

<Page 
x:Class="MundoWumpus.SecondPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:MundoWumpus" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="10"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <TextBlock Text="Mundo de Wumpus" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10" /> 
    <ContentControl x:Name="myContent" HorizontalAlignment="Center" Grid.Row="1"/> 
</Grid> 

SecondPage.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// O modelo do item de página em branco está documentado em http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace MundoWumpus 
{ 
    /// <summary> 
    /// Uma página vazia que pode ser usada isoladamente ou navegada dentro de um Quadro. 
    /// </summary> 
    public sealed partial class SecondPage : Page 
    { 
     public SecondPage() 
     { 
      this.InitializeComponent(); 
     } 

     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      int size = (int)e.Parameter(); 

      World world = new World(size); 
      WorldCanvas wrldCanvas = new WorldCanvas(size); 
      myContent.Content = WrldCanvas; 
     } 
    } 
} 

CanvasWorldキャンバスから派生するクラスです: これらは、いくつかのクラスです。 MainPageから受け取ったパラメータ(サイズ)が必要なのでSecondPageで初期化する必要があります。 CanvasWorld(size)は、正方形の等級を作るメソッドコンストラクタです。 SecondaryPageを更新する方法を知りたいのですが、wrldCanvasは初期化されてページに表示されますが、配置されていないためです。

SecondPage Running

答えて

0

あなたはOnNavigatedTo methodをオーバーライドについて正しいですが、SecondPageに移動するとき、あなたのOnNavigatedTo方法はトリガされません。

このメソッドは次のようにする必要があります:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    //your code here 
} 
+0

観測のおかげで。しかし、それは保護されています。私はちょうど間違って書いた! –

+0

@SérgioDamasceno、このメソッドのパラメータは 'EventArgs e'ではなく' NavigationEventArgs e'であることに注意してください。 –

0

はあなたがすでにwrldCanvasという名前Canvasを持っているあなたの新しいCanvasWorldオブジェクトを配置すると仮定:いくつかの方法がありますが、最も簡単にはおそらくを使用して、プレースホルダのキャンバスの置換を含みますContentControlのようにもっと実用的なものを入力し、Contentプロパティを新しいCanvasWorldに設定します。

CanvasとXAML行はこのようなものに変更する必要があります。

<ContentControl x:Name="myContent" HorizontalAlignment="Center" Grid.Row="1"/> 

...とビハインドコード:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    int size = (int)e.Parameter; // Property, not method 

    World world = new World(size); // Not clear to me what this is or does? 
    myContent.Content = new CanvasWorld(size); 
} 

(サイド問題:あなたのコードでは、あなたが使用しましたOnNavigatedToのローカル変数の1つ(これは、XAMLで宣言したCanvasに指定したフィールド名と同じもの)の名前wrldCanvasです。これは不正ではなく、両方を参照することもできません(this.wrldCanvas)、それは私の心の中で混乱のレシピです。ローカル変数がフィールドをマスクしない方が良い...)

+0

ありがとうございます。コードを改善するためにそれらを使用しました。 –