Utilisation du composant WindowsStore

De RAD Studio
Aller à : navigation, rechercher

Le composant TWindowsStore vous permet de connecter une application au Windows Store et d'utiliser des fonctionnalités telles que l'affichage de la liste d'apps appartenant à l'utilisateur, les add-ons disponibles, les adds-ons achetés et la gestion du mode évaluation.

Le composant TWindowsStore est uniquement pris en charge par les plates-formes Windows ; RAD Studio 10.3 Alexandria comprend une version VCL et une version FireMonkey. TWindowsStore est un wrapper de composant pour TWindowsStoreCore.

Vérification des apps et des add-ons appartenant à l'utilisateur

Pour récupérer la liste des applications Windows et des add-ons appartenant à l'utilisateur, vous devez insérer le composant TWindowsStore dans la fiche ou le créer à l'exécution, ajouter un TButton et inclure le code suivant :

procedure TStoreForm.lbInfoClick(Sender: TObject);
var
  LMsg: string;
const
  //These consts mirrors the name of the Add-Ons ad defined
  //in the MS Windows Store. Using this approach you can “Ask” to the
  //store what the current user has bought.
  //The current user is indentified automatically because the
  //app has been downloaded from the store.
  HENRYFORDQUOTES = 'henryfordquotes';
  SAVETOFILE = 'savetofile';
  SAVEASFAVOURITE = 'saveasfavourite';
begin
  LMsg := '**You current situation**' + sLineBreak;
  LMsg := LMsg + 'You ' + IfThen(WindowsStore1.UserHasBought(HENRYFORDQUOTES),
    'have bought', 'don''t have bought') +
    ' the great Henry Ford aphorisms archive.' + sLineBreak;
  LMsg := LMsg + 'You ' + IfThen(WindowsStore1.UserHasBought(SAVETOFILE),
    'have bought', 'don''t have bought') +
    ' the ability to save aphorisms to file.' + sLineBreak;
  LMsg := LMsg + 'You ' + IfThen(WindowsStore1.UserHasBought(SAVEASFAVOURITE),
    'have bought', 'don''t have bought') +
    ' the ability to save aphorisms as favourite.' + sLineBreak;
  ShowMessage(LMsg);
end;

Accès aux add-ons disponibles

Le composant vous permet également d'inspecter tous les add-ons disponibles en accédant à la propriété indexée AppProducts (sgAvailableProducts est un TStringGrid), comme dans le code affiché ci-dessous :

procedure TStoreForm.UpdateAvailableProducts;
var
  LProdsCount: Integer;
  I: Integer;
  LRow: TStrings;
begin
  LProdsCount := WindowsStore1.AppProducts.Count;
  for I := 0 to LProdsCount - 1 do
  begin
    LRow := sgAvailableProducts.Rows[I + 1];
    LRow.Add(WindowsStore1.AppProducts[I].StoreId.ToString);
    LRow.Add(WindowsStore1.AppProducts[I].Title.ToString);
    LRow.Add(WindowsStore1.AppProducts[I].Price.FormattedBasePrice.ToString);
    LRow.Add(WindowsStore1.AppProducts[I].ProductKind.ToString);
    LRow.Add(
      WindowsStore1.AppProducts[I].Price.IsOnSale.ToString(TUseBoolStrs.True)
    );    
    LRow.Add(
      WindowsStore1.AppProducts[I].IsInUserCollection
       .ToString(TUseBoolStrs.True)
    );
  end;
End;

Vérification des add-ons appartenant à l'utilisateur

Pour récupérer la liste des add-ons appartenant à l'utilisateur, utilisez la propriété indexée UserCollection qui fonctionne comme AppProducts.

Gestion du mode évaluation

Utilisez la propriété AppLicense pour gérer le TrialMode de votre app comme indiqué ci-dessous :

procedure TStoreForm.lbTrialInformationClick(Sender: TObject);
var
  LRemainingTrialDays: Integer;
begin
  if WindowsStore1.AppLicense.IsActive then
  begin
    if WindowsStore1.AppLicense.IsTrial then
    begin
      LRemainingTrialDays := WindowsStore1.AppLicense.TrialTimeRemaining.Days;
      ShowMessage(
        Format('You can use this app for %d more days before the trial period ends.',
        [LRemainingTrialDays]));
    end
    else
    begin
      ShowMessage('You have a full license. The trial time is not meaningful.');
    end;

  end
  else
  begin
    ShowMessage('You don''t have a license. The trial time can''t be determined.');
  end;
End;

Modèle:Remarque https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/Store/cs/Scenario1_TrialMode.xaml.cs#L82.}}]