Aller au contenu principal

Banner Ad

Integration

Step 1. Register the handler

A custom control and its handler must be registered with an app, before it can be consumed. This should occur in the CreateMauiApp method in the MauiProgram class in your app project, which is the cross-platform entry point for the app:

using BlueStack.Controls.BannerAdView;
using Microsoft.Extensions.Logging;

namespace BlueStack.SDK.Maui.DemoApp;

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(BannerAdView), typeof(BannerAdViewHandler));
});
return builder.Build();
}
}

Step 2. Instantiate Banner Ad

You can instanstiate a Banner ad right after the SDK finish BlueStack ad network initialization. You can instantiate a BannerAdView either through MAUI XAML or programmatically.

  • Using MAUI XAML

BannerSinglePage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BlueStack.SDK.Maui.DemoApp.banner.BannerSinglePage"
xmlns:bannerAdView="clr-namespace:BlueStack.Controls.BannerAdView;assembly=BlueStack.SDK.Maui">

<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center"
x:Name="ContainerLayout">
...
<bannerAdView:BannerAdView
x:Name="BannerView"
WidthRequest="320"
HeightRequest="50"
PlacementId="my_placement"
/>
...
</VerticalStackLayout>
</ContentPage>

  • Programitically

BannerSinglePage.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BlueStack.SDK.Maui.DemoApp.banner.BannerSinglePage">
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center"
x:Name="ContainerLayout">
...
</VerticalStackLayout>
</ContentPage>

BannerSinglePage.xaml.cs

using BlueStack.API;
using BlueStack.Controls.BannerAdView;

namespace BlueStack.SDK.Maui.DemoApp.banner;

public partial class BannerSinglePage : ContentPage
{
...
private BannerAdView _bannerAdView;

public BannerSinglePage()
{
InitializeComponent();
InitializeNewBanner();
...
}

private void InitializeNewBanner()
{
_bannerAdView = new BannerAdView
{
PlacementId = "your_ad_placement"
};
_bannerAdView.Handler = new BannerAdViewHandler();
ContainerLayout.Add(_bannerAdView);
}
...
}

Step 3. Register event listeners

To receive banner ad events, implement the IMauiBannerAdListener interface and add it during the initialization of BannerAdView using SetBannerAdListener.

using BlueStack.API;
using BlueStack.Controls.BannerAdView;

namespace BlueStack.SDK.Maui.DemoApp.banner;

public partial class BannerSinglePage : ContentPage, IMauiBannerAdListener
{
private BannerAdView _bannerAdView;

public BannerSinglePage()
{
InitializeComponent();
InitializeNewBanner();
}

private void InitializeNewBanner()
{
...
_bannerAdView.SetBannerAdListener(this);
}

public void OnBannerDidLoad()
{
Logger.Debug(TAG, "OnBannerDidLoad");
}

public void OnBannerDidFailed(BlueStackError blueStackError)
{
Logger.Debug(TAG, "OnBannerDidFailed: " + blueStackError.Message);
}

public void OnAdClicked()
{
Logger.Debug(TAG, "OnAdClicked");
}

public void OnBannerDidRefresh()
{
Logger.Debug(TAG, "OnBannerDidRefresh");
}

public void OnBannerDidFailToRefresh(BlueStackError blueStackError)
{
Logger.Debug(TAG, "OnBannerDidFailToRefresh: " + blueStackError.Message);
}
}

Step 4. Load Banner ad

BannerAd takes AdSize to load banner. It also have another Load method that takes Preference instance along with AdSize. Banner Ad supports following ad sizes:

Ad SizeDefinition
Banner320x50
DynamicBanner0x50
LargeBanner320x100
FullBanner468x60
Leaderboard728x90
DynamicLeaderboard0x90
MediumRectangle300x250
  • Without Preference
public class BannerSinglePage : ContentPage, IMauiBannerAdListener
{
private BannerAdView _bannerAdView;
private AdSize _adSize = AdSize.Banner;
...
private void LoadAd()
{
_bannerAdView.Load(_adSize);
}
...
}
  • With Preference
public class BannerSinglePage : ContentPage, IMauiBannerAdListener
{
private BannerAdView _bannerAdView;
private AdSize _adSize = AdSize.Banner;
...
private void LoadAd()
{
Preference bsPreference = new Preference();
Location myLocation = new Location(Location.NONE_PROVIDER)
{
Latitude = 35.757866,
Longitude = 10.810547
};

bsPreference.SetAge(25);
bsPreference.SetLanguage("en");
bsPreference.SetGender(Gender.Male);
bsPreference.SetKeyword("brand=myBrand;category=sport");
bsPreference.SetLocation(myLocation, 3);
bsPreference.SetContentUrl("https://madvertise.com/en/");

_bannerAdView.Load(_adSize, BlueStackAdPreferenceFactory.create());
}
...
}

Force refresh banner

Force refresh will refresh the banner ad immediately. Call this method if you need a force load of banner ad without destroying the existing one.

_bannerAd.ForceRefresh();

Note : The setLocation method takes the following parameters:

  • the Location instance.
  • the CONSENT_FLAG value (corresponds to a int : 0,1,2 or 3).
    • 0 = Not allow to send location.
    • 1 = When you managed location according to consent value.
    • 2 and 3 = Allow the SDK to managed location directly in accordance with the consent value use TCF v1 or TCF v2, see with the madvertise team it depends on your implementation.

Destroy banner ad

To properly manage resources, make sure to destroy the banner ad when it is no longer in use.

_bannerAd.Destroy();