atwalsh
atwalsh

Reputation: 3722

TextBlock causes error with InitializeComponent()

I have this XAML code inside my MainPage.xaml which creates row and column definitions and a TextBlock:

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="layoutGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="TEST" 
                   Grid.Row="1" 
                   Grid.Column="1" 
                   Height="23" 
                   HorizontalAlignment="Right" 
                   Margin="0,45,70,0" 
                   x:Name="Test TextBlock"/>

    </Grid>

</Page>

If the TextBlock is present, I get errors in my MainPage.xaml.cs:

.xaml.cs errors

I can't figure out what is wrong with this code. If I comment out the TextBlock on the .xaml page, the error goes away. Here is the complete code with screenshots showing the problems I am having.

Upvotes: 0

Views: 55

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124642

x:Name="Test TextBlock"

x:Name defines the variable name for that object. Test TextBlock is not a valid identifier. Use TestTextBlock. Also, you could have taken a peek at InitializeComponent, or read your error message. The error in your XAML file literally says (look at the blue squigglies):

'Test TextBlock' is not a valid value for property 'Name'

Upvotes: 4

Related Questions