0

How to set a text in textblock defined xaml using c#. I am redirecting to a phone page where I have a stackpanel and couple of textblocks inside. If I name a text block and call it in the c# code, it is not working. How to set the string in the text block when the page is loaded... And the text is passed from the previous page. Basically the textblock in not recognised... How to make it visible in c# code...

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;
}
venerik
  • 5,486
  • 1
  • 29
  • 41
user88975
  • 560
  • 1
  • 7
  • 18
  • Is the "useremail" markup in the same class as the code-behind (ie, it's not in a nested control/user control)? – McGarnagle Feb 27 '14 at 19:46
  • I tried x:useremail for textblock. Very particularly for TextBlock it is not recognised in the c# code. For text box x:somename is working well. Any idea, what is special in TextBlock? – user88975 Feb 27 '14 at 19:47
  • Can you access to `btnRAM` ? Something like this `btnRAM.Content = toto`in `setTextBoxValue` ? – aloisdg Feb 27 '14 at 19:49
  • Not sure about nested control. I have page a.xaml which contains the code I pasted, and a.xaml.cs is where I am referring a the textblock like useremail.Text, which is not recognised. – user88975 Feb 27 '14 at 19:50
  • Yes, I can access btnRam, even I can add event like lostfocus to textblock but I cannot access it to just set the text value. – user88975 Feb 27 '14 at 19:51
  • c# code says, the name "useremail" does not exist in the current context – user88975 Feb 27 '14 at 20:07
  • 1
    You have something else. I try your code and it works. – aloisdg Feb 27 '14 at 20:14

2 Answers2

0

Have a look at this question. According to the accepted answer the cause can be:

  1. Mismatch between namespace of x:Class in XAML and Class in C# or
  2. Wrong build action (should be page) for XAML.

Good luck.

Community
  • 1
  • 1
venerik
  • 5,486
  • 1
  • 29
  • 41
0

I just don't know what happened. I simply restarted the whole machine and the error is gone.

user88975
  • 560
  • 1
  • 7
  • 18