当用户点击魅力设置中的共享时,如何使文本框内容在Windows 8 xaml应用程序中共享?

时间:2012-11-29 15:29:24

标签: windows-8 windows-runtime winrt-xaml winrt-async

我的Windows8商店应用(xaml)应用程序中有一个文本文本框,当用户从Charm设置中选择“共享”选项时,我想让它可共享?这可以在Windows 8中存储xaml app吗?

1 个答案:

答案 0 :(得分:0)

是的,您需要回复" DataRequested"事件并将文本框的内容添加到数据包中。这是一个代码示例:

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);
}