- 1 year ago
How to make clipboard sync between Windows 10 Mobile & PC on UWP C# app?
To enable clipboard synchronization between Windows 10 Mobile and PC in a Universal Windows Platform (UWP) C# app, you can use the DataPackageView
and DataPackage
classes provided by the Windows Runtime API. Here's a step-by-step guide:
Add the necessary capabilities: Open your UWP project's manifest file (
Package.appxmanifest
) and make sure theclipboardHistory
andclipboard
capabilities are enabled. This allows your app to access the clipboard functionality. Go to the "Capabilities" tab in Visual Studio and check the appropriate checkboxes.Implement clipboard read functionality: To read the clipboard content, you can use the
DataPackageView
class. Here's an example of how you can retrieve the text from the clipboard:csharpusing Windows.ApplicationModel.DataTransfer;
// ...
DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Text))
{
string text = await dataPackageView.GetTextAsync();
// Use the text from the clipboard
}
This code retrieves the content from the clipboard using
Clipboard.GetContent()
. It checks if the content contains text (StandardDataFormats.Text
) and then retrieves the text asynchronously usingGetTextAsync()
.Implement clipboard write functionality: To write content to the clipboard, you can use the
DataPackage
class. Here's an example of how you can set the text in the clipboard:csharpusing Windows.ApplicationModel.DataTransfer;
// ...
DataPackage dataPackage = new DataPackage();
dataPackage.SetText("Text to be copied to the clipboard");
Clipboard.SetContent(dataPackage);
This code creates a new
DataPackage
and sets the desired text usingSetText()
. Then it sets the content of the clipboard usingClipboard.SetContent()
.Test the clipboard synchronization: Deploy and run your UWP app on both Windows 10 Mobile and a PC. Ensure that both devices are signed in with the same Microsoft account. Copying text on one device should update the clipboard content, and the other device should reflect the change when you read the clipboard.
By implementing these steps, you can enable clipboard synchronization between Windows 10 Mobile and PC in your UWP C# app. Keep in mind that clipboard synchronization depends on the user's Microsoft account and the devices being signed in with the same account.