Create mobile apps with HTML5, JavaScript and Visual Studio

(Elle) #1

msdnmagazine.com February 2014 33


feature can be found in the “Debugging Asynchronous Code in Visual


Studio 2013—Call Stack enhancements” blog post at bit.ly/19NTNez.


Th e Tasks window in Visual Studio 2013 is designed to help you


understand the state of async tasks in your apps by displaying all


the currently running and scheduled tasks. It’s a replacement for the


Parallel Tasks window that was available in previous Visual Studio


versions. Figure 4 shows a snapshot of a Visual Studio 2013 Tasks


window for the sample code given in Figure 1.


x64 Edit and Continue This was a popular debugger feature


request, with more than 2,600 votes on the Visual Studio UserVoice


site where users can request new features (bit.ly/14YIM8X). Developers


have loved using the Edit and Continue feature since it was intro-


duced with Visual Studio 2005 and the .NET Framework 2.0


release, for x86 projects. Edit and Continue makes it easier to write


the correct code by letting you change the source code during a


debugging session, while app state is available. You can even move


the instruction pointer so you can replay code aft er making a change.


It provides a more productive development experience because you


don’t have to stop and restart the session to validate your changes.


x64 support for Edit and Continue is now enabled with Visual


Studio 2013 and the .NET Framework 4.5.1


release. You can use this feature for debugging


desktop applications (Windows Presentation


Foundation, Windows Forms and so on),


Windows Store apps, ASP.NET Web applications


and Windows Azure Cloud Services projects


targeting x64, AnyCPU or x86 architectures.


Managed Return Value Inspection


Debugger support for managed return values is


another popular request with more than 1,000


votes on the UserVoice site. Th e Visual C++


debugger has an existing feature that allows you


to observe the return values of methods, and


we wanted the same capability for .NET as well.


Th is feature is useful for many code patterns.


However, you can really see its value with nested


methods, as demonstrated in Figure 5. Wit h


this feature, you no longer have to worry about storing the results


of your methods in locals solely to make debugging easier. When


you step over a method call, both direct return values and the


return values of the embedded methods will be displayed in the Autos


window along with the parameter values passed to the functions.


You can also use the Immediate window to access the last return


value through the use of the new $ReturnValue pseudo-variable.


Windows Store Development Enhancements We responded


to feedback and provided .NET support for new Windows


Runtime (WinRT) features to improve the .NET Windows Store


app development experience.


One of the pain points was converting a .NET Stream to a WinRT


IRandomAccessStream. In the .NET Framework 4.5.1, we added a new


extension method, AsRandomAccessStream, for System.IO.Stream


to solve this problem. You can now write the following code, which


allows you to easily provide an IRandomAccessStream:


// EXAMPLE: Get image from URL via networking I/O
var client = new HttpClient();
Stream stream = await client.GetStreamAsync(imageUrl);
var memStream = new MemoryStream();
await stream.CopyToAsync(memStream);
memStream.Position = 0;
var bitmap = new BitmapImage();
bitmap.SetSource(memStream.AsRandomAccessStream());
image.Source = bitmap;

Th is example code reads an image from the Web and displays it


in a XAML Image control (represented by the “image” variable).


Another improvement is error propagation in the Windows


Runtime. Th e Windows Runtime, in Windows 8.1, enables exceptions


to pass between WinRT components. With this support, an exception


can be thrown from a C++ WinRT component and be caught in C#


(or vice versa). Additional information for the exception is now avail-


able via the Message and StackTrace properties on System.Exception.


Th e Windows Runtime also added support for nullable value


types in structures. You can build managed WinRT components


that expose structs with this new feature, such as in this sample code:


public struct PatientRecord
{
public string Name;
public int Age;
public string HomeAddress;
// InsuranceID is nullable
public int? InsuranceId;
}

private async void ShowSampleImg_Click(object sender, RoutedEventArgs e)
{
string imgUri = "http://example.com/sample.jpg";
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = await GetSampleImgMemStream(imgUri);
bitmap.EndInit();
sampleImg.Source = bitmap;
}
private async Task<MemoryStream> GetSampleImgMemStream(string srcUri)
{
Stream stream = await GetSampleImage(srcUri);
var memStream = new MemoryStream();
await stream.CopyToAsync(memStream);
memStream.Position = 0;
return memStream;
}
private async Task<Stream> GetSampleImage(string srcUri)
{
HttpClient client = new HttpClient();
Stream stream = await client.GetStreamAsync(srcUri);
return stream;
}

Figure 1 Asynchronous Code Sample


Figure 3 Visual Studio 2013 Call Stack Window


Figure 2 Visual Studio 2012 Call Stack Window

Free download pdf