Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

VS 2019 Community: compiler Errors using grid.Children.Add

$
0
0

I have a sub (Xamarin.Forms Mobile) that creates images in Code behind with 2 for loops. They are small images in Rows and Cols. I started with 10 rows X 46 cols. All of these are contained with in a Grid/ScrollView/AbsoluteLayout/ContentPage. The grid is used as a container as Images are positioned as offsets instead of grid Row x Col. There is probably a better container, but this is my first Xamarin coding. No Border or canvas tags.

OK. The **code below runs ** and performs as expected. I can scroll the images left and right. Then, I want more cols images. Change the inner for loop (cols) to 56. I picked a number big enough to cause this error and should still fit within the sizing that I have. This number changes depending .. i don't know what. You can get the same problem by increasing the rows. I also can get it my increasing the image size. Its some mix of the 3 things. _ I suspect it is a memory issue_.

  1. I was getting a message saying I have an unhandled exception and needed to enable JIT debugging. Ok, don't see why, but That is now done.
  2. I tried setting debugging to Mixed. Didn't help, so its set back.
  3. Change cols to 56. "A debugger is attached to APP.UWP.exe but not configured to debug this unhandled exception. To Debug this exception, detach the current debugger". Don't really know how to do that.. and why? It works at 46. The window gives me an option to select an available debugger, which is a new version of VS. Selecting that opens a new version of VS, but I don't know how to make use of that.
  4. If I don't close the "Chose Just-In-Time debugger, the process repeats and I have two of them. Seems like stopping the debug run would close the window. So, multiple little windows are on the screen. Close those, else I get a deployment issue.
  5. I added code so I could put a break when cols x rows was getting close to blowing up. I did that, and started stepping thru. It worked for a few more, up to 50 cols as I recall. That makes no sense to me.

So, after two weeks... I am getting no where. I posted this on StackOverflow, but no help there. (I just got access to post here a day or so ago)

The code below is the WORKING code. Adjust the for loops to get the error. (try 56)
The image is an Embedded Image, so there is a sub included to allow that. The image is a small right arrow.
I removed all the Header stuff, but the grid rows are still there.
In the Xaml, 1 image is defined, which proves the image is found.

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="Black"
             WidthRequest="800"                  
             xmlns:local="clr-namespace:ScKWander"
             x:Class="ScKWander.MainPage">
    <AbsoluteLayout x:Name="absLayMain" HeightRequest="750" WidthRequest="400" BackgroundColor="Black">
        <BoxView AbsoluteLayout.LayoutBounds="000, 000, 740, 350" BackgroundColor="GreenYellow"  />
        <BoxView AbsoluteLayout.LayoutBounds="002, 002, 736, 346" BackgroundColor="Black"  />

        <BoxView AbsoluteLayout.LayoutBounds="008, 030, 724, 273" BackgroundColor="GreenYellow"  />
        <BoxView x:Name="GameBoard" AbsoluteLayout.LayoutBounds="010, 032, 720,268" BackgroundColor="Black" />

        <!--HeightRequest="268" WidthRequest="714"-->
        <ScrollView Orientation="Horizontal"  BackgroundColor="Transparent" HorizontalScrollBarVisibility="Always"
                    HeightRequest="268" WidthRequest="714" 
                    TranslationY="32" TranslationX="14">

            <!--HeightRequest="268" WidthRequest="1714"-->
            <Grid x:Name="gridBoard" BackgroundColor="Indigo"  
              HeightRequest="268" WidthRequest="1714"
              TranslationY="0" TranslationX="0">

                <Grid.RowDefinitions>
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
            </Grid.RowDefinitions>

                <Image HeightRequest="15" WidthRequest="15"
                       Source="{local:ImageResource ScKWander.Images.UI.arrow_pointer_r.png}" 
                       HorizontalOptions="Start"
                       TranslationY="240" TranslationX="160"
                       />

            </Grid>
        </ScrollView>
    </AbsoluteLayout>        

</ContentPage>

CODE BEHIND

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Reflection;

namespace ScKWander
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            FillGrid();

            Title = "ScKWander";
            BackgroundColor = Color.Black;
        }

        private void FillGrid()
        {
            int iYoffset = 0;
            int iXoffset = 0;
            int iYGridsize = 25;
            int iXGridsize = 25;
            try
            {
                for (int iCount0 = 0; iCount0 < 10; iCount0++)
                {
                    for (int iCount1 = 0; iCount1 < 46; iCount1++)
                    {
                        if (iCount0 == 9 && iCount1 > 45) 
                        { int iXXX = 1; }

                        var thisImage = new Image
                        {
                            Source = ImageSource.FromResource(
                                "ScKWander.Images.UI.arrow_pointer_r.png",
                                typeof(ImageResourceExtension).GetTypeInfo().Assembly
                              ) ,
                            HorizontalOptions = LayoutOptions.Start
                        };

                        //thisImage.HorizontalOptions = LayoutOptions.Start};

                        thisImage.TranslationY = iYoffset + (iCount0 * iYGridsize);
                        thisImage.TranslationX = iXoffset + (iCount1 * iXGridsize);
                        thisImage.HeightRequest = iYGridsize;
                        thisImage.WidthRequest = iXGridsize;
                        thisImage.HorizontalOptions = LayoutOptions.Start;

                        gridBoard.Children.Add(thisImage);
                    }
                }
            }
            catch (Exception e)
            {
                string ex = e.Message;
            }
        }
    }
}

IMAGERESOURCEEXTENSION.CS

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Reflection;

namespace ScKWander
{
    [ContentProperty(nameof(Source))]
    class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }

            // Do your translation lookup here, using whatever method you require
            var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);

            return imageSource;
        }
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>