Error CS0070: The event 'Game1.OnLoadingContent' can only appear on the left hand side of += or -= (except when used from within the type 'Game1')

Kim Strasser 1,036 Reputation points
2024-06-18T09:01:42.58+00:00

I get an error if I want to call OnLoadingContent from another class than Game1.cs. I have tried to call it from Options.cs:

game1.OnLoadingContent?.Invoke(this, new EventArgs());

Error CS0070: The event 'Game1.OnLoadingContent' can only appear on the left hand side of += or -= (except when used from within the type 'Game1')

How can I call OnLoadingContent from Options.cs?

In my Android project:

Activity1.cs:

protected override void OnCreate(Bundle bundle)
{   
    base.OnCreate(bundle);   
    SetContentView(Resource.Layout.activity_main);   
    CrossMediaManager.Current.Init(this);    
    _game = new Game1();   
    _game.OnLoadingContent += G_OnLoadingContent;
    _view = _game.Services.GetService(typeof(View)) as View;
    SetContentView(_view);
    _game.Run();
}

private void G_OnLoadingContent(object sender, System.EventArgs e)
{  
    customDialog = new VideoDialog(this);  
    customDialog.SetCancelable(false);
    customDialog.Show();
    CrossMediaManager.Current.MediaItemFinished += (s, e) =>   
    {
        customDialog.Cancel();
    };
}

In my SharedCode project:

Game1.cs:

public event EventHandler OnLoadingContent;
public event EventHandler OnLoadingContentRemove;
Options MyOptions;
MyOptions = new Options(this, GraphicsDevice);
MyOptions.Load(Content);

Options.cs:

using System;
using MediaManager;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace SharedCode
{
public class Options
{
private Game1 game1;
GraphicsDevice graphicsDevice;

public Options(Game1 game, GraphicsDevice device)
{
    game1 = game;
    graphicsDevice = device;
}

public async void Load(ContentManager content)
{
    game1.OnLoadingContent?.Invoke(this, new EventArgs());
    await CrossMediaManager.Current.Play("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4");
}
}
}
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,110 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,487 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,431 Reputation points Microsoft Vendor
    2024-06-19T06:20:54.8533333+00:00

    Hello,

    Event handlers only can be added or removed within the Game1 class where the event is declared.

    public class Game1
    {
        public event EventHandler OnLoadingContent;
    
        public void SomeMethod()
        {
            // Subscribe to the event
            this.OnLoadingContent += YourEventHandlerMethod;
    
            // Unsubscribe from the event
            this.OnLoadingContent -= YourEventHandlerMethod;
        }
    
        private void YourEventHandlerMethod(object sender, EventArgs e)
        {
            // Event handling logic
        }
    }
    

    Based on your previous link, do you want to popup dialog and play the video by Game1 class in the Options.cs?

    If so, you can add an attribute in the Game1's constructor. Then create a method(called ShowTheVideo for testing) to popup a dialog and play the video. By the way, please remove the await CrossMediaManager.Current.Play("https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4"); in the LoadContent method, and add ShowTheVideo(); directly.

    public class Game1 : Game
      {
          private GraphicsDeviceManager _graphics;
          private SpriteBatch _spriteBatch;
          private Activity1 activity1;
          public Game1(Activity1 activity1)
          {
              this.activity1 = activity1;
              _graphics = new GraphicsDeviceManager(this);
              Content.RootDirectory = "Content";
              IsMouseVisible = true;
          }
    
        public async void ShowTheVideo()
        {
     
         VideoDialog customDialog = new VideoDialog(activity1);
         customDialog.SetCancelable(false);
         customDialog.Show();
         await CrossMediaManager.Current.Play("https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4");
         CrossMediaManager.Current.MediaItemFinished += (s, e) =>
         {
             customDialog.Cancel();
         };
        }
    
         protected override async void LoadContent()
            {
                _spriteBatch = new SpriteBatch(GraphicsDevice);
             //   await CrossMediaManager.Current.Play("https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4");
    
    ShowTheVideo();
            }
    }
    

    In the end, you can call game1.ShowTheVideo(); in the Load method of Options class.

    public class Options
    {
    private Game1 game1;
    GraphicsDevice graphicsDevice;
    
    public Options(Game1 game, GraphicsDevice device)
    {
        game1 = game;
        graphicsDevice = device;
    }
    
    public async void Load(ContentManager content)
    {
        game1.ShowTheVideo();
       // await CrossMediaManager.Current.Play("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4");
    }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.