首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UWP中的System.DivideByZeroException

UWP中的System.DivideByZeroException
EN

Stack Overflow用户
提问于 2019-02-11 01:52:47
回答 1查看 137关注 0票数 0

所以,我对我的microsoft visual studio做了一些更新。之前,我的UWP flipview工作得很好。基本上,我的Flipview从本地图片库读取图像。现在如果我运行我的UWP,这个异常就会出现!

代码语言:javascript
复制
System.DivideByZeroException
  HResult=0x80020012
  Message=Attempted to divide by zero.
  Source=TMD_Latest
  StackTrace:
   at TMD_Latest.Views.MainPage.ChangeImage(Object sender, Object o) in C:\Users\alish\source\repos\TMD_Latest\TMD_Latest\Views\MainPage.xaml.cs:line 97



 private void ChangeImage(object sender, object o)
        {

            //Get the number of items in the flip view
            var totalItems = TheFlipView.Items.Count;
            //Figure out the new item's index (the current index plus one, if the next item would be out of range, go back to zero)


//This line below is the exception!
            var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems;

            //Set the displayed item's index on the flip view

            TheFlipView.SelectedIndex = newItemIndex;
        }

我的xaml:

代码语言:javascript
复制
<Grid VariableSizedWrapGrid.ColumnSpan="5"
                  VariableSizedWrapGrid.RowSpan="5"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Padding="0 30 20 20"
                   Margin="200,-100,0,10"
                  Background="Transparent">
                <x21:Grid.RowDefinitions>
                    <x21:RowDefinition Height="405*"/>
                    <x21:RowDefinition Height="21*"/>
                    <x21:RowDefinition Height="425*"/>
                </x21:Grid.RowDefinitions>
                <FlipView x:Name="TheFlipView"
            SelectionChanged="DisplayedItemChanged" Margin="-235,205.6,30,-1221.6" x21:Grid.Row="2"  >
                    <FlipView.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,0,10" >
                                <Image HorizontalAlignment="Center"  VerticalAlignment="Stretch"  Source="{Binding}"
                        Stretch="Fill" Margin="0,-200,0,0" />
                            </Grid>
                        </DataTemplate>
                    </FlipView.ItemTemplate>
                </FlipView>

            </Grid>

请帮助我:(

EN

回答 1

Stack Overflow用户

发布于 2019-02-11 02:09:22

正如@Abestrad提到的,这是因为余数运算符的右侧为零。如前所述,here

x % y的结果是x - (x / y) * y产生的价值。如果y_System.DivideByZeroException为零,则抛出异常。

在您的情况下,解决此问题的一种方法是将余数运算符包装在if条件中:

代码语言:javascript
复制
if (totalItems > 0)
{
    var newItemIndex = (TheFlipView.SelectedIndex + 1) % totalItems; 

    //Set the displayed item's index on the flip view 
    TheFlipView.SelectedIndex = newItemIndex;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54619269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档