Silverlight confirm dialog imported by MEF

April 6, 2010 at 4:55 PMdsoltesz

I wanted to created a user control that would allow to me ask the user a question and then based on their answer, do something, hence I needed a confirm dialog.  I wanted to be able to use this control by adding a reference or by using MEF just to export/import my confirm dialog.  To start, I created a new silverlight application and then added a childwindow control and named it "ConfirmWindow".  Since I wanted to be able to export this control via MEF, I also created an interface for our confirm dialog.

<controls:ChildWindow x:Class="Axis.iDoc.Window.ConfirmWindow"

         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

           Title="Confirm">

    <Grid x:Name="LayoutRoot" Width="400" Margin="2">

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

 

        <TextBlock x:Name="tbMessage" TextWrapping="Wrap" Grid.Row="0" />

        <Button x:Name="CancelButton" Content="No" Click="CancelButton_Click"  Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />

        <Button x:Name="OKButton" Content="Yes" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />

    </Grid>

</controls:ChildWindow>

Code behind

[Export(PartContracts.ConfirmWindow,typeof(IConfirmWindow))]

    public partial class ConfirmWindow : ChildWindow,IConfirmWindow

    {

        public ConfirmWindow()

        {

            InitializeComponent();

            this.Closed += (s, e) => dialogResult(this.DialogResult);

        }

        Action<bool?> dialogResult;

        public void Show(string message,Action<bool?> windowClosedFunc)

        {

            if (windowClosedFunc != null)

            {

                dialogResult = windowClosedFunc;

            }

 

            tbMessage.Text = message;

            this.Show();

        }

 

        private void OKButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = true;

        }

 

        private void CancelButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = false;

        }

 

 

    }

 

 

You will notice that I have an Export attribute at the top because I want to be able to export my confirm window via MEF.

 

IConfirmWindow.cs

public interface IConfirmWindow

   {

       void Show(string message, Action<bool?> windowClosedFunc);

   }


 

To use this control you can either add a reference to this silverlight application or you can import it via MEF. I decided to import it via MEF.

 

private Lazy<IConfirmWindow> confirmWindow;

 

       [Import(PartContracts.ConfirmWindow, AllowDefault = true, AllowRecomposition = true)]

       public Lazy<IConfirmWindow> ConfirmWindow

       {

           get { return confirmWindow; }

           set

           {

               if (confirmWindow == value)

               {

                   return;

               }

               confirmWindow = value;

 

           }

       }

 

 I created a little helper method to show confirm window.

 

 

public void ShowConfirmWindow(string message, Action<bool?> windowClosedAction)

 

if (ConfirmWindow != null)

 

 To use the confirm dialog is the same regardless if you added a reference to it or you imported from MEF.

 

private void deleteButton_Click(object sender, RoutedEventArgs e)

{

   _window.ShowConfirmWindow("Are you sure you want to delete item?", d => confirmDelete_Closed(d));

}

void confirmDelete_Closed(bool? dialogResult)

{

    if (dialogResult.HasValue && dialogResult.Value)

    {

       //user wants to delete   

    }

}

 

{

ConfirmWindow.Value.Show(message, windowClosedAction);

}

}

 

{

 

Posted in: .NET | c# | silverlight

Tags: ,

Comments (2) -

Nguyen Minh Dzung
Vietnam Nguyen Minh Dzung says:

Very excellent post. Thanks for your helpful article.

Reply

curious
Afghanistan curious says:

Dude,

Your approach has potential.  But...

(1) Where's the body of the ShowConfirmWindow method?
(2) Whence is the ShowConfirmWindow method called?
(3) Your article is written a tad sloppily.  Could you "re-factor" it?
(4) Could you attach a downloadable file of the complete solution?

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading