【问题标题】:How to set a text in TextBlock.如何在 TextBlock 中设置文本。
【发布时间】:2014-03-01 05:17:13
【问题描述】:

如何使用 c# 在 textblock 定义的 xaml 中设置文本。我正在重定向到一个电话页面,其中有一个堆栈面板和几个文本块。如果我命名一个文本块并在 c# 代码中调用它,它就不起作用。页面加载时如何在文本块中设置字符串...并且文本是从上一页传递过来的。基本上无法识别文本块...如何使其在 c# 代码中可见...

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="16,0,12,0">
    <StackPanel x:Name="activationPanel" Margin="0,10">
        <TextBlock TextWrapping="Wrap" Text="Almost there !" FontFamily="Segoe UI" FontWeight="Light" FontSize="24" Foreground="#FF486070"/>
        <TextBlock TextWrapping="Wrap" Text="An activation mail has been sent" FontFamily="Segoe UI" FontWeight="Light" FontSize="24" Foreground="#FF486070" Margin="0,18,0,0"/>
        <TextBlock Name="useremail" TextWrapping="Wrap" FontFamily="Segoe UI" FontWeight="SemiBold" FontSize="24" Foreground="#FF486070" Margin="0,10,0,0" LostFocus="useremail_LostFocus"/>
        <TextBlock TextWrapping="Wrap" Text="please click on the link to activate your account" FontFamily="Segoe UI" FontWeight="Light" FontSize="24" Foreground="#FF486070" Margin="0,50,78,0"/>
        <Button x:Name="btnAcA" Content="ACCOUNT ACTIVATED" Width="335" HorizontalAlignment="Left" Height="42" Padding="12,0,10,0" Background="#FF55D782" BorderBrush="{x:Null}" BorderThickness="0" FontSize="16" FontFamily="Segoe UI" Style="{StaticResource LoginButtonStyle}" Margin="0,90,0,25" Click="btnAcA_Click"/>
        <Button x:Name="btnRAM" Content="RESEND ACTIVATION MAIL" Width="335" HorizontalAlignment="Left" Height="42" Padding="12,0,10,0" Background="#FF637A8A" BorderBrush="{x:Null}" BorderThickness="0" FontSize="16" FontFamily="Segoe UI" Style="{StaticResource LoginButtonStyle}" Margin="0,0,0,25"/>
    </StackPanel>
</Grid>

C#:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    string email=null;
    if (this.NavigationContext.QueryString.ContainsKey("email"))
    {
        email = this.NavigationContext.QueryString["email"];
    }

    Debug.WriteLine(email);
    setTextBoxValue(email);
}

private void setTextBoxValue(string email)
{
    //TextBlock useremail = new TextBlock();
    useremail.Text = email;
}

【问题讨论】:

  • “useremail”标记是否与代码隐藏在同一类中(即,它不在嵌套控件/用户控件中)?
  • 我尝试将 x:useremail 用于文本块。尤其是对于 TextBlock,它在 c# 代码中无法识别。对于文本框 x:somename 运行良好。知道吗,TextBlock 有什么特别之处?
  • 您可以访问btnRAM 吗?像这样btnRAM.Content = totoin setTextBoxValue
  • 不确定嵌套控件。我的页面 a.xaml 包含我粘贴的代码,而 a.xaml.cs 是我指的文本块,如 useremail.Text,无法识别。
  • 你还有别的东西。我试试你的代码,它可以工作。

标签: c# wpf xaml windows-phone-8


【解决方案1】:

看看this 的问题。根据接受的答案,原因可能是:

  1. XAML 中 x:Class 的命名空间与 C# 中的 Class 的命名空间不匹配或
  2. XAML 的错误构建操作(应该是页面)。

祝你好运。

【讨论】:

    【解决方案2】:

    我只是不知道发生了什么。我只是重启了整台机器,错误就消失了。

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 2010-12-12
      • 1970-01-01
      • 2011-01-18
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      相关资源
      最近更新 更多