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

时间:2015-09-26 12:13:33

标签: c# windows xaml uwp

我有一个将GridView绑定到samplingata的应用程序。我有一个按钮,可以切换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资源中定义了两个DataTemplates:

<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:

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

如果我加载应用程序并且不改变网格视图,当我旋转手机时没有崩溃。 MINUTE我手动分配了数据模板,当我旋转时手机崩溃。

如果你很好奇,这是我旋转手机时调用的视觉状态管理器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>

1 个答案:

答案 0 :(得分:1)

您可以,您只能重复使用相同的FontIcon两次。

这段代码很好用:

FontIcon fi = new FontIcon();
FontIcon fi2 = new FontIcon();

fi.Glyph = "\uE80A";
fi2.Glyph = "\uE80A";

ViewItems.Icon = fi;
ViewItemsBottom.Icon = fi2;