If you are using some of the predefined modal dialogs part of RadWindow: Alert, Prompt and Confirm you may find this article quite handy.

As you know the calls to these methods are asynchronous, thus you cannot stop the current UI thread in order to get the user feedback from the windows synchronously. This introduces some issues such as the need to declare class member variables whenever they may not be necessary. Due to that there have been a few requests on our support website to create a generic parameter inside the static calls of the dialogs with the purpose of carrying object for further actions, and avoid the declaration of class member variables. Instead of limiting you to a generic Action delegate with one parameter, we concluded that it will be handier if you just create an anonymous method and implement your logic there. To read more about anonymous methods refer to this website.

To demonstrate the best use of anonymous methods consider the following example. I will create a simple navigation control that hosts two buttons.

<UserControl x:Class="SilverlightPlayground.Navigation" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    Width="400" Height="300">  
    <StackPanel x:Name="LayoutRoot" Background="White">  
        <Button Content="Page1" Click="OnNavButtonClicked"/>  
        <Button Content="Page2" Click="OnNavButtonClicked"/>  
    </StackPanel> 
</UserControl> 

To make the things simpler I will just use one method for handling the buttons’ callbacks. Now we want to use a Confirm dialog to confirm the opening of a page. As I explained before, the constraint of having only asynchronous calls to our dialogs means that we have to “store” the result of this callback(the button clicked), and perform some actions depending on it after we get response from the user like this:

public partial class Navigation : UserControl  
    {  
        public Navigation()  
        {  
            InitializeComponent();  
        }  
        private string url;  
        private void OnNavButtonClicked(object sender, RoutedEventArgs e)  
        {  
            this.url = (sender as Button).Content.ToString();  
            RadWindow.Confirm(String.Format("Do you want to open {0}"this.url), OnRadConfirmClosed);  
        }  
 
        private void OnRadConfirmClosed(object sender, WindowClosedEventArgs e)  
        {  
            if (e.DialogResult == true)  
            {  
                this.GoTo(url);  
            }  
        }  
        private void GoTo(string url)  
        {  
            RadWindow.Alert(String.Format("{0} is opened", url));  
        }  
    }  
 
 

Storing the value in a class member variable may not be considered as a best practice, since the variable may not be further used. Using anonymous methods we can simplify the implementation of the button callback to this:

        private void OnNavButtonClicked(object sender, RoutedEventArgs e)  
        {  
            string url = (sender as Button).Content.ToString();  
            RadWindow.Confirm(String.Format("Do you want to open {0}", url),  
                (sender1, args) =>  
                {  
                    if (b.DialogResult == true
                    {  
                        this.GoTo(url);  
                    }  
                });  
        } 

Note: No use of class member variable.


About the Author

Hristo Borisov

Hristo Borisov (@hristoborisov) is currently a product line manager in Telerik leading all cloud technologies part of the Telerik Platform after spending more than 6 years with the company. A passionate advocate of applying lean startup practices to existing organizations, Hristo is on the quest for discovering scalable and sustainable business models through product and customer development using tools like MVPs, Pivots, and Lean Business Model Canvases.

Related Posts

Comments

Comments are disabled in preview mode.