title: UWP App Randomly Crashes When Returning to Previous Page
body: <p>I have an app where I list some items and then change to another page for editing. In the edit page, I have some fields where i need to choose a value from a list which I load in another page. I call the list page from the edit page, passing a method from the edit page view model to be invoked when a selection is made so that I can update my model and bindings.</p>
  
  <p>The problem is that sometimes, when returning to the edit page, the debugger breaks in the generated file App.g.i.cs with an </p>
  
  <pre><code> "Error HRESULT E_FAIL has been returned from a call to a COM component." exception. 
  </code></pre>
  
  <p>It fully displays the page with the correct bindings and selected values before breaking.</p>
  
  <p>I've excluded possible errors from the async loading of the value list by replacing it with a dummy page and by removing any callbacks set by the edit page. The error still happened. 
  The edit page has its navigation cache mode set to Required so I don't lose the previous changes. I tried without a required cache mode and still got the error.</p>
  
  <p><s>The only thing that seemed to resolve the issue was when I removed the bindings that used custom converters from the edit page xaml but I have those in other pages and never had a problem.</s> (See Update)</p>
  
  <p>The debugger break in the generated code:</p>
  
  <pre><code>#if DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
          UnhandledException += (sender, e) =&gt;
          {
              if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
          };
  </code></pre>
  
  <p>This is the xaml for the edit page:</p>
  
  <pre><code>&lt;Page.Resources&gt;
      &lt;converters:BooleanToVisibilityConverter x:Key="BoolToVisibility" /&gt;
      &lt;converters:StringFormatConverter x:Key="StringFormat" /&gt;
      &lt;converters:DateTimeToDateTimeOffsetConverter x:Key="DateOffset" /&gt;
      &lt;converters:DateTimeToTimeSpanConverter x:Key="TimeSpan" /&gt;
  &lt;/Page.Resources&gt;
  
  &lt;Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="*" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="auto" /&gt;
          &lt;RowDefinition Height="auto" /&gt;
          &lt;RowDefinition Height="*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;fieldSelectControl:BackButtonAndTitle Title="Page Title" /&gt;
      &lt;StackPanel Grid.Row="1" Orientation="Horizontal"&gt;
          &lt;Button Name="HomeButton"
                  Width="70"
                  Height="50"
                  Background="Black"
                  Content="&amp;#xE80F;"
                  FontFamily="Segoe MDL2 Assets"
                  FontSize="30" /&gt;
          &lt;TextBlock Name="PageTitle"
                     FontSize="36"
                     Text="{Binding OrderTitle}" /&gt;
      &lt;/StackPanel&gt;
      &lt;ProgressRing Grid.Row="2" IsActive="{Binding IsLoading}" /&gt;
  
      &lt;ScrollViewer Grid.Row="2"
                    Height="Auto"
                    HorizontalScrollBarVisibility="Disabled"
                    VerticalScrollBarVisibility="Auto"
                    Visibility="{Binding Path=FinishedLoading,
                                         Converter={StaticResource BoolToVisibility}}"&gt;
  
          &lt;StackPanel Margin="20,10,20,30"&gt;
              &lt;TextBlock Margin="0,5,0,5"
                         Style="{StaticResource DetailLabel}"
                         Text="Date" /&gt;
              &lt;DatePicker Date="{Binding Day, Mode=TwoWay, Converter={StaticResource DateOffset}}" /&gt;
              &lt;TextBlock Margin="0,15,0,5"
                         Style="{StaticResource DetailLabel}"
                         Text="Type" /&gt;
              &lt;ComboBox ItemsSource="{Binding ComboValues}" SelectedItem="{Binding SelectedHourType, Mode=TwoWay}" /&gt;
  
              &lt;TextBlock Margin="0,15,0,5"
                         Style="{StaticResource DetailLabel}"
                         Text="Start" /&gt;
              &lt;TimePicker ClockIdentifier="24HourClock" Time="{Binding StartTime, Mode=TwoWay, Converter={StaticResource TimeSpan}}" /&gt;
  
              &lt;TextBlock Margin="0,5,0,5"
                         Style="{StaticResource DetailLabel}"
                         Text="End" /&gt;
              &lt;TimePicker ClockIdentifier="24HourClock" Time="{Binding EndTime, Mode=TwoWay, Converter={StaticResource TimeSpan}}" /&gt;
  
              &lt;TextBlock Margin="0,15,0,5"
                         Style="{StaticResource DetailLabel}"
                         Text="Amount" /&gt;
              &lt;TextBox InputScope="Number" Text="{Binding HValue, Mode=TwoWay}" /&gt;
  
  
  
              &lt;fieldSelectControl:FieldWithIconSelector x:Name="fsSelect"
                                                        IconClickedEvent="btnClose_Click"
                                                        IconField="&amp;#xE109;"
                                                        TitleField="Add..."
                                                        TitleIconField=""
                                                        ValueField="" /&gt;
  
              &lt;ScrollViewer Height="Auto"
                            HorizontalScrollBarVisibility="Disabled"
                            VerticalScrollBarVisibility="Auto"&gt;
                  &lt;ListView x:Name="lstOrders"
                            Height="auto"
                            MaxHeight="600"
                            HorizontalAlignment="Stretch"
                            IsItemClickEnabled="True"
                            ItemClick="lstOrders_ItemClick"
                            ItemsSource="{Binding SelectedEmployees,
                                                  Mode=OneWay}"&gt;
                      &lt;ListView.ItemTemplate&gt;
                          &lt;DataTemplate x:DataType="data:Responsible"&gt;
                              &lt;fieldSelectControl:FieldWithIconSelector x:Name="fsEmployees"
                                                                        IconClickedEvent="fsEmployees_IconClickedEvent"
                                                                        IconField=""
                                                                        TitleField=""
                                                                        TitleIconField=""
                                                                        ValueField="{Binding Name}" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListView.ItemTemplate&gt;
                      &lt;ListView.ItemContainerStyle&gt;
                          &lt;Style TargetType="ListViewItem"&gt;
                              &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt;
                          &lt;/Style&gt;
                      &lt;/ListView.ItemContainerStyle&gt;
                  &lt;/ListView&gt;
              &lt;/ScrollViewer&gt;
              &lt;Button Name="btnSave"
                      Margin="0,20,0,15"
                      HorizontalAlignment="Stretch"
                      Click="btnSave_Click"
                      Visibility="{Binding Path=FinishedSaving,
                                           Converter={StaticResource BoolToVisibility}}"&gt;
                  &lt;Button.Template&gt;
                      &lt;ControlTemplate&gt;
                          &lt;StackPanel HorizontalAlignment="Stretch"
                                      VerticalAlignment="Stretch"
                                      Background="#0099cc"
                                      Orientation="Vertical"
                                      Padding="5"&gt;
                              &lt;TextBlock HorizontalAlignment="Center"
                                         FontFamily="Segoe MDL2 Assets"
                                         FontSize="25"
                                         Text="&amp;#xE74E;" /&gt;
                              &lt;TextBlock HorizontalAlignment="Center" Text="Save" /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Button.Template&gt;
              &lt;/Button&gt;
          &lt;/StackPanel&gt;
  
  
  
      &lt;/ScrollViewer&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Code for the DateOffset Converter:</p>
  
  <pre><code>public class DateTimeToDateTimeOffsetConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, string language)
      {
          try
          {
              if (value is DateTime)
              {
                  DateTime date = (DateTime)value;
                  return new DateTimeOffset(date);
              }
              else
                  return new DateTimeOffset(DateTime.Now);
          }
          catch (Exception ex)
          {
              return new DateTimeOffset(DateTime.Now);
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, string language)
      {
          DateTimeOffset dto = (DateTimeOffset)value;
          return dto.DateTime;
      }
  }
  </code></pre>
  
  <p>Code for the TimeOffset Converter:</p>
  
  <pre><code>public class DateTimeToTimeSpanConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, string language)
      {
          try
          {
              if (value is DateTime)
              {
                  DateTime date = (DateTime)value;
                  return new TimeSpan(date.Ticks);
              }
              else
                  return new TimeSpan(DateTime.Now.Ticks);
          }
          catch (Exception ex)
          {
              return new TimeSpan(DateTime.Now.Ticks);
          }
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, string language)
      {
          TimeSpan ts = (TimeSpan)value;
          DateTime dt = new DateTime(ts.Ticks);
  
          return dt;
      }
  }
  </code></pre>
  
  <p>Given the code where the debugger breaks, the error seems to be on the xaml side and, as I said, removing the date and time pickers, which are the only elements where the binding uses a custom converter, seems to fix it.</p>
  
  <p>What am I missing?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Further testing has brought to my attention that removing the custom converters doesnt actually fix the issue, it just increases the number of navigations until the error happens. I've tried removing the bindings altogether and still got the error. Removing every element but the button that calls the next page seems to work but at this point I can't tell for sure if it actually fixes the issue or just mitigates it.</p>
  
  <p>On a side note, navigating back to the first page using the phone <code>Back</code> button works flawlessly. The error only seems to happen if <code>Frame.GoBack()</code> is explicitly invoked.</p>