Search code examples
windows-8windows-runtimewinrt-xamlwinrt-async

how to make textbox content shareable in windows 8 xaml app when user clicks share from charm settings?


I have one text textbox in my Windows8 Store app (xaml) application and I want to make it shareable when user selects Share option from Charm settings ? Is that possible to in windows 8 store xaml app ?


Solution

  • Yes, you need to respond to the "DataRequested" event and add the content of the textbox to the data package. Here is a code sample:

    JavaScript:

    // Call this during initialization
    function registerForShare() {
        var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView();
        dataTransferManager.addEventListener("datarequested", shareTextHandler);
    }
    
    function shareTextHandler(e) {
        var request = e.request;
        request.data.properties.title = "Your title";
        request.data.properties.description = "Description of what you're sharing";
        request.data.setText(yourTextBox.Text);
    }
    

    C#:

    // Call this during initialization
    private void ShareSourceLoad()
    {
        var dataTransferManager = DataTransferManager.GetForCurrentView();
        dataTransferManager.DataRequested += DataRequested;
    }
    
    private void DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
    {
        DataRequest request = e.Request;
        request.Data.Properties.Title = "Your title";
        request.Data.Properties.Description = "Description of what you're sharing";
        request.Data.SetText(yourTextBox.Text);
    }