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