繁体   English   中英

更新UI UWP页面应用程序

[英]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在初始化后出现在页面上,但没有对齐。

SecondPage正在运行

您应该重写OnNavigatedTo方法 ,但是导航到SecondPage时不会触发您的OnNavigatedTo方法。

此方法应如下所示:

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

假设您要将新的CanvasWorld对象放置在已经有一个名为wrldCanvasCanvas 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM