InteractionRequest Raise not showing up the Popup (Prism - Interactivity)

1.4k views Asked by At

I'm implementing some interactivity using Prism to show a form in a popup after pressing a button. But the pop-up is not showing.

This is my view where the button is defined (ContactsView):

<i:Interaction.Triggers>
    <prism:InteractionRequestTrigger SourceObject="{Binding ContactInteractionRequest, Mode=OneWay}">
        <prism:PopupWindowAction>
            <prism:PopupWindowAction.WindowContent>
                <views:AddContactPopupView />
            </prism:PopupWindowAction.WindowContent>
        </prism:PopupWindowAction>
    </prism:InteractionRequestTrigger>
</i:Interaction.Triggers>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <DockPanel Grid.Row="0" Background="#D6D6DC">
        <ToolBar Style="{StaticResource ModuleToolBarStyle}">
            <TextBlock Margin="10,0,0,0" Text="Contacts"></TextBlock>
            <Button Name="addContactButton" ToolTip="Add Contact"
                    Command="{Binding RaiseAddContactInteractionCommand}">
                <Image Source="/PrismApp.Controls;component/Images/add.png"/>
            </Button>
            ...
        </ToolBar>
    </DockPanel>
    ...
</Grid>

This is the AddContactPopupView:

<UserControl x:Class="PrismApp.Modules.Configuration.Contacts.Views.AddContactPopupView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:prism="http://prismlibrary.com/" 
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="700" d:DesignWidth="500">   
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <DockPanel Grid.Row="0">
            <ToolBar Style="{StaticResource ModuleToolBarStyle}">
                <TextBlock Margin="10,0,0,0" Text="Add contact"/>
                <Button Name="addAndCloseButton" ToolTip="Add Contact" Command="{Binding ConfirmCommand}">
                    <Image Source="/PrismApp.Controls;component/Images/add.png"/>
                </Button>
                <Button Name="cancelButton" ToolTip="Cancel" Command="{Binding CancelCommand}">
                    <Image Source="/PrismApp.Controls;component/Images/cancel.png"/>
                </Button>
            </ToolBar>
        </DockPanel>
        <Grid Margin="0,0,7,0" Grid.Row="1">
            ...
            <!-- User controls to fill the contact details -->
        </Grid>
    </Grid>
</UserControl>

This is the AddContactPopupViewModel:

public class AddContactPopupViewModel : BindableBase, IInteractionRequestAware
{
    private ContactNotification notification;

    public AddContactPopupViewModel()
    {
        this.ConfirmCommand = new DelegateCommand(this.AcceptContactConfiguration);
        this.CancelCommand = new DelegateCommand(this.CancelInteraction);
    }

    public Action FinishInteraction { get; set; }

    public INotification Notification
    {
        get
        {
            return this.notification;
        }
        set
        {
            if (value is ContactNotification)
            {
                this.notification = value as ContactNotification;
                this.OnPropertyChanged(() => this.Notification);
            }
        }
    }

    public ICommand ConfirmCommand { get; private set; }

    public ICommand CancelCommand { get; private set; }

    public void AcceptContactConfiguration()
    {
        if (this.notification != null)
        {
            this.notification.Confirmed = true;
        }

        this.FinishInteraction();
    }

    public void CancelInteraction()
    {
        if (this.notification != null)
        {
            this.notification.Name = null;
            this.notification.LastName = null;
            this.notification.Telephone1 = null;

            this.notification.Confirmed = false;
        }

        this.FinishInteraction();
    }
}

This is the ContactNotification custom class:

public class ContactNotification : Confirmation
{
    public ContactNotification()
    {

    }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Telephone1 { get; set; }
}

And finally, this is ContactsViewModel:

public class ContactsViewModel : BindableBase
{
    private readonly IRegionManager regionManager;
    private readonly IEventAggregator eventAggregator;
    private readonly IConfigurationContactsService contactsService;

    private readonly DelegateCommand<object> deleteContactCommand;

    private ObservableCollection<Contact> contactsCollection;
    private ICollectionView contactsView;

    public ContactsViewModel(IEventAggregator eventAggregator, IConfigurationContactsService contactsService, IRegionManager regionManager)
    {
        this.regionManager = regionManager;
        this.contactsService = contactsService;
        this.eventAggregator = eventAggregator;

        this.deleteContactCommand = new DelegateCommand<object>(this.DeleteContact, this.CanDeleteContact);

        this.contactsCollection = new ObservableCollection<Contact>(contactsService.GetContacts());
        this.contactsView = CollectionViewSource.GetDefaultView(this.contactsCollection);

        this.ContactInteractionRequest = new InteractionRequest<ContactNotification>(); // Here is the InteractionRequest instantiated
        this.RaiseAddContactInteractionCommand = new DelegateCommand(this.RaiseAddContactInteraction);
    }

    public ICollectionView ContactsView
    {
        get { return this.contactsView; }
    }
    public ObservableCollection<Contact> Contacts
    {
        get { return this.contactsCollection; }
    }

    public ICommand DeleteContactCommand
    {
        get { return this.deleteContactCommand; }
    }

    private void DeleteContact(object ignore)
    {
        IList<Contact> selectedContacts = contactsService.GetSelectedContacts();
        foreach (Contact contact in selectedContacts)
        {
            if (contact != null)
            {
                contactsService.DeleteContact(contact);
                Contacts.Remove(contact);
            }
        }
    }
    private bool CanDeleteContact(object ignored)
    {
        return true;
    }

    public InteractionRequest<ContactNotification> ContactInteractionRequest { get; private set; }
    public ICommand RaiseAddContactInteractionCommand { get; private set; }
    private void RaiseAddContactInteraction()
    {
        ContactNotification notification = new ContactNotification();
        notification.Title = "New contact";

        this.ContactInteractionRequest.Raise(notification,
            returned =>
            {
                if (returned != null && returned.Confirmed)
                {
                    var result = contactsService.AddContact(new Contact
                    {
                        Name = returned.Name,
                        LastName = returned.LastName,
                        Telephone1 = returned.Telephone1,
                    });

                    if (!result)
                        throw new NotImplementedException("TODO: Handle this");

                    SetProperty<ObservableCollection<Contact>>(ref this.contactsCollection, new ObservableCollection<Contact>(contactsService.GetContacts()), "Contacts");
                }
                else
                {
                    // TODO
                }
            }); // After this, AddContactPopup should popup in a new window. But nothing happens
    }
}

After ContactInteractionRequest.Raise is called, a new window should pop up, but nothing happens. I'm struggling to find the issue. Any ideas where to look at?

1

There are 1 answers

6
Haukinger On BEST ANSWER

Any ideas where to look at?

You have to cleanup your references. Mixing Prism 5 and Prism 6 doesn't work too well, or rather, doesn't work at all.

So remove everything prism related from your packages.configs and project references and then add prism 6 from nuget (I just added Prism.Unity and it brought everything needed). Then you have to correct some usings and the module confiuration in app.config to <section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" /> and the popup shows up as expected.