【问题标题】:InteractionRequest Raise not showing up the Popup (Prism - Interactivity)InteractionRequest Raise 不显示弹出窗口(Prism - 交互性)
【发布时间】:2017-01-02 15:53:22
【问题描述】:

我正在使用 Prism 实现一些交互性,以便在按下按钮后在弹出窗口中显示表单。但是没有显示弹出窗口。

这是我定义按钮的视图(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>

这是 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>

这是 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();
    }
}

这是 ContactNotification 自定义类:

public class ContactNotification : Confirmation
{
    public ContactNotification()
    {

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

最后,这是 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
    }
}

调用 ContactInteractionRequest.Raise 后,应该会弹出一个新窗口,但没有任何反应。我正在努力寻找问题。有什么想法可以去哪里看?

【问题讨论】:

  • 确保只创建了 ContactsViewModel 类的一个实例,并通过在方法中设置断点调用此特定实例的 RaiseAddContactInteraction() 方法并调试您的应用程序。
  • 是的。当我将视图模型注册为单例并且 RaiseAddContactInteraction() 被命中时,只创建了一个实例。执行了这一行:"this.ContactInteractionRequest.Raise(notification, returned =>..." 但没有弹出窗口。
  • 如果您尝试使用更简单的窗口内容会怎样?:test...。如果这可行,则您的 AddContactPopupView 可能有问题。
  • @mm8 也不行……
  • 我想您需要提供一个示例应用程序,您的问题可以在其中重现,以便任何人都能够帮助您。

标签: wpf prism popupwindow interaction


【解决方案1】:

有什么想法可以看吗?

你必须清理你的引用。将 Prism 5 和 Prism 6 混合使用效果不太好,或者说根本不起作用。

所以从你的packages.configs 和项目引用中删除所有与棱镜相关的东西,然后从 nuget 添加棱镜 6(我刚刚添加了Prism.Unity,它带来了所需的一切)。然后你必须更正一些usings 和app.config 中的模块配置为&lt;section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf" /&gt; 并且弹出窗口按预期显示。

【讨论】:

  • 您好,感谢您的回答。我正在按照您的步骤操作,但是在编译期间我从 Bootstrapper 的 ConfigureRegionAdapterMappings() 方法中收到了一个我无法修复的错误。这是错误:“Microsoft.Practices.Unity.IUnityContainer”不包含“Resolve”的定义和最佳扩展方法重载“Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(Microsoft.Practices.Unity. IUnityContainer, params Microsoft.Practices.Unity.ResolverOverride[])' 有一些无效参数'。你是如何解决这个错误的?你也遇到过这种情况吗?
  • 在移除 prism 5 的东西后,我已经看到了 100 多个这样的错误,但是当我移除过时的使用并添加 prism 6 时,所有错误都消失了......
  • 我只是弄得一团糟,因为我不知道哪些用途适用于棱镜 6,哪些不是。你能分享一个更正项目的链接吗?
  • 我在写完答案后删除了它:D Resharper 用红色强调了错误的部分并建议了正确的部分,所以应该在几分钟内就能把它弄好。
  • 完成,实际上不到五分钟的工作...filebin.ca/37hVaqqGxbJg/PrismApp_unity_interaction_fixed.zip
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2021-10-19
相关资源
最近更新 更多