ArcGIS Runtime SDK for WPF - Library Reference
OpenReadCompleted Event
See Also  Example
ESRI.ArcGIS.Client Namespace > ArcGISWebClient Class : OpenReadCompleted Event

Occurs when an asynchronous operation to open a stream containing a resource completes.

Syntax

Visual Basic (Declaration) 
Public Event OpenReadCompleted As EventHandler(Of ArcGISWebClient.OpenReadCompletedEventArgs)

Remarks

Use the ArcGISWebClient.DownloadStringAsync Method / ArcGISWebClient.DownloadStringCompleted Event to process (read/write) text such as: JSON, XAML, XML, and HTML.

Use the ArcGISWebClient.OpenReadAsync Method / ArcGISWebClient.OpenReadCompleted Event to process (read/write) binary streams of data such as: images, files (.doc, .pdf, .exe), music, movies, etc.

Event Data

The event handler receives an argument of type ArcGISWebClient.OpenReadCompletedEventArgs containing data related to this event. The following ArcGISWebClient.OpenReadCompletedEventArgs properties provide information specific to this event.

PropertyDescription
Cancelled (Inherited from System.ComponentModel.AsyncCompletedEventArgs) 
Error (Inherited from System.ComponentModel.AsyncCompletedEventArgs) 
Result Gets a readable stream that contains data downloaded by a ArcGISWebClient.OpenReadAsync method.
UserState (Inherited from System.ComponentModel.AsyncCompletedEventArgs) 

Example

How to use:

Click the 'OpenReadAsync' Button to start an ArcGISWebClient operation. Check 'on' the CheckBox if you want to see the JSON results in a MessageBox, otherwise the result will display in the TextBox. The JSON result returned is a count of the number of features in a FeatureLayer. The Url for this example uses a token secured ArcGIS Server requiring the correct credientials in order to get information about the layer using the ArcGISWebClient operation. Use the following credentials when prompted by the ESRI.ArcGIS.Client.Toolkit.SignInDialog:
Username: user1
Passoword: pass.word1

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Using the ArcGISWebClient.OpenReadAsync Method to obtain JSON string information from ArcGIS Server.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
  
  <!-- Url for the ArcGISWebClient operation. This is a token secured ArcGIS Server requiring the correct credentials 
  in order to get information about the layer using the ArcGISWebClient operation. Use the following credentials when
  prompted by the ESRI.ArcGIS.Client.Toolkit.SignInDialog:
  Username: user1
  Passoword: pass.word1
  -->
  <TextBox Height="44" HorizontalAlignment="Left" Margin="4,181,0,0" Name="TextBox_Url" VerticalAlignment="Top" Width="575" 
  Text="http://serverapps10.esri.com/ArcGIS/rest/services/Oil/MapServer/0/query?where=1%3D1&amp;returnCountOnly=true&amp;f=json"
  TextWrapping="Wrap"/>
  
  <!-- Button to perform the work. -->
  <Button Content="OpenReadAsync" Height="24" Width="201" HorizontalAlignment="Left" Margin="4,232,0,0" 
          Name="Button_OpenReadAsync" VerticalAlignment="Top" Click="Button_OpenReadAsync_Click" />
  
  <!-- CheckBox option to display the ArcGISWebClient Response JSON in a MessageBox or TextBox.-->
  <CheckBox Content="Display Results in a MessageBox" Height="16" HorizontalAlignment="Left" Margin="211,235,0,0" 
            Name="CheckBox1" VerticalAlignment="Top" />
  
  <!-- Display the Response for the ArcGISWebClient operation. -->
  <TextBox Height="95" HorizontalAlignment="Left" Margin="4,262,0,0" Name="TextBox_Response" 
           VerticalAlignment="Top" Width="575" VerticalScrollBarVisibility="Auto" 
           HorizontalScrollBarVisibility="Auto" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="136" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="625" 
  TextWrapping="Wrap" Text="Click the 'OpenReadAsync' Button to start an ArcGISWebClient operation. Check 
  'on' the CheckBox if you want to see the JSON results in a MessageBox, otherwise the result will
  display in the TextBox. The JSON result returned is a count of the number of features in a FeatureLayer. 
  The Url for this example uses a token secured ArcGIS Server requiring the correct credientials in order 
  to get information about the layer using the ArcGISWebClient operation. Use the following credentials 
  when prompted by the ESRI.ArcGIS.Client.Toolkit.SignInDialog: 
  Username: user1
  Passoword: pass.word1" />
  
</Grid>
C#Copy Code
private void Button_OpenReadAsync_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Call the ESRI.ArcGIS.Client.Toolkit.SignInDialog which assists in getting Identity Manager credential
  // information for accessing a token secured service. For test ArcGIS Server web service in this example
  // use the following information:
  // Username: user1
  // Passoword: pass.word1
  ESRI.ArcGIS.Client.IdentityManager.Current.ChallengeMethod = ESRI.ArcGIS.Client.Toolkit.SignInDialog.DoSignIn;
  
  // Clear out any existing information in the TextBox.
  TextBox_Response.Text = "";
  
  // Create a new ArcGISWebClient object.
  ESRI.ArcGIS.Client.ArcGISWebClient myArcGISWebClient = new ESRI.ArcGIS.Client.ArcGISWebClient();
  
  // Wire up an Event Handler for the ArcGISWebClient.OpenReadCompleted Event.
  myArcGISWebClient.OpenReadCompleted += myArcGISWebClient_OpenReadCompleted;
  
  // Get the Url for the ArcGISWebClient request.
  string myUrl = TextBox_Url.Text;
  
  // Get the userState (aka. user token) to determine how to display the results of the ArcGISWebClient operation.
  bool myUserToken = (bool)CheckBox1.IsChecked;
  
  // Invoke the ArcGISWebClient.OpenReadAsync Method passing in the Url, null for the parameter collection, and a user token.
  myArcGISWebClient.OpenReadAsync(new Uri(myUrl), null, ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto ,myUserToken);
}
  
private void myArcGISWebClient_OpenReadCompleted(object sender, ESRI.ArcGIS.Client.ArcGISWebClient.OpenReadCompletedEventArgs e)
{
  // Get the userState (aka. user token) from the ArcGISWebClient operation.
  bool myUserToken = (bool)e.UserState;
  
  // Get the JSON string result from the ArcGISWebClient operation.
  System.IO.Stream theStream = e.Result;
  
  // Create a new StreamReader object using the Result of OpenReadCompletedEventArgs
  System.IO.StreamReader theStreamReader = new System.IO.StreamReader(theStream);
  
  // Even though the ArcGISWebClient.OpenReadAsync returned data as a Stream, in this case our return data is
  // really just a string.
  string theResult = theStreamReader.ReadToEnd();
  
  if (myUserToken == true)
  {
    // Display the JSON string response in a MessageBox.
    MessageBox.Show(theResult);
  }
  else
  {
    // Display the JSON string response in the TextBox.
    TextBox_Response.Text = theResult;
  }
}
VB.NETCopy Code
Private Sub Button_OpenReadAsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Call the ESRI.ArcGIS.Client.Toolkit.SignInDialog which assists in getting Identity Manager credential
  ' information for accessing a token secured service. For test ArcGIS Server web service in this example
  ' use the following information:
  ' Username: user1
  ' Passoword: pass.word1
  ESRI.ArcGIS.Client.IdentityManager.Current.ChallengeMethod = AddressOf ESRI.ArcGIS.Client.Toolkit.SignInDialog.DoSignIn
  
  ' Clear out any existing information in the TextBox.
  TextBox_Response.Text = ""
  
  ' Create a new ArcGISWebClient object.
  Dim myArcGISWebClient As ESRI.ArcGIS.Client.ArcGISWebClient = New ESRI.ArcGIS.Client.ArcGISWebClient
  
  ' Wire up an Event Handler for the ArcGISWebClient.OpenReadCompleted Event.
  AddHandler myArcGISWebClient.OpenReadCompleted, AddressOf myArcGISWebClient_OpenReadCompleted
  
  ' Get the Url for the ArcGISWebClient request.
  Dim myUrl As String = TextBox_Url.Text
  
  ' Get the userState (aka. user token) to determine how to display the results of the ArcGISWebClient operation.
  Dim myUserToken As Boolean = CheckBox1.IsChecked
  
  ' Invoke the ArcGISWebClient.OpenReadAsync Method passing in the Url, nothing for the parameter collection, and a user token.
  myArcGISWebClient.OpenReadAsync(New Uri(myUrl), Nothing, ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto, myUserToken)
  
End Sub
  
Private Sub myArcGISWebClient_OpenReadCompleted(sender As Object, e As ESRI.ArcGIS.Client.ArcGISWebClient.OpenReadCompletedEventArgs)
  
  ' Get the userState (aka. user token) from the ArcGISWebClient operation.
  Dim myUserToken As Boolean = e.UserState
  
  ' Get the JSON string result from the ArcGISWebClient operation.
  Dim theStream As System.IO.Stream = e.Result
  
  ' Create a new StreamReader object using the Result of OpenReadCompletedEventArgs
  Dim theStreamReader As System.IO.StreamReader = New System.IO.StreamReader(theStream)
  
  ' Even though the ArcGISWebClient.OpenReadAsync returned data as a Stream, in this case our return data is
  ' really just a string.
  Dim theResult As String = theStreamReader.ReadToEnd
  
  If myUserToken = True Then
    
    ' Display the JSON string response in a MessageBox.
    MessageBox.Show(theResult)
    
  Else
    
    ' Display the JSON string response in the TextBox.
    TextBox_Response.Text = theResult
    
  End If
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8

See Also

© ESRI, Inc. All Rights Reserved.