在添加更改GridView模板的选项后,当旋转设备时应用程序崩溃

本文关键字:旋转 崩溃 应用程序 选项 添加 GridView | 更新日期: 2023-09-27 18:14:34

我有一个应用程序,有一个GridView绑定到sampledata。我有一个按钮,可以切换布局(列表,网格)的GridView。它工作得很好,但在我改变数据模板后旋转手机的那一刻,应用程序崩溃了。我在App.g.i.cs文件中得到这个:

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

我已经把问题缩小到这段代码:

FontIcon fi = new FontIcon(); 
fi.Glyph = "'uE80A"; 
ViewItems.Icon = fi; 
ViewItemsBottom.Icon = fi; 

我不知道为什么。我的appbarbutton有一个自定义的符号作为图标,这就是我改变它的地方。当我在使用上述代码更改后旋转手机时,应用程序崩溃了。如果我注释掉这部分代码,就不会崩溃。有人知道怎么解决这个问题吗?


我改变模板的方式是我在Page资源中定义了两个datatemplate:

<Page.Resources>
    <DataTemplate x:Key="WideTile" x:Name="WideTile">
        <StackPanel Width="300" Height="80" Margin="0,0,0,0" Orientation="Horizontal"
                               Background="{ThemeResource MainBackgroundColor}" 
                               BorderBrush="#11000000"
                               BorderThickness="0,0,2,2"
                               Padding="10">
            <StackPanel Margin="10,0,0,0" Width="40" Height="40" HorizontalAlignment="Center" VerticalAlignment="Center">
                <BitmapIcon Width="40" Height="40" 
                                        UriSource="Assets/recipeicon.png" Foreground="{Binding IconColor}"/>
            </StackPanel>
            <Grid Width="200" Height="80" Margin="20,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center">
                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
                                       Text="{Binding RecipeName}" 
                                       Foreground="White"
                                       FontSize="16"/>
            </Grid>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="SquareTile" x:Name="SquareTile">
        <StackPanel Width="150" Height="150" Margin="0,0,0,0" Orientation="Vertical"
                               Background="{ThemeResource MainBackgroundColor}" 
                               BorderBrush="#11000000"
                               BorderThickness="0,0,2,2"
                               Padding="10">
            <StackPanel Margin="0,30,0,0" Width="130" Height="110" HorizontalAlignment="Center" VerticalAlignment="Center">
                <BitmapIcon Width="50" Height="50" 
                                        UriSource="Assets/recipeicon.png" Foreground="{Binding IconColor}"/>
            </StackPanel>
            <Grid Width="150" Height="20" Margin="0,-30,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
                <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
                                       Text="{Binding RecipeName}" 
                                       Foreground="White"
                                       FontSize="12"/>
            </Grid>
        </StackPanel>
    </DataTemplate>
</Page.Resources>

,我有一个按钮调用这段代码进行更改:

private void ViewItems_Click(object sender, RoutedEventArgs e)
    {
        if (gridViewStyle == "0")
        {
            gridViewStyle = "1";
        } else if (gridViewStyle == "1")
        {
            gridViewStyle = "0";
        }
        setGridViewLayout();
        localSettings.Values["gridViewStyle"] = gridViewStyle;
    }

    private void setGridViewLayout()
    {
        if (gridViewStyle == "0")
        {
            DataTemplate tmpl;
            tmpl = WideTile;
            recipeGridView.ItemTemplate = tmpl;
            FontIcon fi = new FontIcon();
            fi.Glyph =  "'uE80A";
            ViewItems.Icon = fi;
            ViewItems.Label = "Square";
            ViewItemsBottom.Icon = fi;
            ViewItemsBottom.Label = "Square";
        }
        else if (gridViewStyle == "1")
        {
            DataTemplate tmpl; 
            tmpl = SquareTile;
            recipeGridView.ItemTemplate = tmpl;
            FontIcon fi = new FontIcon();
            fi.Glyph = "'uE292";
            ViewItems.Icon = fi;
            ViewItems.Label = "Wide";
            ViewItemsBottom.Icon = fi;
            ViewItemsBottom.Label = "Wide";
        }
    }

大部分代码只是改变appbar图标的字形和文本取决于他们目前在什么视图,但它的目的是我这样做是为了改变数据模板:

DataTemplate tmpl;
tmpl = WideTile;
recipeGridView.ItemTemplate = tmpl;

如果我加载应用程序,不改变gridview,当我旋转手机,没有崩溃。在我手动分配数据模板的那一刻,当我旋转它时,手机崩溃了。

如果你很好奇,这是我的可视化状态管理器XAML当我旋转手机时被调用:

<VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <!-- Wider than 600px -->
            <VisualState x:Name="WideState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="600" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="TheCommandBar.Visibility" Value="Visible" />
                    <Setter Target="TheCommandBarBottom.Visibility" Value="Collapsed" />
                </VisualState.Setters>
            </VisualState>
            <!-- Narrower than 600px -->
            <VisualState x:Name="NarrowState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="TheCommandBar.Visibility" Value="Collapsed" />
                    <Setter Target="TheCommandBarBottom.Visibility" Value="Visible" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

在添加更改GridView模板的选项后,当旋转设备时应用程序崩溃

你可以,你只是不能重复使用同一个FontIcon。

下面的代码运行正常:

FontIcon fi = new FontIcon();
FontIcon fi2 = new FontIcon();
fi.Glyph = "'uE80A";
fi2.Glyph = "'uE80A";
ViewItems.Icon = fi;
ViewItemsBottom.Icon = fi2;