Tuesday, December 20, 2011

Showing a modal and continue to execution of calling window

Again some bits of code. For a couple of reason you may consider not using the ShowDialog method of a window, one is that the calling method does not continue the execution and it blocks until the dialog window becomes closed. I have written my MVVM tool set in a way that this behaviour causes some problems. The other reason is ShowDialog blocks all other windows in your application, so what if you want to open a window over another window in a way that only the calling window becomes blocked. Here is the solution I came up with:

public static class WPFWindowExtensions
    public static void ShowNonBlockingModal(this Window window)
        var parent = window.Owner;
        EventHandler parentDeactivate = (_, __) => { window.Activate(); };
        parent.Activated += parentDeactivate;
        EventHandler window_Closed = (_, __) => { parent.Activated -= parentDeactivate; };

I tried to keep it simple to be more readable, however you may add a check for the window.Owner to have a value and throw an exception if it is null, or you may automatically set the owner with the current active window as described in this blog post.

Friday, December 16, 2011

OnNavigatedTo will be called after selecting a date using DatePicker

Long Story:
During developing my Windows Phone application I faced a strange problem. At first I thought that the problems is related to DatePicker Value binding is not working properly. I tried several things and then finally I relaized that OnNavigatedTo method is being called when the user selects a data or presses the back button and then when this method is executed again I override all the bindings. And the solution is very simple, just check for NavigationMode of event args to not be NavigationMode.Back, here is a sample code:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
    viewModel = new AddWeightViewModel();
    viewModel.Date = DateTime.Today;
    this.DataContext = viewModel;

Short Story:
Always make sure that you add the following lines of code to the OnNavigatedTo method, specially if you have a DatePicker on your page:

    if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)

By adding the above code to the beginning of OnNavigatedTo you prevent from resetting your page data. Keep in mind although the OnNavigatedTo is called, the page is still has its own values.

Thursday, December 15, 2011

Solution to a problem in Application Bar and Binding on the same page

Just a quick share of a useful code I found in MSDN forums for when application bar button invokes the event handler before binding on the current element takes place.
The problem is that if you add a textbox to the page and then user presses a button on the application bar, then if user presses that button just after filling that textbox, then unexpectedly you will not receive the updated value for that textbox's binding target, whatever the reason is the solution for this is to update binding manually knowing the fact that the textbox is the current focused control, here is the code:

public static class Utilities
    public static void MakeSureBindingsApplied()
        var focusObj = FocusManager.GetFocusedElement() as TextBox;
        if (focusObj != null)
            var binding = focusObj.GetBindingExpression(TextBox.TextProperty);

Then what you have to do is just to call it in the event handler for application bar button, make sure that you call this method before reading data from binding's targets.

private void saveButton_Click(object sender, System.EventArgs e)

Wednesday, December 7, 2011

Library for reading emails from a POP3 server

I needed to get the list of messages in a the inbox, so I can process them and save them into a database. After searching a while I found out a good free library for this purpose is OpenPop. Net . This library has a good interface and easy to use. Also is well documented, plus lots of useful examples. And one important thing, it supports SSL, so, for example you can read your emails in your gmail or live accounts.

This is a simple sample code for having your inbox in an Asp.Net page:
No need to say, you have to change the third line with your own gmail credentials:

   Dim client = New OpenPop.Pop3.Pop3Client
   client.Connect("pop.gmail.com", 995, True)
   Dim messageCount = client.GetMessageCount()
   Dim allMessages = New List(Of Net.Mail.MailMessage)(messageCount)
   For i = 1 To Math.Max(10, messageCount)
        Dim message = client.GetMessage(i)
        Dim mailMessage = message.ToMailMessage()
   grdInbox.DataSource = allMessages

Sunday, December 4, 2011

Finally I started Windows Phone 7 development

A couple of weeks ago I attended in a Microsoft's workshop for Windows Phone 7 development just after buying my Samsung Omnia 7 phone, the workshop inspired me with the fact that Windows Phone 7 development is just fun.
So moving to Windows Phone 7 world I will be posting about Windows Phone 7 applications, games, news, development tips in the future.

For the beginning I would like to write about why moving to Windows Phone 7, of course,  from my perceptive. I have several reason but here is two of them that I can share. One is that its development is easy and the other one is that I hope in Windows Phone 7 future.

Reason 1 : Windows Phone 7 development is easy.
Now that I have wrote a couple of applications reading for publishing into market I am thinking of developing Windows Phone 7 application is even easier than writing Desktop or Web applications. For every functionality you desire to add to your application there is a too much simple API.
Also I attended in an Android development workshop at SystemGroup company. I was just pain. In two hours we only learnt how to add a button to UI and then handle it's click button. No need to say iPhone development is even harder than Windows Phone.

Reason 2 : Windows Phone 7 will beat both iPhone and Android in the near future
First of all I have to say I hate iPhone, I know iPhone is the favourite phone for most of you, but to me it never attracted me. I used iPhone as the company's phone, it was nothing special to me. iPhone seems too boring to me. To understand me you have to use both iPhone and Windows Phone for a while, then you will probably get this feeling. Also I am not inspired by the Siri as it is only a non practical funny feature.
Also there are some predictions that in the future Windows Phone will beat Android.