Data transfer in Avalon

Data transfer in Avalon is all about sharing data between applications. Getting your data from one place to another is a very basic part of your computer interaction. The two most commonly used mechanism to move data from one app to another is through the copy and paste command, which puts and gets data from the system clipboard, and through drag and drop operations.

Don't be surprised if you find you already know the basics of this area. People familiar with the WinForms Clipboard class or OLE data transfer will be in familiar territory.

What it's all about

Everything here revolves around the IDataObject interface, which allows applications to set and get data in different formats. Avalon also implement a DataObject class that implements the storage / retrieval functionality for you, and should be adequate for most applications.

One last piece of the puzzle, and I'll leave you with a code sample for today. To identify what format you want your data in, you will normally use a type or a string with the format name. You can refer to the standard format names available from the DataFormats class.

Show me the code

Now, this small Avalon application will show you the plain text and the rich text data available on the clipboard when you click the button. It's based on the March CTP, on a standard Avalon application created through VS. Run this and try copying text from Notepad, WordPad and Visual Studio before clicking the button and see what happens!

 Window1.xaml
 <Window x:Class="DataSample1.Window1"
    xmlns="https://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="https://schemas.microsoft.com/winfx/xaml/2005"
    Text="Multiple format sample"
    >
  <DockPanel>
    <TextBlock DockPanel.Dock="Top">Copy text to the clipboard and then click the Paste button.</TextBlock>
    <Button DockPanel.Dock="Top" Click="PasteClick">Paste</Button>
    <TextBox ID="PlainTextBox" MinLines="2" MaxLines="4" DockPanel.Dock="Top"
             VerticalScrollBarVisibility="Visible"
             HorizontalScrollBarVisibility="Visible"
             Wrap="True" IsReadOnly="True" />
    <TextBox ID="RtfTextBox" MinLines="2"
             VerticalScrollBarVisibility="Visible"
             HorizontalScrollBarVisibility="Visible"
             Wrap="True" IsReadOnly="True" />
    </DockPanel>
</Window>
 Window1.xaml.cs
 using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
 namespace DataSample1
{
 public partial class Window1 : Window
 {
  public Window1(): base()
  {
   InitializeComponent();
  }
   private void PasteClick(object sender, RoutedEventArgs e)
  {
   IDataObject dataObject;
    dataObject = Clipboard.GetDataObject();
   if (dataObject == null)
   {
    this.PlainTextBox.Text =
     this.RtfTextBox.Text = "There is no clipboard data.";
   }
   else
   {
    if (dataObject.GetDataPresent(DataFormats.UnicodeText))
    {
     this.PlainTextBox.Text =
      "Contents in plain text format:\r\n" +
      (string)(dataObject.GetData(DataFormats.UnicodeText));
    }
    else
    {
     this.PlainTextBox.Text = "The clipboard data does not " +
      "support plain text.";
    }
    if (dataObject.GetDataPresent(DataFormats.Rtf))
    {
     this.RtfTextBox.Text =
      "Contents in RTF format:\r\n" +
      (string)(dataObject.GetData(DataFormats.Rtf));
    }
    else
    {
     this.RtfTextBox.Text = "The clipboard data does not " +
      "support RTF.";
    }
   }
  }
 }
}

This posting is provided "AS IS" with no warranties, and confers no rights.