加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:00:10 所属栏目:百科 来源:网络整理
导读:我有一个应用程序,我列出了一些项目,然后更改为另一个页面进行编辑.在编辑页面中,我有一些字段,我需要从列表中选择一个值,我在另一个页面中加载.我从编辑页面调用列表页面,从编辑页面视图模型传递一个方法,以便在进行选择时调用,以便我可以更新我的模型和绑
我有一个应用程序,我列出了一些项目,然后更改为另一个页面进行编辑.在编辑页面中,我有一些字段,我需要从列表中选择一个值,我在另一个页面中加载.我从编辑页面调用列表页面,从编辑页面视图模型传递一个方法,以便在进行选择时调用,以便我可以更新我的模型和绑定.

问题是,有时候,当返回到编辑页面时,调试器会在生成的文件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线程所绑定的属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读