2016-04-14 113 views
0

我正在嘗試在C#中創建一個Wumpus世界。 這些是一些類:更新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接收的參數(大小)。 CanvasWorld(大小)是一個方法構造函數,可以創建一個正方形的等級。我想知道如何更新第二頁,因爲wrldCanvas被初始化後出現在頁面上,但沒有對齊。

SecondPage Running

回答

0

你說得對有關替代OnNavigatedTo method,但是當導航到SecondPageOnNavigatedTo方法不會被觸發。

這種方法應該是這樣的:

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

感謝您的觀察。但它受到保護。我剛寫錯了! –

+0

@SérgioDamasceno,請注意,此方法的參數應該是'NavigationEventArgs e',而不是'EventArgs e'。 –

0

假設你希望把你的新CanvasWorld對象,你已經有一個Canvas名爲wrldCanvas:有幾種方法,最簡單的可能涉及與更換您的佔位符帆布更實用的東西,如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); 
} 

(次要的問題:在你的代碼中使用的名稱wrldCanvas代表OnNavigatedTo中的一個局部變量,與您在XAML中聲明的Canvas的字段名稱相同,但這既不合法也不會阻止您引用兩者(通過將該字段引用爲this.wrldCanvas),在我看來,這是混淆的祕訣。如果局部變量不屏蔽字段,則更好...)

+0

謝謝您的建議。我用它們來改進我的代碼。 –