【问题标题】:howto add a textBlock to a silverlight/XNA WP7 app?如何将 textBlock 添加到 silverlight/XNA WP7 应用程序?
【发布时间】:2012-02-10 14:28:10
【问题描述】:

您好,我对用于 wp7 编程的 silverlight/xna 模板有疑问。

我开始一个新的项目,然后在游戏屏幕上绘制一些内容。然后我将以下内容添加到 GamePage 的 xaml 页面中:

<Grid Height="800" Name="grid1" Width="480" Background="White">
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock1" Text="Lifes: 3" VerticalAlignment="Top" Foreground="Black" />
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="393,12,0,0" Name="textBlock2" Text="Points: 0" VerticalAlignment="Top" Foreground="Black" />
</Grid>

但问题是我没有看到 textBlock,我也不知道为什么。 谁能给个提示?

【问题讨论】:

  • 你的页面方向是什么(横向)?
  • SupportedOrientations="Portrait" Orientation="Portrait"
  • 我有分析器,我必须使用 UIElementRenderer
  • 相当基础 - MSDN 上的 SL/XNA 代码示例应该会引导您完成所有这些。

标签: silverlight windows-phone-7 xaml xna


【解决方案1】:

你的代码看起来像这样吗?

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <Grid Height="800" Name="grid1" Grid.Row="1" Width="480" Background="White">
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock1" Text="Lifes: 3" VerticalAlignment="Top" Foreground="Black" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="367,12,0,0" Name="textBlock2" Text="Points: 0" VerticalAlignment="Top" Foreground="Black" />
    </Grid>
</Grid>

因为我对此进行了测试并且效果很好。结果如下:

更新:

否则您应该提供一些可能导致问题的其他代码。

【讨论】:

  • 不错的猜测,因为问题中缺少信息,但 OP 说他忘记使用 UIElementRenderer
  • 抱歉,没看到评论...-_-'
【解决方案2】:

提示:最初的问题文本中有错字。 @gurehbgui 问他为什么没有看到 textBox(他指的是 TextBlock)。这就是这个答案在这里的原因。错字被修复后,这个答案变得不再相关。

您正在使用TextBlock 控件。

您应该使用TextBox 为文本元素设置一个编辑字段。

此外,由于前景设置为黑色,因此控件在黑色背景上可能不可见。您可以使用 Silverlight Spy 应用程序检查 WP7 GUI 布局并调试此类问题。间谍支持试用期。

【讨论】:

  • 我想要一个文本块(如标签)。我为什么要使用文本框?不希望用户输入。
  • 在你的问题中你明确提到“但问题是我没有看到文本框,我不知道为什么。”我认为在您否决帮助您的尝试之前,您应该更加小心问题描述;)
  • @MaximV.Pavlov 为我的努力+1 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多