Search code examples
c#windowsuwpuwp-xamlwinui

UWP nested RelativePanel app crash pointing to global::System.Diagnostics.Debugger.Break();


I am trying to build an UWP app using nested RelativePanel layout components as part of a DataTemplate used for a ListView:

               <ListView x:Name="Listtest" Grid.Row="1"
                          SelectionMode="Single"
                          ScrollViewer.VerticalScrollBarVisibility="Visible">

                    <ListView.ItemTemplate>

                        <DataTemplate>
                            <RelativePanel>

                                <Grid RelativePanel.AlignVerticalCenterWithPanel="True" x:Name="gridVoto">
                                    <Ellipse x:Name="Ellipse"
                                             Grid.RowSpan="2"
                                             Width="42"
                                             Height="42"
                                             Margin="2"
                                             Fill="{Binding decimalValue, Converter={StaticResource GradeToColorConverter}}"

                                             VerticalAlignment="Center"
                                             HorizontalAlignment="Center">
                                        <!-- ="{ThemeResource SystemControlBackgroundBaseMediumBrush}" -->
                                    </Ellipse>
                                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"
                                               Text="{Binding displayValue}" FontSize="14" Foreground="#FFFFFF" />

                                </Grid>

                                <RelativePanel >
                                    <TextBlock x:Name="textMateria" TextWrapping="Wrap" RelativePanel.AlignVerticalCenterWithPanel="True"
                                           MaxWidth="500"
                                           Text="{Binding subjectDesc}"

                                           Style="{ThemeResource BaseTextBlockStyle}"
                                           Margin="12,6,0,0" />

                                <TextBlock RelativePanel.Below="textMateria" RelativePanel.RightOf="gridVoto"
                                           TextWrapping="Wrap"

                                           Text="{Binding notesForFamily}"

                                           Style="{ThemeResource BodyTextBlockStyle}"
                                           Margin="12,0,0,6" />

                                <TextBlock RelativePanel.RightOf="textMateria" x:Name="dataVoto"
                                           Text="{Binding evtDate}"

                                           Foreground="LightGray"
                                           Style="{ThemeResource BaseTextBlockStyle}"
                                           Margin="12,6,0,0" />

                                <TextBlock RelativePanel.RightOf="dataVoto"
                                           Text="{Binding componentDesc}"


                                           Style="{ThemeResource BodyTextBlockStyle}"
                                           Foreground="LightGray"
                                           Margin="12,6,0,0" />

                                </RelativePanel>



                            </RelativePanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

When I run the app in Visual Studio it crashes without any clear exception error, but a line of code highlighted: global::System.Diagnostics.Debugger.Break();.

I tried using other layout components like Grid or StackPanel as child of the main RelativePanel, they work but they don't suit my needs like RelativePanel would.


Solution

  • The reason for your behavior is the second TextBlock in the nested RelativePanel. You are setting the RelativePanel.RightOf to gridVoto. The gridVoto is the Grid control that out of the main RelativePanel, not the nested RelativePanel. So the XAML compiler can't find this element in the child elments of nested RelativePanel

      <TextBlock RelativePanel.Below="textMateria" RelativePanel.RightOf="gridVoto"
                                           TextWrapping="Wrap"
    
                                           Text="{Binding notesForFamily}"
    
                                           Style="{ThemeResource BodyTextBlockStyle}"
                                           Margin="12,0,0,6" />
    

    Please just remove RelativePanel.RightOf="gridVoto" in the code. If you want put that TextBlock in the right of the Grid, you should place it in the main RelativePanel but out of the nested RelativePanel.