1

I created a new UWP app, something I've never done before, and copy and pasted a bunch of code from another Xamarin Forms project, code that did work there. Now I run into the error:

System.InvalidOperationException: 'You must call Xamarin.Forms.Forms.Init(); prior to using this property.

"this property" being a chuck of code meant to create a "refresh rate" so that I can create movement of SkiaSharp graphics: (if theres a better way to do this please let me know)

Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
            {
                canvasView.Invalidate();
                return true;
            });

I don't necessarily just want the code to make this work but I genuinely would like to understand what's happening. I can't just insert Forms.Init(); into my code because that requires parameters, but I don't know what parameters to feed into it. I don't necessarily just want the code to make this work but I genuinely would like to understand what's happening. Any help is appreciated.

My code:

using RPS_Sim;
using RPS_Sim.Objects;
using SkiaSharp;
using SkiaSharp.Views.UWP;
using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace RPS_Sim
{
    //Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    public partial class MainPage
    {

        int speed = 1, size = 50;
        PosMov posMov;
        Rock rock;
        Paper paper;
        Scissors scissor;
        List<Object> Objects;


        int w, h;
        public MainPage()
        {
            DependencyService.Register<MainPage>();
            InitializeComponent();
           
            Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
            {
                canvasView.Invalidate();
                return true;
            });

            posMov = new PosMov(0, 0, speed, size);

            rock = new Rock(posMov.x, posMov.y, posMov.speed, posMov.size);
            paper = new Paper(posMov.x, posMov.y, posMov.speed, posMov.size);
            scissor = new Scissors(posMov.x, posMov.y, posMov.speed, posMov.size);

            Objects = new List<Object>();


            Objects.Add(rock);
            Objects.Add(paper);
            Objects.Add(scissor);

            foreach (var O in Objects)
            {
                O.Randomize(1);
            }
        }
        private void canvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)//, MouseButtonEventArgs mouse)            //put all realtime events here
        {
            
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;
            canvas.Clear(SKColors.White);
            w = e.Info.Width / 2;
            h = e.Info.Height / 2;
            canvas.Translate(w, h);

            /*foreach (var O in Objects) 
            {
                O.Update(canvas, w, h);
            }*/

            rock.Update(canvas, w, h);
            paper.Update(canvas, w, h);
            scissor.Update(canvas, w, h);
        }

    }
}
1
  • You don't insert Xamarin Forms Init into THAT code. If you create a new Xamarin.Forms project, it "should" have the Init in the correct place, inside UWP-specific app startup code. Did you make new project, choosing Xamarin.Forms "project template", and checked "UWP" as one of the desired platforms? If so, please search and see if that call is ANYWHERE in the UWP-specific code. (If you didn't create the project that way, start over, with that project template.) Commented Dec 23, 2022 at 6:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.