简体   繁体   中英

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

Problem solved. See the comments from FayWang below.

Added info.

Ok. I added some code to delete the items. Deleting (one at a time). When seeing one image .. all 25 are being created to the same spot. The problem is in the Sub to create the new images. Some of them have no image, like the source was not set. Doesn't explain why they all have the same coordinates. And then the next load may work properly, or not.


I am probably going to feel really stupid, but....

I just built a quick and short program (C# UWP VS 2019). It is going to build 25 images to the screen. I was going to add a remove them all button (trying to find a memory leak) and add them back but never got that far.

The program compiles no errors. I run it. Usually, instead of 25 images, I SEE 1. On the first run. Just run it again (no rebuild or code changes) and I may get the 25. I cant tell what will happen.

After the last build, in 10 runs, I got 1, 25, 1, 25, 25, 1, 25, 25, 25, 1

Also, on the 1 image ones. They seem to be in different places.

I am putting the full code below. I often make mistakes. But this is just re-running the code.

Also, I just re-installed Visual studio this morning too.

一个笑脸


在此处输入图片说明


First the Xaml inside the Page tag, then the code behind

<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);
        }
    }

When you explicitly set the width of the image, it's better to set DecodePixelWidth property of BitmapImage. it will load the BitmapImage with the specified width. Helps with memory usage and speed when loading large images.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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