Sunday, October 28, 2007

Call for action: Command and Control by using Microsoft technology

This is an invitation to attend at the event, arranged by Microsoft Israel and mPrest systems about building Homeland security, C&C and other military systems, by using latest Microsoft technologies. The event will take part at 26-Dec (8:30AM - 1:30PM) at Microsoft Israel building in Ra'anana.

Agenda:

  • C&C Windows and Web client development by using WPF and Silverlight - Myself
  • State-of-Art C&C server (mCore) and generic client development - Eli Arlazoroff
  • WCF + Virtual Earth for mission data visualization - Lior Rozner

If you are in military or other RT industry, this is one of "must attend" events. Register@MSEvents.

RTL support for Silverlight - community sneak preview

About three months ago, I blogged about work-around solution to display right-to-left text in Silverlight. A bit after, Microsoft MVP Justin-Josef Angel begun to work on real solution. And today it's ready. I checked a code, posted on Microsoft development community site CodePlex and found it very nice and useful. So, if you have a problem with Hebrew, Arabic or any other RTL language in Silverlight. Use it (you can try it or see webcast as well), before manufacture's  solution will come (and it'll come, I believe, some day). Very nice work, Justin!

Friday, October 26, 2007

Windows Live Gallery wave 2 update (commercial)

Cool new update come today to Windows Live Gallery. Now you, as developers can sell your gadgets and as customers to buy them. Money tree? Not exactly. Purchasing or selling might be done by using Microsoft Points (those of XBox). Cool feature, that, probably, will increase the amount of available gadgets, by motivating developers to build and sell them. Meanwhile all my gadgets are fee :)

Wednesday, October 24, 2007

Clipboard SetData / GetData troubles with VPC and TS

This one of those posts you can categorize is WTF (see me WPF presentation to know what is it) it is not working. However, you, probably will face with the problem now or after.

Symptoms: You are trying to set and get clipboard data from Virtual PC or remote computer by using Terminal Services and nothing happens. When you are using .NET, you're getting Unhandled exception. If you'll dig deeper, you'll find 0x800401D0 (CLIPBRD_E_CANT_OPEN) exception.

Reason: Passing data through TS or VPC pipes bug

Solution: Just retry a couple of times.

Actually, in unmanaged and COM world, you'll never face this problem, because of the way we are handling OleSetClipboard() and OleGetClipboard() methods (which wrapped by .NET Clipboard.GetData/Clipboard.SetData Set/Get Text etc. methods).  So, something like following functions will solve your problems.

public static IDataObject GetRemoteClipboardDataObject()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    return Clipboard.GetDataObject();
                }
                catch { }
                System.Threading.Thread.Sleep(100);
            }
        }

public static void SetRemoteClipboardDataObject(object data)
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    Clipboard.SetDataObject(data);
                    return;
                }
                catch { }
                System.Threading.Thread.Sleep(100);
            }
        }

Now, to be honest with myself, take a look into one of Clipboard.SetDataObject method signatures in .NET framework 2.0

//Attempts to place data on the system Clipboard the specified number of times and with the specified delay between attempts, optionally leaving the data on the Clipboard after the application exits. 


public static void SetDataObject (
Object data,
bool copy,
int retryTimes,
int retryDelay
)



Don't it looks familiar? :) Have a nice day and be honest with yourselves.

Tuesday, October 23, 2007

Merge your Live accounts

New huge feature was added to wave 2 of Windows.Live - merging of Live accounts. Just go to this page and merge all of them. From now, you can use all Live products by choosing proper account from upper right corner, rather then relogin with other credentials.

via LifeSide

Sunday, October 21, 2007

How to filter ItemsControl View, rather then Data Source

How to filter data source of ItemsControl, you, probably, know, but what to do, when you want to filter view of the control's items, rather then the data behind? What's the strange requirement, you'll ask. I'll give you very simple example for business case, when you need to filter only what you are viewing in ListBox or any other items control, rather then data behind. You have two listboxes bounded to the same data source. In one, and only one, of those listboxes you want to filter data. What to do?

image

If  you'll filter data by using CollectionViewSource.GetDefaultView(YourListBox.ItemsSource).Filter = new Predicate(FilterOdds) statement or, even YourListBox.Items.Filter = new Predicate(FilterOdds) statement, you'll filter data in both ListBoxes. This is not what you want. You need to filter the view of the data and not a data itself.

Just enumerate listbox items and Collapse "unnecessaries", according predefined criteria. Like following:

for (int i = 0; i < MyListBox.Items.Count; i++)
{
  ListBoxItem itm = MyListBox.Items[i] as ListBoxItem;
   if (FilterOdds(itm.Content))
    MyListBox.Items[i].Visibility = Visibility.Collapsed;
}

image

But, if you want to be even more precise, pay attention on what has been virtualized and what has not by getting underlying VirtualizingStackPanel and enumerating and operating it's items. How to do it? Get and see in source code for this article.

Thursday, October 18, 2007

How to build dynamic menus and toolbars

The challenge is as following:

  • I want to have external XAML files with menus and toolbars
  • I do not want to recompile project if I want to change something in those menus and toolbars
  • I want to provide commands for those menus and toolbars without recompiling
  • I want to write least amount of code to provide such functionality

What's the problem? The answer is XamlReader. First let's create menus and toolbars in external loose XAML files and put them into our BIN directory.

Menu1.xaml

<Menu xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <MenuItem Header="Edit">
    <MenuItem Command="ApplicationCommands.Cut"/>
    <MenuItem Command="ApplicationCommands.Copy"/>
    <MenuItem Command="ApplicationCommands.Paste"/>
  </MenuItem>
</Menu>

ToolBar1.xaml

<ToolBar xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Button Command="ApplicationCommands.Cut">
    <Image Source="pack://siteoforigin:,,,/cut.png"/>
  </Button>
    <Button Command="ApplicationCommands.Copy">
      <Image Source="pack://siteoforigin:,,,/copy.png"/>
    </Button>
    <Button Command="ApplicationCommands.Paste">
      <Image Source="pack://siteoforigin:,,,/paste.png"/>
    </Button>
</ToolBar>

 

Now let's create out window

Window1.xaml

<Window x:Class="DynamicMenu.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DynamicMenu" Height="300" Width="300"
    >
  <Window.Resources>
    <Style TargetType="Image">
      <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Opacity" Value="0.5"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
    <StackPanel Name="main">
      <TextBox/>
      <TextBox/>
    </StackPanel>
</Window>

 

Now, the only thing we should do it to read Menu1.xaml and TooBar1.xaml from our BIN directory and add Menu and ToolBar controls dynamically to the page

public Window1()
       {
           InitializeComponent();
           using (FileStream s = new FileStream("Menu1.xaml", FileMode.Open))
           {
               Menu menu = XamlReader.Load(s, new ParserContext()) as Menu;
               if (menu != null)
               {
                   main.Children.Insert(0, menu);
               }
           }
           using (FileStream s = new FileStream("ToolBar1.xaml", FileMode.Open))
           {
               ToolBar tool = XamlReader.Load(s, new ParserContext()) as ToolBar;
               if (tool != null)
               {
                   main.Children.Insert(1, tool);
               }
           }
       }

 

image

We done. Have a nice day :)

Source code for this article

Tuesday, October 16, 2007

Very useful Excel feature

After Excel bugs, where resolved, I started to pay attention to what this program provides me. I noticed about very useful feature of automatic calculation of averages and summaries of selected cells. Here the really useful result of such feature. Long life, Excel 2007

image

Monday, October 15, 2007

Sorted dictionary class for Silverlight

Yes, Silverlight 1.1 is far from being perfect. It's lack of some very important classes. The good example for it is SortedDictionary and binary search tree. Why it's so important and what top-down (red-black) binary search trees are?

If you want to use generic dictionary not only for storing key-value information, but also for searching and accessing data by other criteria,  you, probably, can implement IComparable interface and for each new data entry or update resort whole list. But, if you are looking not only for such functionality, but, also, for performance, you'd like to use black-red trees to achieve the behavior.  So, what we really have?

  • Our data exists in Key-Value pairs
  • Total order of items is predefined
  • We can sort by such criteria keys and values and not only keys.

All those features are defined by Binary Search Tree (or BST). But SortedDictionary is maintaining not only those trees, but, also, the criteria or, other words, the order. It also uses sequential algorithm to locate keys. SortedList has linear search time, as the last key in the list will require comparisons to every preceding key, so item insertion, removal and retrieval time will be O(log n) contrasts SortedList, that inserts and removes items with O(n) time and lookup O(log n). So, it's obvious, that SortedDictionary works much faster with large amount of frequent changing data. However, it uses much more objects on heap, due to fact, that all of it's items are linked together with tree, wrapped in a Node (that internally implemented reference type)

Each application should choose what type of sorted collections to use, however, it's really important to understand, that even in SL application, we should have SortedDictionary class.

I got shared source CLI for 2.0 (ECMA CLI and C# for XP), which is freely available through MSDN and wrap and fix SortedDictionary class to be used within Silverlight constraints. So, if you need such class in your Silverlight application, download it here.

Sunday, October 14, 2007

Formatting templates and one small tip

From internal discussion list:
Are there any samples around doing this kind of formatting with WPF?

clip_image002

My response: This should be very easy to do with value converter for Background brush of Item template.

How to do it?

First of all, let's understand what we want to archive? We want our gradientbrush to change it's end point, according to relative value passed to the item. So, let's do it

<DataTemplate x:Key="template">
      <TextBlock Text="{Binding}" >
        <TextBlock.Background>
          <LinearGradientBrush EndPoint="{Binding Converter={StaticResource converter}, ConverterParameter=1000}" StartPoint="0,0.5">
            <GradientStop Color="#FF7CA0CF" Offset="0"/>
            <GradientStop Color="#FF7CA0CF" Offset="0.8"/>
            <GradientStop Color="#FFFFFFFF" Offset="1"/>
          </LinearGradientBrush>
        </TextBlock.Background>
      </TextBlock>
    </DataTemplate>

In the code of converter we'll just translate values, according the max value, passed as parameter.

class DataConverter:IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int val = (int)value;
            int max = int.Parse(parameter.ToString());
            return new Point(val == 0 ? 0 : ((double)val / (double)max), 0.5);
        }

Very good. Compile and run. We get wrong result. Why? Due to fact, that TextBlock control does not resizes and stretches, according it's parent width. We can do it hardcoded for sure, but we want it automatically. Binding will help us another time. But this time, it's relative binding to ancestor's value.

<TextBlock Text="{Binding}" Margin="5,5,5,5" TextAlignment="Right" Padding="0,0,20,0"  Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=ActualWidth}">

That's all. We got the behavior, we looked for. See yourself.

image

Source code for this article.

Friday, October 12, 2007

Visual Studio 2008 beta 2 is crashing? Install this patch

Most of clients, installed VSTS 2008 Beta 2 complained about rapidly crashing IDE. The core problem was found in IntelliSense feature of C# and yesterday new patch was released. So, if you are running into this problem and using English SKU, please install the patch to fix the problem.

Thursday, October 11, 2007

WPF perf sessions is over and results are coming. What's new (in performance case) in .NET 3.5 and 3.0 SP1

After WPF performance sessions is over, one of WPF performance PMs, Josef Goldberg discovered in his blog about what are good news, will come with WPF (3.5 and 3.0/SP1). Here come the list:

  • Animation smoothing improvements (finally, it'll use DWM engines)
  • Layered Windows Improvements - fix was released about three months ago. This one will be included as well in SP1
  • Data Binding improvements - XMLDataSource will work faster
  • Coldstart time (hmmm, sounds strange, due to fact, that delays mostly come from CLR, not from WPF. But who knows)
  • Workset - less (or more???) space for future possibilities?
  • Software rendering improvements. We'll finally be able to do it manually.
  • Battery life improvements - well, not sounds good :)

But the real good news, that we where sounded. And this is the real value of this event.

Tnx, Yossi :)

Tuesday, October 09, 2007

Life before Starbucks and Microsoft vs. Crapper

In case, you don't know, that it used to be life before Starbucks and Microsoft here the real-life evidence.

In case, that when you hear word "crapper", you are thinking about discarded cast offs. Here the other proof

Be good people - life is good, even with Jack-In-Box and Macy*s

Wednesday, October 03, 2007

Microsoft releases the source code for .NET base classes

Wow, it's finally happening. Microsoft is releasing the source code for base classes of .NET framework. This includes ASP.NET, Windows Forms and WPF. Huge! Now you can break into debugger with the source code of .NET