Condividi tramite


Trasferimento di un oggetto Properties-Only nel dispositivo

Mentre l'esempio nell'argomento precedente ha illustrato la creazione di contenuto in un dispositivo costituito da proprietà e dati, questo argomento è incentrato sulla creazione di un oggetto solo proprietà.

I trasferimenti di proprietà vengono eseguiti usando le interfacce descritte nella tabella seguente.

Interfaccia Descrizione
Interfaccia IPortableDeviceContent Fornisce l'accesso ai metodi specifici del contenuto.
Interfaccia IPortableDeviceValues Usato per recuperare le proprietà che descrivono il contenuto.

 

La TransferContactToDevice funzione nel modulo ContentTransfer.cpp dell'applicazione di esempio illustra come un'applicazione potrebbe trasferire le informazioni di contatto da un PC a un dispositivo connesso. In questo particolare esempio, il nome di contatto trasferito è un hard coded "John Kane" e il numero di telefono di contatto trasferito è sempre "425-555-0123".

La prima attività eseguita dalla TransferContactToDevice funzione consiste nel richiedere all'utente di immettere un identificatore di oggetto per l'oggetto padre nel dispositivo (in cui il contenuto verrà trasferito).

HRESULT                             hr = S_OK;
WCHAR                               szSelection[81]        = {0};
CComPtr<IPortableDeviceValues>      pFinalObjectProperties;
CComPtr<IPortableDeviceContent>     pContent;

// Prompt user to enter an object identifier for the parent object on the device to transfer.
printf("Enter the identifer of the parent object which the contact will be transferred under.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting content transfer\n");
}

Il passaggio successivo è il recupero delle proprietà del contatto, che verrà usato quando l'esempio scrive il nuovo contatto nel dispositivo. Il recupero delle proprietà del contatto viene eseguito dalla GetRequiredPropertiesForPropertiesOnlyContact funzione helper.

// 2) Get the properties that describe the object being created on the device

if (SUCCEEDED(hr))
{
    hr = GetRequiredPropertiesForPropertiesOnlyContact(szSelection,              // Parent to transfer the data under
                                                       &pFinalObjectProperties);  // Returned properties describing the data
    if (FAILED(hr))
    {
        printf("! Failed to get required properties needed to transfer an image file to the device, hr = 0x%lx\n", hr);
    }
}

Questa funzione scrive i dati seguenti in un oggetto IPortableDeviceValues .

  • Identificatore dell'oggetto per l'elemento padre del contatto nel dispositivo. Questa è la destinazione in cui il dispositivo archivierà l'oggetto.
  • Tipo di oggetto (contatto).
  • Nome contatto (stringa codificata hard di "John Kane").
  • Numero di telefono del contatto (stringa hard-coded di "425-555-0123").

Il passaggio finale crea l'oggetto solo proprietà nel dispositivo (usando le proprietà impostate dalla GetRequiredPropertiesForPropertiesOnlyContact funzione helper). Questa operazione viene eseguita chiamando il metodo IPortableDeviceContent::CreateObjectWithPropertiesOnly .

if (SUCCEEDED(hr))
{
    PWSTR pszNewlyCreatedObject = NULL;
    hr = pContent->CreateObjectWithPropertiesOnly(pFinalObjectProperties,    // Properties describing the object data
                                                  &pszNewlyCreatedObject);
    if (SUCCEEDED(hr))
    {
        printf("The contact was transferred to the device.\nThe newly created object's ID is '%ws'\n",pszNewlyCreatedObject);
    }

    if (FAILED(hr))
    {
        printf("! Failed to transfer contact object to the device, hr = 0x%lx\n",hr);
    }

    // Free the object identifier string returned from CreateObjectWithPropertiesOnly
    CoTaskMemFree(pszNewlyCreatedObject);
    pszNewlyCreatedObject = NULL;
}

Interfaccia IPortableDevice

Interfaccia IPortableDeviceContent

Interfaccia IPortableDeviceValues

Guida alla programmazione