c# – UWP App在返回上一页时随机崩溃

c# – UWP App在返回上一页时随机崩溃,第1张

概述我有一个应用程序,我列出了一些项目,然后更改为另一个页面进行编辑.在编辑页面中,我有一些字段,我需要从列表中选择一个值,我在另一个页面中加载.我从编辑页面调用列表页面,从编辑页面视图模型传递一个方法,以便在进行选择时调用,以便我可以更新我的模型和绑定. 问题是,有时候,当返回到编辑页面时,调试器会在生成的文件App.g.i.cs中打破 "Error HRESULT E_FAIL has been 我有一个应用程序,我列出了一些项目,然后更改为另一个页面进行编辑.在编辑页面中,我有一些字段,我需要从列表中选择一个值,我在另一个页面中加载.我从编辑页面调用列表页面,从编辑页面视图模型传递一个方法,以便在进行选择时调用,以便我可以更新我的模型和绑定.

问题是,有时候,当返回到编辑页面时,调试器会在生成的文件App.g.i.cs中打破

"Error HRESulT E_FAIL has been returned from a call to a COM component." exception.

它在打破之前完全显示具有正确绑定和选定值的页面.

我通过将其替换为虚拟页面并删除编辑页面设置的任何回调,从值列表的异步加载中排除了可能的错误.错误仍然发生.
编辑页面的导航缓存模式设置为必需,因此我不会丢失以前的更改.我尝试没有所需的缓存模式,仍然得到错误.

似乎解决这个问题的唯一一件事就是我从编辑页面xaml中删除了使用自定义转换器的绑定,但我在其他页面中有那些并且从未遇到过问题. (见更新)

调试器在生成的代码中断:

#if DEBUG && !disABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION        UnhandledException += (sender,e) =>        {            if (global::System.Diagnostics.DeBUGger.IsAttached) global::System.Diagnostics.DeBUGger.Break();        };

这是编辑页面的xaml:

<Page.Resources>    <converters:BooleanToVisibilityConverter x:Key="BoolToVisibility" />    <converters:StringFormatConverter x:Key="StringFormat" />    <converters:DateTimetoDateTimeOffsetConverter x:Key="DateOffset" />    <converters:DateTimetoTimeSpanConverter x:Key="TimeSpan" /></Page.Resources><GrID Background="{themeResource ApplicationPageBackgroundthemeBrush}">    <GrID.ColumnDeFinitions>        <ColumnDeFinition WIDth="*" />    </GrID.ColumnDeFinitions>    <GrID.RowDeFinitions>        <RowDeFinition Height="auto" />        <RowDeFinition Height="auto" />        <RowDeFinition Height="*" />    </GrID.RowDeFinitions>    <fIEldSelectControl:BackbuttonAndTitle title="Page Title" />    <StackPanel GrID.Row="1" OrIEntation="Horizontal">        <button name="Homebutton"                WIDth="70"                Height="50"                Background="Black"                Content="&#xE80F;"                FontFamily="Segoe MDL2 Assets"                FontSize="30" />        <TextBlock name="PageTitle"                   FontSize="36"                   Text="{Binding OrderTitle}" />    </StackPanel>    <ProgressRing GrID.Row="2" IsActive="{Binding IsLoading}" />    <ScrollVIEwer GrID.Row="2"                  Height="auto"                  HorizontalScrollbarVisibility="Disabled"                  VerticalScrollbarVisibility="auto"                  Visibility="{Binding Path=FinishedLoading,Converter={StaticResource BoolToVisibility}}">        <StackPanel margin="20,10,20,30">            <TextBlock margin="0,5,5"                       Style="{StaticResource DetailLabel}"                       Text="Date" />            <DatePicker Date="{Binding Day,Mode=TwoWay,Converter={StaticResource DateOffset}}" />            <TextBlock margin="0,15,5"                       Style="{StaticResource DetailLabel}"                       Text="Type" />            <ComboBox ItemsSource="{Binding Combovalues}" SelectedItem="{Binding SelectedHourType,Mode=TwoWay}" />            <TextBlock margin="0,5"                       Style="{StaticResource DetailLabel}"                       Text="Start" />            <TimePicker ClockIDentifIEr="24HourClock" Time="{Binding StartTime,Converter={StaticResource TimeSpan}}" />            <TextBlock margin="0,5"                       Style="{StaticResource DetailLabel}"                       Text="End" />            <TimePicker ClockIDentifIEr="24HourClock" Time="{Binding EndTime,5"                       Style="{StaticResource DetailLabel}"                       Text="Amount" />            <TextBox inputScope="Number" Text="{Binding HValue,Mode=TwoWay}" />            <fIEldSelectControl:FIElDWithIconSelector x:name="fsSelect"                                                      IconClickedEvent="btnClose_Click"                                                      IconFIEld="&#xE109;"                                                      TitleFIEld="Add..."                                                      TitleIconFIEld=""                                                      ValueFIEld="" />            <ScrollVIEwer Height="auto"                          HorizontalScrollbarVisibility="Disabled"                          VerticalScrollbarVisibility="auto">                <ListVIEw x:name="lstOrders"                          Height="auto"                          MaxHeight="600"                          HorizontalAlignment="Stretch"                          IsItemClickEnabled="True"                          ItemClick="lstOrders_ItemClick"                          ItemsSource="{Binding SelectedEmployees,Mode=OneWay}">                    <ListVIEw.ItemTemplate>                        <DataTemplate x:DataType="data:Responsible">                            <fIEldSelectControl:FIElDWithIconSelector x:name="fsEmployees"                                                                      IconClickedEvent="fsEmployees_IconClickedEvent"                                                                      IconFIEld=""                                                                      TitleFIEld=""                                                                      TitleIconFIEld=""                                                                      ValueFIEld="{Binding name}" />                        </DataTemplate>                    </ListVIEw.ItemTemplate>                    <ListVIEw.ItemContainerStyle>                        <Style targettype="ListVIEwItem">                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />                        </Style>                    </ListVIEw.ItemContainerStyle>                </ListVIEw>            </ScrollVIEwer>            <button name="btnSave"                    margin="0,15"                    HorizontalAlignment="Stretch"                    Click="btnSave_Click"                    Visibility="{Binding Path=FinishedSaving,Converter={StaticResource BoolToVisibility}}">                <button.Template>                    <ControlTemplate>                        <StackPanel HorizontalAlignment="Stretch"                                    VerticalAlignment="Stretch"                                    Background="#0099cc"                                    OrIEntation="Vertical"                                    padding="5">                            <TextBlock HorizontalAlignment="Center"                                       FontFamily="Segoe MDL2 Assets"                                       FontSize="25"                                       Text="&#xE74E;" />                            <TextBlock HorizontalAlignment="Center" Text="Save" />                        </StackPanel>                    </ControlTemplate>                </button.Template>            </button>        </StackPanel>    </ScrollVIEwer></GrID>

DateOffset转换器的代码:

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,string language)    {        DateTimeOffset dto = (DateTimeOffset)value;        return dto.DateTime;    }}

TimeOffset转换器的代码:

public class DateTimetoTimeSpanConverter : IValueConverter{    public object Convert(object value,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,string language)    {        TimeSpan ts = (TimeSpan)value;        DateTime dt = new DateTime(ts.Ticks);        return dt;    }}

鉴于调试器中断的代码,错误似乎是在xaml端,正如我所说,删除日期和时间选择器,这是绑定使用自定义转换器的唯一元素,似乎解决它.

我错过了什么?

UPDATE

进一步的测试引起了我的注意,删除自定义转换器实际上并没有解决问题,它只是增加了导航的数量,直到错误发生.我已经尝试完全删除绑定仍然得到错误.删除每个元素,但调用下一页的按钮似乎工作,但此时我无法确定它是否实际修复了问题或只是缓解了它.

另一方面,使用手机返回按钮导航回第一页可以完美无瑕地工作.只有在显式调用Frame.GoBack()时才会出现错误.

解决方法 这些异常并没有提供太多关于导致它们的提示,但是有一些事情可以尝试:

>我将从lstOrders周围删除ScrollVIEwer.它没有添加任何值,但它可能会破坏ListVIEw的虚拟化,导致它尝试使用任意数量的内存和cpu,如果您不小心删除了lstOrders上设置的MaxHeight.> btnSave模板很糟糕,因为它删除了交互式反馈元素.>确保未绑定UI / dispatcher线程所绑定的属性.

总结

以上是内存溢出为你收集整理的c# – UWP App在返回上一页时随机崩溃全部内容,希望文章能够帮你解决c# – UWP App在返回上一页时随机崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1226931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存