Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Windows UWP Ads

$
0
0

@Paul_Crawley wrote:

I've been trying to get uwp ads working but am getting an exception when I call the show from outside of the game.xaml.cs file, heres the code

  public sealed partial class GamePage : Page
  {
    private readonly Game1 _game;

    private static InterstitialAd myInterstitialAd;
    private static string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
    private static string myAdUnitId = "test";
    public static bool adIsReady;
    public static bool adTrigger;
    public static AdType adType;

    public GamePage()
    {
      InitializeComponent();

      // Create the game.
      string launchArguments = string.Empty;
      _game = MonoGame.Framework.XamlGame<Game1>.Create(launchArguments,Window.Current.CoreWindow,swapChainPanel);


      myInterstitialAd = new InterstitialAd();

      adType = AdType.Display;                                                                      // carful using this phones can be twitchy if set to Display      

      myInterstitialAd.AdReady += MyInterstitialAd_AdReady;
      myInterstitialAd.ErrorOccurred += MyInterstitialAd_ErrorOccurred;
      myInterstitialAd.Completed += MyInterstitialAd_Completed;
      myInterstitialAd.Cancelled += MyInterstitialAd_Cancelled;
      myInterstitialAd.RequestAd(adType,myAppId,myAdUnitId);
      Game1.interstitialAd = myInterstitialAd;

      // Try making an instance and using that to do all this?

    }

    // ERROR EXCEPTION WHEN CALLING THIS FROM GAME1.CS 

    public static void ShowAd(InterstitialAd ad)
    {
      if(InterstitialAdState.Ready == ad.State)
      {
        ad.Show(); // EXCEPTION AT THIS CALL? HELP!
        adTrigger = false;
      }
    }
    private void MyInterstitialAd_AdReady(object sender,object e)
    {
      adIsReady = true;
      if(InterstitialAdState.Ready == myInterstitialAd.State)
      {
        //        myInterstitialAd.Show();                                                                  // show the ad 
        adTrigger = true;
//        ShowAd();
      }
    }
    private void MyInterstitialAd_ErrorOccurred(object sender,AdErrorEventArgs e)
    {
      adIsReady = false;
      throw new System.Exception("Failed to load ad:- GamePage.xaml.cs");
      // handle an error
    }
    private void MyInterstitialAd_Completed(object sender,object e)
    {
      adIsReady = false;
      // load the next ad
      myInterstitialAd.RequestAd(adType,myAppId,myAdUnitId);
    }
    private void MyInterstitialAd_Cancelled(object sender,object e)
    {
      adIsReady = false;
      // load the next ad
      myInterstitialAd.RequestAd(adType,myAppId,myAdUnitId);
    }
  }

Yet if I put the show() inside the AdReady call it works just fine, no bloody good if I want to show ads after a level, basically any ideas how as in admob I can preload the ad, then show it when I want to? ive read pretty much every example but no one actually does a real world example where a straight call in the middle of a running program an ad can be triggered.
Thanks in advance
Paul

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles