Zayko in the Net

Personal blog of Vitaly Zayko

NAVIGATION - SEARCH

Custom icons don’t appear in Windows Phone 7 toolbar

Playing with most recent Windows Phone 7 Tools I found that custom icons don’t appear in emulator by default although they exist in a solution.

wp7_no_icons

 <shell:ApplicationBarIconButton IconUri="/Images/add.png" Text="Add" Click="AddClick" />
 <shell:ApplicationBarIconButton IconUri="/Images/remove.png" Text="Remove" Click="RemoveClick"/>

This happens because pictures adding with “Resource” as default build action. To make this work, select your recently added picture in Solution Explorer, switch to Properties and change Build Action to “Content”. After recompiling you will get your icons in place.

vs_menuwp7_with_icons

Technorati Tags:

Adding Buttons to Window Thumbnail in WPF 4 for Windows 7 (C#)

I'm pretty sure you all noticed that when you move your cursor under an App in Windows 7 Task bar, small window copy (aka Thumbnail) appear and there are small buttons on some windows could be attached. Good examples are Windows Media Player or WinAmp.

Windows Player Thumbnail with buttons on it

There is a good reason to put such buttons there: using them you can manage this Application without necessity to open its window.
Here is how to add this functionality to your own WPF App.
Add reference to TaskBatItemInfo just like we did to add progress bar in this post.
Add references to ThumbButtonInfo, so you will see something like this:

 <Window.TaskbarItemInfo>
     <TaskbarItemInfo>
         <TaskbarItemInfo.ThumbButtonInfos>
             <ThumbButtonInfo ImageSource="/IconProgressDemo;component/Icon1.ico" Description="Play!" Click="ThumbButtonInfo_Click" />
             <ThumbButtonInfo ImageSource="/IconProgressDemo;component/Icon2.ico" Description="Stop!" Click="ThumbButtonInfo_Click" />
         </TaskbarItemInfo.ThumbButtonInfos>
     </TaskbarItemInfo>
 </Window.TaskbarItemInfo>
 

Now just handle Click event in way you want and you done!

 private  void  ThumbButtonInfo_Click(object  sender, EventArgs  e)
 {
     MessageBox .Show((sender as  System.Windows.Shell.ThumbButtonInfo ).Description);
 }
 
Technorati Tags: ,,

How to permanently remove TFS Source Control association bindings?

I have faced this issue when I decided to move one of my freelance project from my own TFS to CodePlex.
To re-assign to a new Source Control, you need to remove association to old one first.

Another case when you may need this is when you renamed your TFS and your projects cannot find their code storage and you will see this warning each time you try to open these projects:

TFS is off-line warning

Here is how to remove this association:
1. Remove all *.vssscc and *.vspscc files from your Solution folders.
2. Check if your Solution *.sln file is Read-Only. Remove this flag if needed.
3. Open *.sln file in an editor. I prefer Windows Notepad for such operations.
4. Find GlobalSection(TeamFoundationVersionControl) looking as showing below and remove it begining with GlobalSection and ending with EndGlobalSection:

GlobalSection(TeamFoundationVersionControl) = preSolution
    SccNumberOfProjects = 2
    SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
    SccTeamFoundationServer =
http://<YourTFS>/tfs/defaultcollection
    SccLocalPath0 = .
    SccProjectUniqueName1 = <Your Project path and name in TFS>.csproj
    SccProjectName1 = <Project Name>
    SccLocalPath1 = <Project Path>
EndGlobalSection

5. Save and close your Solution file.
6. Go ahead and open this Solution in Visual Studio IDE as usual.
7. If you will get IDE warning similar as shown below, just select "Permanently remove source control association bindings".

Permanently remove a source control association

That's it! Now you can associate your code with a new TFS Project.
P.S. I tested this on VS/TFS 2010. Not sure if this works with older versions.

Technorati Tags:

How to add progress support to an App icon on Windows 7 Taskbar? (C#)

If you ever work with Windows 7, you already noticed that some Applications could show progress of a long process directly in their icon placed on Windows 7 Taskbar. Good example of this functionality is Internet Explorer when it download a file from Internet or Windows Explorer when you copy/move a file from one location to another. Before .NET Framework 4 era you have had to use external libraries to do this or implement PInvoke by yourself. Now, in your WPF 4 Applications you can use .NET 4 functionality. Here is step-by-step guideline how to do this.

1. Open your progress dialog XAML file. Create reference to Windows.TaskbarItemInfo as very top item of your dialog as shown below (lines 7-9):

1: <Window x:Class="IconProgressDemo.MainWindow" 
2:         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
3:         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
4:         Title="MainWindow" Height="130" Width="490"> 
5:      
6:     <!-- Reference to TaskbarItemInfo: --> 
7:     <Window.TaskbarItemInfo> 
8:         <TaskbarItemInfo /> 
9:     </Window.TaskbarItemInfo> 
10:      
11:     <Grid> 
12:         <StackPanel> 
13:             <!-- Regular WPF ScrollBar: --> 
14:             <ProgressBar Name="progressBar" HorizontalAlignment="Stretch" Height="25" /> 
15:              
16:             <!-- Start long process by pushing this button: --> 
17:             <Button HorizontalAlignment="Center" Margin="0,20" Width="65" Height="25" Click="Button_Click">Start!</Button> 
18:         </StackPanel> 
19:     </Grid> 
20: </Window> 
21: 
22: 

2. Switch to code behind of this dialog. Before process start, set a process state (line #52 below). I’ll explain possible states later. Let start with Normal.

3. Pass a process value to TaskbarItemInfo at the same time as to regular progress bar (lines 65-70).

4. When process finishes toy may want to reset both progress bar and TaskbarItemInfo (lines 58-63).

5. Run this example and press Start button. You will see that Task bar icon shows green progress over it self.

TaskbarItemInfo in Normal state

Now, as promised, let see what other ProgressState values for.

TaskbarItemInfo.ProgressState could be one of the following values:

Normal. As you saw above, this value shows regular green progress on App’s icon.

None. This is default value. This is why it’s important to change this value to any other before you start the process. Otherwise you won’t see any progress on your App icon.

Error. Show red progress bar and suitable for emerging cases when, for example, a non-critical error occurs and you don’t want to interrupt the process but let customer know about such case.

Indeterminate. Shows green ‘wave’ or ‘pulse’. Good for processes when you don’t know how long they will take and have to wait until they finish.

Paused. Shows yellow progress and good for interrupted processes. But note that switching to this value doesn’t interrupt the process it self, you still have to do this manually.

The pictures below illustrate Error, Indeterminate and Paused values of ProgressState property:

Error Indeterminate Paused

And here is full code behind of this example:

1: using  System;
2: using  System.Collections.Generic;
3: using  System.Linq;
4: using  System.Text;
5: using  System.Windows;
6: using  System.Windows.Controls;
7: using  System.Windows.Data;
8: using  System.Windows.Documents;
9: using  System.Windows.Input;
10: using  System.Windows.Media;
11: using  System.Windows.Media.Imaging;
12: using  System.Windows.Navigation;
13: using  System.Windows.Shapes;
14: using  System.Threading;
15: using  System.ComponentModel;
16: 
17: namespace  IconProgressDemo
18: {
19:     /// <summary> 
20:     /// Interaction logic for MainWindow.xaml 
21:     /// </summary> 
22:     public  partial  class  MainWindow  : Window 
23:     {
24:         public  MainWindow()
25:         {
26:             InitializeComponent();
27:         }
28: 
29:         private  void  Button_Click(object  sender, RoutedEventArgs  e)
30:         {
31:             // Simulate long process by using BackgroundWorker: 
32:             using  (BackgroundWorker  bw = new  BackgroundWorker ())
33:             {
34:                 bw.WorkerReportsProgress = true ;
35: 
36:                 // Pass process data: 
37:                 bw.ProgressChanged += new  ProgressChangedEventHandler (bw_ProgressChanged);
38: 
39:                 // Reset the counter: 
40:                 bw.RunWorkerCompleted += new  RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);
41: 
42:                 bw.DoWork += (p, ev) =>
43:                     {
44:                         for  (int  i = 0; i <= 100; i += 10)
45:                         {
46:                             Thread .Sleep(1000);
47:                             ((BackgroundWorker )p).ReportProgress(i);
48:                         }
49:                     };
50: 
51:                 // Set the progress state: 
52:                 TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState .Normal;
53: 
54:                 bw.RunWorkerAsync();
55:             }
56:         }
57: 
58:         void  bw_RunWorkerCompleted(object  sender, RunWorkerCompletedEventArgs  e)
59:         {
60:             // The process was finished, reset the counter: 
61:             progressBar.Value = 0;
62:             TaskbarItemInfo.ProgressValue = 0;
63:         }
64: 
65:         void  bw_ProgressChanged(object  sender, ProgressChangedEventArgs  e)
66:         {
67:             // Pass process counter to both ProgressBar and TaskbarItemInfo: 
68:             progressBar.Value = e.ProgressPercentage;
69:             TaskbarItemInfo.ProgressValue = (double )e.ProgressPercentage / 100;
70:         }
71:     }
72: }
73: 
74: 

Have fun!

Technorati Tags: ,

Encoding a string with MD5 in C#

It is a common sense to keep user passwords encoded so they won't be compromised in case of hacker attack. Here is simple function doing this:

 /// <summary>
 /// Encodes a string using MD5. Don't forget to add ref. to System.Security.Cryptography
 /// </summary>
 /// <param name="password">A string to encode</param>
 /// <returns>Encoded string in Hex format</returns>
 public static string EncodeString(string password)
 {
     string md5encoded;
 
     // MD5 is disposable:
     using (MD5 md5 = new MD5CryptoServiceProvider())
         md5encoded = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(password)));
 
     // Removing dashes to shrink result length:
     return md5encoded.Replace("-", String.Empty);
 }
 

This function will return you string similar to this: E10ADC3949BA59ABBE56E057F20F883E.

Doesn't look like common MD5 string, should be like this: 4QrcOUm6Wau+VuBX8g+IPg== isn't it? To convert our result to such view we need just Bas64 it. Use this line instead of on example above:

 md5encoded = Convert.ToBase64String(md5.ComputeHash(Encoding.ASCII.GetBytes(password)));

So the whole function becoming to this:

 public static string EncodeString(string password)
 {
     string md5encoded;
 
     // MD5 is disposable:
     using (MD5 md5 = new MD5CryptoServiceProvider())
         md5encoded = Convert.ToBase64String(md5.ComputeHash(Encoding.ASCII.GetBytes(password)));
 
     // Removing dashes to shrink result length:
     return md5encoded;
 }
 

 

Is Power of 2? (C#)

Continue playing with Integers. Here is how to determine if a number is power of 2 or not:

 /// <summary>
 /// Checks if an integer is power of 2
 /// </summary>
 /// <param name="i">An int number to check</param>
 /// <returns>true if i is power of 2, false if otherwise</returns>
 public static bool IsPower2(int i)
 {
     return (i != 0) && ((i & -i) == i);
 }

Odd or Even? (C#)

How to quickly determine is given number odd or even? There are several simple methods.

Here is the first one:

 

 public bool IsEven(int i)
 {
     return (i & 1) == 0;
 }

And the second:

 public bool IsEven(int i)
 {
     return (i % 2) == 0;
 }

 

Technorati Tags:

God Mode on Windows 7

Recently taken from here.

Do you want to keep all Windows 7 settings in one simple place? Then create an empty folder on your desktop and rename it to GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}. Amazing!