[英]Update UI UWP Page Application
我正在嘗試用C#創建一個Wumpus World。 這些是一些類:
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是從Canvas派生的類。 我必須在SecondPage中對其進行初始化,因為它需要從MainPage接收到的參數(大小)。 CanvasWorld(size)是一種方法構造器,可構成一個正方形。 我想知道如何更新SecondPage,因為wrldCanvas在初始化后出現在頁面上,但沒有對齊。
您應該重寫OnNavigatedTo方法 ,但是導航到SecondPage
時不會觸發您的OnNavigatedTo
方法。
此方法應如下所示:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//your code here
}
假設您要將新的CanvasWorld
對象放置在已經有一個名為wrldCanvas
的Canvas
wrldCanvas
:有幾種方法,最簡單的方法可能是用一個更實用的東西(例如ContentControl
替換占位符Canvas,然后將其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
,一樣的,你給了字段名Canvas
在XAML聲明雖然這既不違法,也不阻止你引用兩者(通過。將該字段引用為this.wrldCanvas
),在我看來,這是造成混亂的秘訣。如果局部變量不掩蓋字段則更好...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.