繁体   English   中英

重新运行程序,得到不同的结果 C# UWP vs2019

[英]Re-Run the program and get different results C# UWP vs2019

问题解决了。 请参阅下面来自 FayWang 的评论。

添加了信息。

好的。 我添加了一些代码来删除项目。 删除(一次一个)。 当看到一张图片时,所有 25 张图片都被创建到同一个位置。 问题出在创建新图像的 Sub 中。 其中一些没有图像,例如未设置源。 没有解释为什么它们都具有相同的坐标。 然后下一个负载可能会正常工作,也可能不会。


我可能会觉得自己很愚蠢,但是......

我刚刚构建了一个快速而简短的程序(C# UWP VS 2019)。 它将在屏幕上构建 25 个图像。 我打算添加一个全部删除按钮(试图找到内存泄漏)并将它们添加回来,但从来没有那么远。

程序编译没有错误。 我运行它。 通常,我看到的不是 25 张图像,而是 1。在第一次运行时。 再次运行它(没有重建或代码更改),我可能会得到 25。我不知道会发生什么。

在最后一次构建之后,在 10 次运行中,我得到了 1, 25, 1, 25, 25, 1, 25, 25, 25, 1

此外,在 1 个图像上。 他们似乎在不同的地方。

我把完整的代码放在下面。 我经常犯错误。 但这只是重新运行代码。

另外,我今天早上也刚刚重新安装了 Visual Studio。

一个笑脸


在此处输入图片说明


首先是 Page 标签内的 Xaml,然后是后面的代码

<Grid>
    <Canvas x:Name="MyCanvas">
        <Rectangle x:Name="CutCopyRect"
                    Height="980" Width="1480"
                    Fill="LemonChiffon" 
                    Canvas.Left="10" Canvas.Top="10"/>
    </Canvas>

    public MainPage()
    {  this.InitializeComponent();
        DrawSomeImages();
    }

    void DrawSomeImages()
    {for (int iCt1 = 0; iCt1 < 5; iCt1++)
        { for (int iCt2 = 0; iCt2 < 5; iCt2++)  
            {  makeImage(iCt1, iCt2); }  
        }
    }

    void makeImage(int inTop, int inLeft)
    {   try
        { 
            Image img1 = new Image();
            img1.Source = new BitmapImage(new Uri("ms-appx:///Assets/SmileyAngry.png"));
            img1.Width = 220;
            img1.Height = 220;
            img1.SetValue(Canvas.TopProperty, (inTop * 38));
            img1.SetValue(Canvas.LeftProperty, (inLeft * 39));
            MyCanvas.Children.Add(img1);
        }
        catch (Exception ex)
        {
            string thisProc = System.Reflection.MethodBase.GetCurrentMethod().ToString();
            Debug.WriteLine("Error Message: ", ex.Message, "  In " + thisProc);
        }
    }

显式设置图片的宽度时,最好设置BitmapImage的DecodePixelWidth属性。 它将加载具有指定宽度的 BitmapImage。 加载大图像时有助于内存使用和速度。

Image img1 = new Image(); 
BitmapImage bi = new BitmapImage(new Uri(this.BaseUri, "ms-appx:///Assets/Logo.png")); 
img1.Width = bi.DecodePixelWidth = 110; 
img1.Height = bi.DecodePixelHeight = 110;
img1.SetValue(Canvas.TopProperty, (inTop * 38));
img1.SetValue(Canvas.LeftProperty, (inLeft * 39));
img1.Source = bi;
MyCanvas.Children.Add(img1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM