martes, agosto 19, 2008

Cool Site with great controls, components and skins - www.wpfstyles.com

Logo If you haven't you gotta check www.wpfstyles.com by Andrew den Hertog. The just released site oriented to the dev community, is quickly becoming a great resources for controls, components and skins sharing for WPF and Silverlight. Go give it a try and if you have something, share it and if you need something, probably you will find it here. If you don't check it back later, the site is new and people is starting to publish content, so don't freak out if you don't see 1,000 controls yet.

Personal Note/Public Apology: There has been a while without a post, because I have been traveling a lot and didn't have Internet connection in several of those places. I just moved to another country and I'm still getting installed, but you can expect frequent posts again.

martes, agosto 05, 2008

Using the ASP.NET Providers from Silverlight - Membership, Roles and Profile

Brad Abrams wrote an excellent post about how to set everything in the server and client to consume this services. He covers for areas:

1. Login\Logout
2. Save personalization settings
3. Enable custom UI based on a user's role (for example, manager or employee)
4. A custom log-in control to make the UI a bit cleaner

These Services are simple wrappers to the actual providers and only provider the most common functions (i.e. Membership has login/logout, but not CreateUser), but would be pretty straightforward to extend them. If your application is not 100% SL and you have parts already build in ASP.NET you can easily combine them, for example, use the Login controls to Authenticate users, but use the Role Services to restrict certain functions depending on who is who.

sábado, agosto 02, 2008

Value Converters - More Simple Samples

This is the third of several posts related to Value Converters. Reading the posts in order can be useful, but not necessary. The first post, Databinding 101, served as an intro to the problem and the second, DefaultValueConverter, introduced the basics about ValueConverters. Now we're ready to present several examples to give you more ideas of where ValueConverters can be used.

 

ImageSourceConverter

The Image control doesn't update its Source property automatically, so setting it to a path will work only the first time, but if the path in your datasource changes it won't be reflected, well the ImageSourceConverter will help you. This is really a bug that will be corrected in SL RTM, but meanwhile this is another place to use ValueConverters.

public class ImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string path = (string)value;
return new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}


VisibilityConverter



You might want to change the visibility of a control based on a boolean property of your objects, this is simple with the VisibiltyConverter:



public class VisibilityConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
return Visibility.Visible;
return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}


StringFormatConverter



This is one of my favorites. ASP.NET and Winforms use a StringFormat that let you display properly numeric or date objects (use these cheat sheet as reference). With this converter for example you can simply pass the ConverterParameter of C to format a numeric value as currency and will work similar to the way it does in the mentioned platforms. This is another convertar that I feel should be part of the Binding object.



public class StringFormatConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string formatString = parameter.ToString();
return String.Format("{0:" + formatString + "}", value);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new InvalidOperationException("This Convert supports only OneWay binding");
}
}


StringFixedLengthConverter



For some controls you might want to specify a fixedlength for the text. You can do it with the StringFixedLengthConverter setting its ConverterParameter. This is rarely useful...



public class StringFixedLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return GetFixedLengthText((string)value, int.Parse((string)parameter));
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private string GetFixedLengthText(string originalString, int maxChars)
{
int textLength = originalString.Length;
if (textLength > maxChars)
return originalString.Substring(0, maxChars - 3) + "...";
int padding = (int)Math.Round((maxChars - textLength) / 2d);
string paddingChars = "";
for (int i = 0; i < padding; i++)
paddingChars += " ";
return paddingChars + originalString + paddingChars;
}
}


IfNullChangeColorConverter



This is not common, but we had run into cases where we need to change the color of a control in case a property is null. This could be combined with the DefaultValueConverter presented yesterday. Imagine you will display a DefaultValue of 'Not Available' if the person doesn't have a phone number, but you also want to greyout the text to emphasize the lack of data. To create this effect, specify the desired color in the ConverterParameter (if no value is specify grey will be used).



public class IfNullChangeColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return new SolidColorBrush(GetColor(parameter));
return new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
}

public Color GetColor(object parameter)
{
byte[] argbColor = new byte[4];
if (parameter != null)
{
string[] argbColorStrings = parameter.ToString().Split(',');
for (int i = 0; i < 4; i++)
{
argbColor[i] = byte.Parse(argbColorStrings[i]);
}
}
else
{
//#FFC0C0C0
argbColor[0] = 255;
argbColor[1] = 192;
argbColor[2] = 192;
argbColor[3] = 192;
}
return Color.FromArgb(argbColor[0], argbColor[1], argbColor[2], argbColor[3]);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}


Creating ValueConverters is really easy and these examples are meant to show that, rather than to try to cover every scenario. However, ValueConverters have many drawbacks. You can't combine them (e.g. use StringFormat and DefaultValue for the same property because the binding object only allows one) and won't scale easily (you will end up with lots of classes for super simple scenarios). Tomorrow I will talk about a pattern that can overcome these drawbacks.



You can download the attached code here with the last examples and the classes presented here.


I format my code using Manoli's website


Don't forget to vote for your favorite topics for this column.

viernes, agosto 01, 2008

Professional Visual Studio 2008 for Sale Now.

As I mentioned before I was invited to write the chapters of Team System in the book Professional Visual Studio 2008, well the book is finally out now and you can get it in Amazon, Barnes & Noble or your favorite bookstore. This is the Table of Contents:

Table of Contents

Part I: Integrated Development Environment

Chapter 1: A Quick Tour
Chapter 2: The Solution Explorer, Toolbox, and Properties
Chapter 3: Options and Customizations
Chapter 4: Workspace Control
Chapter 5: Find and Replace, and Help

Part II: Getting Started

Chapter 6: Solutions, Projects, and Items
Chapter 7: Source Control
Chapter 8: Forms and Controls
Chapter 9: Documentation Using Comments and Sandcastle
Chapter 10: Project and Item Templates

Part III: Languages

Chapter 11: Generics, Nullable Types, Partial Types, and Methods
Chapter 12: Anonymous Types, Extension Methods, and Lambda Expressions
Chapter 13: Language-Specific Features
Chapter 14: The My Namespace
Chapter 15: The Languages Ecosystem

Part IV: Coding

Chapter 16: IntelliSense and Bookmarks
Chapter 17: Code Snippets and Refactoring
Chapter 18: Modeling with the Class Designer
Chapter 19: Server Explorer
Chapter 20: Unit Testing

Part V: Data

Chapter 21: DataSets and Data Binding
Chapter 22: Visual Database Tools
Chapter 23: Language Integrated Queries (LINQ)
Chapter 24: LINQ to XML
Chapter 25: LINQ to SQL and Entities
Chapter 26: Synchronization Services

Part VI: Security

Chapter 27: Security in the .NET Framework
Chapter 28: Cryptography
Chapter 29: Obfuscation
Chapter 30: Client Application Services
Chapter 31: Device Security Manager

Part VII: Platforms

Chapter 32: ASP.NET Web Applications
Chapter 33: Office Applications
Chapter 34: Mobile Applications
Chapter 35: WPF Applications
Chapter 36: WCF and WF Applications
Chapter 37: Next Generation Web: Silverlight and ASP.NET MVC

Part VIII: Configuration and Internationalization

Chapter 38: Configuration Files
Chapter 39: Connection Strings
Chapter 40: Resource Files

Part IX: Debugging

Chapter 41: Using the Debugging Windows
Chapter 42: Debugging with Breakpoints
Chapter 43: Creating Debug Proxies and Visualizers
Chapter 44: Debugging Web Applications
Chapter 45: Advanced Debugging Techniques

Part X: Build and Deployment

Chapter 46: Upgrading with Visual Studio 2008
Chapter 47: Build Customization
Chapter 48: Assembly Versioning and Signing
Chapter 49: ClickOnce and MSI Deployment
Chapter 50: Web and Mobile Application Deployment

Part XI: Automation

Chapter 51: The Automation Model
Chapter 52: Add-Ins
Chapter 53: Macros

Part XII: Visual Studio Team System

Chapter 54: VSTS: Architect Edition
Chapter 55: VSTS: Developer Edition
Chapter 56: VSTS: Tester Edition
Chapter 57: VSTS: Database Edition
Chapter 58: Team Foundation Server

I also had a chance to review a couple of chapters. Writing a book was a great experience and I hope you will enjoy the results.

As Dave Gardner mentioned there are books available for instructors from the publisher and other's through the books's page. Nick Randolph mentioned later than they will have books to give away in TechEd Australia and TechEd New Zealand.

Value Converters - Default Value Converter

This is the second of several posts related to Value Converters. Reading the posts in order can be useful, but not necessary. As we saw yesterday sometimes we need to specify a DefaultValue for cases where there's no value specified by our datasource. This is a look of the application with data, then without a phone number and the third pic shows how would it look with the DefaultValueConverter.

With data:

Without data:
image
Wit DefaultValueConverter:
image
The change in XAML is really simple, this is the original line:
<TextBlock Text='{Binding Mode=OneWay, Path=PhoneNumber}'/>
 
This is the modified line:

<TextBlock Text='{Binding Mode=OneWay, Path=PhoneNumber, ConverterParameter="[Not Available]", Converter={StaticResource DefaultValueConverter}}'/>

We also need to add a Resource that holds the reference to our DefaultValueConverter

<UserControl.Resources>
<SilverlightApplication14:DefaultValueConverter x:Key="DefaultValueConverter"/>
</UserControl.Resources>

We added two attributes to the Binding object, a ConverterParameter and a resource that will hold an instance of our ValueConverter. A ValueConverter is simply a class that implements IValueConverter and consists of two methods Convert and ConvertBack. ConvertBack is used with TwoWay BindingMode, but we won't use it here. The Convert method simply checks if the value is null and if it is, it returns the parameter which will hold the object specified in the ConverterParameter.

public class DefaultValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return parameter;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

In WPF the Binding object has a DefaultValue property, so there's no need to create a ValueConverter there.


You can download the attached code here
I format my code using Manoli's website
Don't forget to vote for your favorite topics for this column.