3

I have two Prism modules. I want one of them register a window and the other one show this window using the "Show Dialog" mode. How can it be done (if it can be done)?

Diego Stiehl
  • 403
  • 8
  • 25

2 Answers2

1

Yes, it can be done. This is rough procedure:

Declare interface for this View in your "Infrastructure" project

public interface IMyDialogWindow
{
}

[Export] class that implements this interface in your module

[Export(typeof(IMyDialogWindow))]
public class MyClassInModuleA : IMyDialogWindow
{
}

[Import] this class in other module and use it for Dialog

[Import]
public IMyDialogWindow PropertyInModuleB
katit
  • 17,375
  • 35
  • 128
  • 256
  • Could you post some example code? I could't understand your answer perfectly. I have to duplicate the files in the other project? I was thinking in a way in which I register the view in the IoC container on the first module and "read" and show it on the second one. – Diego Stiehl Jan 18 '12 at 01:01
  • You have to "glue" ModuleA and ModuleB via interface which can be placed in your "infrastructure" module. This way ModuleB will be able to import your window from ModuleA without knowing about it.. – katit Jan 18 '12 at 01:31
  • [LINK](http://stackoverflow.com/a/2543381/1148768) <- This querist guy seems to have a similar question and I like this answer. It seems to be what I need, but I couldn't well understand how to use the sample code. – Diego Stiehl Jan 18 '12 at 13:05
1

Well. I think I solved it by following this tip. But I don't know if it was the best solution.

I just created a window on my Shell project. This window is the one that will be popped up as a dialog window.

Here is its code:

Popup.xaml:

<Window x:Class="TryERP2.Shell.Views.Popup"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Popup" Height="315" Width="411"
        xmlns:prism="http://www.codeplex.com/prism">
    <Grid>
        <ContentControl x:Name="DialogRegion" Grid.Row="1" prism:RegionManager.RegionName="DialogRegion" />
    </Grid>
</Window>

Popup.xaml.cs:

public partial class Popup : Window
{
    private static Popup popup;

    private Popup(IRegionManager regionManager)
    {
        InitializeComponent();
        RegionManager.SetRegionManager(this, regionManager);
    }

    //Using the singleton pattern
    public static Popup getPopup(IRegionManager regionManager)
    {
        if (popup == null)
            popup = new Popup(regionManager);
        return popup;
    }
}

And, finally, when I want to show the dialog (in a Command which is in a module), I just instantiate it and inform what's the RegionManager:

private void showDialog()
{
    // Acquiring the RegionManager
    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

    // Getting the Popup object
    Popup p = Popup.getPopup(regionManager);

    // Looking for the view I want to show in the dialog
    var x = new Uri("MyView", UriKind.Relative);

    // Changing the view of the DialogRegion (which is within the Popup)
    regionManager.RequestNavigate("DialogRegion", x);

    // Showing the dialog
    p.ShowDialog();
}
Community
  • 1
  • 1
Diego Stiehl
  • 403
  • 8
  • 25