Zhanbo Sun [MS]

Realize .NET & Windows Vista Potential

  • Silverlight QuickStart Using Microsoft Expression Blend 2

    Filed under:

    Silverlight QuickStart Using Microsoft Expression Blend 2 is live now. It is similar to the the Silverlight 1.0 Beta QuickStart except that it focuses on using the designer tool Microsoft Expression Blend 2 to create Silverlight graphics and UI rather than working directly in XAML code. This tutorial consists of step-by-step instructions of how to use Blend 2 to create a media player, with detailed explanation and complete screenshots.

    Download Microsoft Expression Blend 2 at http://www.microsoft.com/expression/products/download.aspx?key=blend2maypreview.

  • 5 Intensive Nights, 1.1 Silverlight Game

    Filed under:

    Vincent Vergonjeanne just posted BubbleFactory game written with Microsoft Silverlight 1.1 Alpha. You can play with it at http://cosmik.members.winisp.net/BubbleFactory/. And if you are really good at this game, you can get yourself in its scoreboard (top 10 scores).

    Vincent spent five nights working tirelessly on this game. We have to agree: It is very cool!

  • Windows Vista Tips (1-3)

    Filed under:

    I would like to share a few tips in using Windows Vista. I verified them in both Windows Vista Ultimate and Windows Vista Business, but they should be applicable to other versions as well. Maybe you have found one or two, but most of my friends were unaware of them until I did a quick demo.

    1. For the Date and Time dialog that pops up when you single click the Clock icon on the task bar, it shows view for the current month. But you can change it to be 12-month view by clicking on the month hypertext between left and right arrows. For example:


      And if you click on year (e.g. 2007) hypertext, you enter view for 2000-2009. The transition is quite sleek. And it can help you navigate calendars back and forth more efficiently.
    2. Search box on Control Panel is aware of many keywords associated with various applets. Try typing the following words to see if it can save you time: DPI, battery, or User.
    3. Flip (Alt+Tab) and Flip 3d (Window + Tab) mode can be made stay on the screen when you also use Ctrl key.

    Do you have your favorite Windows Vista tips to share?

  • Just .NET (1): CurrentCulture or CurrentUICulture as IFormatProvider in String.Format()?

    Filed under:

    Either Thread.CurrentCulture or Thread.CurrentUICulture can serve as value for the first parameter in String.Format Method (IFormatProvider, String, Object[]). But they are not interchangeable. We should use Thread.CurrentCulture here instead of Thread.CurrentUICulture.

    The .NET Framework uses the terminology “culture” to represent what might have traditionally been called the “locale”. The .NET Framework has two concepts of the active culture.
    UICulture, which is indicated by the Thread.CurrentUICulture property, corresponds to the language of the OS by default, or the selected language on a multi-language version of Windows. This affects which resources get loaded, so it determines which strings and pictures the user sees.

    CurrentUICulture is used almost exclusively for loading resources through the resource manager. If you don’t specify a culture to the resource manager, it defaults to CurrentUICulture.

    Culture, which is indicated by the Thread.CurrentCulture property, corresponds to the selection in Regional Options in the control panel by default. CurrentCulture affects how numbers, dates and times are formatted, and this is also what determines which sorting and casing rules to use.

    For String.Format(), it should use CurrentCulture. Since users are most comfortable in their own culture, CurrentCulture helps ensure that information is presented to users in the format they most prefer.

  • Happy Lunar New Year 2006

    Filed under:

    For people like me who also celebrate lunar New Year, one of its benefits is that I can make my New Year Resolutions again.

    One of my (Lunar) New Year Resolutions is to write more in longhornblogs. And I start with this post right after midnight of New Year's Eve (Pacific Time).

    2006 is the year Windows Vista (codenamed Longhorn) ships and I am really excited about this release. 2006 also happened to be my office number while I was working with Windows Presentation Foundation (codenamed Avalon) in building 10. Starting from the end of December 2005, I am with Visual Designer for Windows Presentation Foundation (codenamed Cider) team.

    If you need an introduction of Cider, you can start with this Channel 9 video featuring Mark Boulter and Brian Pepin. We have already released two CTPs (Community Technical Preview): December CTP and January CTP, with more to come.

    Going forward, I plan to write more on what I have learnt about Cider, Avalon, as well as .NET in general. Realizing the full potential of .NET can only be achieved by small progress we make day in and day out. Sharing knowledge goes a long way toward the goal.

    Have a Happy Lunar New Year!

  • Avalon Q&A 101-102 (on XAML)

    Q101: In XAML file, what can be used as x:Key value within Resources section?

    Answer: A key for a dictionary can be String, LiteralExtension, TypeExtension and StaticExtension. You will get this error message from parsing the following XAML:
    <Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
            <SolidColorBrush Color="LightBlue" x:Key="{x:Null}"/>

    Q102: What if I want to treat {x:Null} as string literal, instead of MarkupExtension?

    Answer: {} is used in XAML file as escape sequence for this purpose. Parser will take whatever follows {} as string literal (with leading and trailing spaces trimmed). Try the sample below in which {}{x:Null} is used. Note: {} must be the first and second character.

    <Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
            <SolidColorBrush Color="LightBlue" x:Key="{}{x:Null}"/>
        <Button Content="Sample 102" Background="{StaticResource {}{x:Null}}"/>

    (This posting is provided "AS IS" with no warranties, and confers no rights.)
  • Bill Gates Keynote from WinHEC 2005

    In his keynote presentation (April 25th, 2005), Microsoft Chairman and Chief Software Architect Bill Gates reflects upon the hardware industry over the last 20 years and how the introduction of 64-bit and multicore computing will create a wave of industry innovation. Gates also provides a view into the foundation being laid for the release of Microsoft Windows "Longhorn."

    The on-demand webcast links are posted at http://www.microsoft.com/events/executives/billgates.mspx

  • WinHEC 2005 Starts

    The annual Microsoft conference on computer hardware and drivers, WinHEC 2005 starts today. It also hosts the launch event for Windows x64. (More highlights.) Of course, many people are very interested in previewing latest longhorn build. Attendants received Longhorn build 5048

    In today’s Wall Street Journal article, Microsoft Puts Early Spotlight On Next Windows Version, it discusses almost exclusively on UI improvement in Longhorn. Of course, Longhorn has much more to offer to be “touted as the most significant product upgrade since Windows 95 a decade ago” (from WSJ article paragraph 16). Just take a moment to open WinHEC 2005 sessions and search for longhorn. 

    And if you also search for Avalon, you can find two relevant sessions: Avalon Imaging Codec Architecture, and Advances in Display and Composition Architecture for Windows. Not too many sessions in WinHEC. Wait until PDC 2005 for much more Avalon coverage. 

    I end this post with a question for my reader. The Wall Street Journal article says “The interface enhancements, based on an underlying technology called Avalon, are designed to give users more immediate visual cues about the nature and number of files they have.” (Paragraph 8) Do you see anything inaccurate here?

  • DockPanel.DockProperty, of type DependencyProperty, is a CLR field

    In Avalon property system, DockPanel.DockProperty is an instance of DependencyProperty. DependencyProperty is a CLR class, and DockProperty is a CLR field defined within DockPanel class:

    .field public static initonly [WindowsBase]System.Windows.DependencyProperty DockProperty

    To be accurate, DockProperty is a public static read-only field. Because it is public and static, any code can access it via DockPanel.DockProperty. Because it is read-only, no code can change its value (to be another DependencyProperty instance, or null) after it is initialized within static constructor. 

    Being a read-only field does not make DockProperty a read-only Dependency Property. My discussion about read-only dependency property appears in the next blog. 

    In brief, DockPanel.DockProperty is

    Within Avalon Property System

        An attached read-write dependency property

    Within CLR

        A public static read-only field

     I end this post with a question for my reader. There is another DependencyProperty defined/owned by DockPanel: LastChildFillProperty. Does the table above apply to it as well?

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

  • Locally set value takes precedence over Style value

    (Note: Whoever has a better tool to post code into this blog system please contact me. )

    If a DependencyProperty’s value is locally set, this value takes precedence over whatever value specified in Style. For example, the only button’s Background is LightGreen instead of LightBlue:

    Read more, at http://blogs.msdn.com/zhanbos/archive/2005/04/22/410683.aspx.

  • FrameworkPropertyMetadata.Inherits

    Filed under:

    FrameworkPropertyMetadata.Inherits indicates whether the DependencyProperty registered with this metadata is inheritable or not. For example, Binding.DataContextProperty is inheritable, yet DockPanel.DockProperty is not.

    The demo code below firstly creates a DockPanel, a StackPanel, and a Button. To differentiate between getting a value from inheritance and getting the default value, we then set DockProperty value for stackPanel1 to be Dock.Top. We also set DataContextProperty for stackPanel1 to be dockPanel1. (Default values for DockProperty and DataContextProperty are Dock.Left and null respectively.)

    Before and after we add button1 into tree, we print out its values for DockProperty and DataContextProperty. It clearly shows the effect of property inheritance. 

    You can put all the code inside Click event handler for a button.

        // Sample event handler: 
        private void ButtonClick(object sender, RoutedEventArgs e)
          DockPanel dockpanel1 = new DockPanel();
          StackPanel stackPanel1 = new StackPanel();
          Button button1 = new Button();

          DockPanel.SetDock(stackPanel1, Dock.Top);
          Binding.SetDataContext(stackPanel1, dockpanel1);

          System.Diagnostics.Debug.WriteLine("button1.DockProperty is " + DockPanel.GetDock(button1).ToString());
          object value1 = Binding.GetDataContext(button1);
          System.Diagnostics.Debug.WriteLine("button1.DataContextProperty is " + (value1==null?"null":value1.ToString()));

          System.Diagnostics.Debug.WriteLine("button1.DockProperty is " + DockPanel.GetDock(button1).ToString());
          value1 = Binding.GetDataContext(button1);
          System.Diagnostics.Debug.WriteLine("button1.DataContextProperty is " + (value1 == null ? "null" : value1.ToString()));

    The output is as follows:
    button1.DockProperty is Left
    button1.DataContextProperty is null
    button1.DockProperty is Left
    button1.DataContextProperty is System.Windows.Controls.DockPanel

    There are various other ways to affect a DependencyProeprty’s value. More blogs to come.

    (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

  • Visual Studio 2005 Shortcut Keys

    The more you play with March 2005 CTP with VS2005, the more likely you will pick up new shortcut keys to speed up your programming. Here are just five of them (for Visual C# projects):

    1. Ctrl+K, Ctrk+X: Insert Snippet
    2. Ctrl+K, Ctrl+S: Surround With
    3. Ctrl+R, Ctrl+R: Rename (One commonly used Refactor operations)
    4. Ctrl+Alt+↓(the down arrow key): Show Active files dropdown menu
    5. Ctrl+Tab: Show active files and tool windows for easy navigation among them

    There are plentifully more and they are subject to change. Of course, many existing shortcut keys in VS.NET 2002/2003 should continue to work in VS2005. For example, pressing Ctrl+Alt+O gives focus to Output window. In future code snippets where System.Diagnostics.Debug.WriteLine()gets used, you can see the result in Output window. 
     (This posting is provided "AS IS" with no warranties, and confers no rights.)

  • What’s New in Avalon March 2005 CTP

    For people who are following WinFX in general and Avalon in particular, this MSDN technical article is very informative: Introducing the March 2005 CTP: What's New in "Avalon". It provides two sample projects you can download and play with while you are reading it. 

    One correction though: We do not have Parser.LoadXAML() method. It should be Parser.LoadXml(). This method is not new in March CTP, but the removal of loose XAML activation support is.

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

  • CLR Event vs. Routed Event: Two Demo Applications

    Because it is extremely difficult to paste code snippet into longhornblogs' new rich text editor, I have to move the bulk of my post into msdn blogs.

    As you may have already known, you can define and use Dependency Property as well as CLR Property in Avalon. (If you want to know more about Avalon’s property system, stay tuned in this blog.) Same thing can be said about CLR Event and Routed Event. By supporting routed event in Avalon, the parent element can participate in events sourcing from its child elements. This blog post presents two applications to show the differences between CLR event and RoutedEvent.


    Complete post at http://blogs.msdn.com/zhanbos/archive/2005/04/02/404901.aspx

  • Raise Routed Event through RaiseEvent

    Filed under: ,

    (Version: Avalon CTP 2004 November)


    Both UIElement and ContentElement implement IInputElement interface, which defines RaiseEvent method.  This post gives you an example of using it.


    We start with a simple Xaml file:

    <Window x:Class="AvalonApplication1.Window1"

        xmlns="http://schemas.microsoft.com/2003/xaml" xmlns:x="Definition"



            <Button ID="btn1" Content="Button 1" Click="ButtonClick1"/>

            <Button ID="btn2" Content="Button 2" Click="ButtonClick2"/>




    Two event handlers are defined as follows:

    private void ButtonClick1(object sender, RoutedEventArgs args)


      //Do Nothing



    private void ButtonClick2(object sender, RoutedEventArgs args)


      MessageBox.Show("Button2 Just Received Click Event");



    So when you click on btn2, you see a MessageBox. But when you click on btn1, you do not see anything in particular.


    Now we are going to add code into ButtonClick1 so that it will raise Click event on btn2. As a result, the same MessageBox shows up when you click on btn1. The code below makes use of RaiseEvent:


    private void ButtonClick1(object sender, RoutedEventArgs args)


      RoutedEventArgs evtArgs = new RoutedEventArgs();





    Note we do not call ButtonClick2 directly. Instead, we raise the Click event on btn2. ButtonClick2 is called to handle that event.


    I end this post with a question for my reader: What will happen if we change ButtonClick1 code again? Are you going to see a MessageBox, a ContextMenu , an Exception, or nothing at all?


    private void ButtonClick1(object sender, RoutedEventArgs args)


      RoutedEventArgs evtArgs = new RoutedEventArgs();





    (This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

1 2 Next >