Clipboard contents of a closed Windows Universal application

I am writing a C # Windows Universal App where a user can copy a file to the clipboard. But if the application is closed, the contents of the clipboard are lost. Usability is terrible if the user can easily lose the contents of the clipboard. Is there a way to make the contents of the application clipboard persistent, as in any other classic Windows application?

Code example:

public static void CopyFileToClipboard(StorageFile file) { DataPackage dp = new DataPackage(); dp.RequestedOperation = DataPackageOperation.Copy; dp.SetStorageItems(new List<StorageFile>() { file }); Clipboard.SetContent(dp); // not available after app closed Clipboard.Flush(); } public static void CopyTextToClipboard(string text) { DataPackage dp = new DataPackage(); dp.RequestedOperation = DataPackageOperation.Copy; dp.SetText(text); // available after app closed Clipboard.SetContent(dp); Clipboard.Flush(); } //I have tried to copy the file to the app folder first but it has nothing changed. public async static void CacheAndCopyFileToClipboard(StorageFile file) { DataPackage dp = new DataPackage(); dp.RequestedOperation = DataPackageOperation.Copy; var xfile = await ApplicationData.Current.LocalFolder.CreateFileAsync(file.Name); await file.CopyAndReplaceAsync(xfile); dp.SetStorageItems(new List<StorageFile>() { xfile }); Clipboard.SetContent(dp); // not available after app closed Clipboard.Flush(); } 

So the question is, how can I put a file on the clipboard so that users can paste it even if the application is closed?

Edit: It seems that this is a problem for all Windows Universal Apps, for example, if you copy a picture in the Windows Photo application, it can only be inserted when the Photos application is running. I cannot imagine that this strange behavior should be the standard behavior of applications. This is more like a mistake, because I see no reason for this strange behavior.

Edit2: A new example of the problem (thanks to Joe300 for the feedback). It works with strings, but not with StorageFile (even if it is first copied to the local application folder). What is the reason that the Flush () command does not work with files? Is there something special to consider when files are used (permissions, ...)?

+7
c # windows-10 windows-runtime uwp win-universal-app
source share
1 answer

You will need to add a Flush call so that the content is available after the application is closed.

Adds content to the clipboard and frees the DataPackage object from the source application. This method allows you to keep content available after closing the application.

You should also set RequestedOperation :

 dp.RequestedOperation = DataPackageOperation.Copy; try { Windows.ApplicationModel.DataTransfer.Clipboard.SetContent(dp); Clipboard.Flush(); } catch (Exception ex) { // Copying data to Clipboard can potentially fail - for example, if another application is holding Clipboard open } 

Other than this, there are actually no other options for managing Clipboard .

+2
source share

All Articles