Click here to Skip to main content
Licence CPOL
First Posted 29 Jan 2012
Views 6,051
Bookmarked 1 time

A simple Silverlight object viewer

By | 29 Jan 2012 | Technical Blog
A simple Silverlight object viewer
A Technical Blog article. View original blog here.[^]

Sometimes when you're making a Windows phone 7 rest client you just want to look at some of the returned data to see what's there. You can wire up some UI or use tracing. Maybe some breakpoints. But it's also nice to be able to drop something into the UI that you can access easily and as needed at runtime. The WinForms object browser for instance is handy for debugging things at runtime, within the app without a debugger or a designed UI. This little widget isn't as fully functional as that but can be helpful in a similar way. Drop it somewhere in the UI and set its DataContext property and all public readable properties will be listed with name and value.

First some XAML:

<UserControl x:Class="GoogleAuthDemo.ObjectBrowser"<br />    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:GoogleAuthDemo"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">
    
    <UserControl.Resources>
        <local:ObjectPropertiesConverter x:Key="ObjectPropertiesConvert"/>
        
        <DataTemplate x:Key="PropertyTemplate">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Key}" Margin="0,0,10,1"/>
                <TextBlock Text="{Binding Value}" TextWrapping="Wrap" 
        TextAlignment="Right" HorizontalAlignment="Right" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    
    <ScrollViewer>
        <ItemsControl ItemTemplate="{StaticResource PropertyTemplate}"
                      ItemsSource="{Binding Path=., 
            Converter={StaticResource ObjectPropertiesConvert}}">                
        </ItemsControl>
    </ScrollViewer>

</UserControl>

With a smidge of C# to create a name value pair collection, given an object's properties:

public class ObjectPropertiesConverter : IValueConverter<br />    {
    public object Convert(object value, Type targetType, 
            object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;
        return from p in value.GetType().GetProperties()
            where p.CanRead
               select new KeyValuePair<string, string>
               (
                  p.Name,
                  p.GetValue(value, null) != null ? 
        p.GetValue(value, null).ToString() : null
               );                   
    }
    public object ConvertBack(object value, Type targetType, 
            object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Don Kackman

Team Leader
Starkey Laboratories
United States United States

Member

The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10
It only went downhill from there.
 
Hey look, I've got a blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 29 Jan 2012
Article Copyright 2012 by Don Kackman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid