WPF事件方法正在执行两次

时间:2014-03-01 08:07:26

标签: c# asp.net wpf prism

我正在使用wpf事件来显示弹出窗口。 作为我的活动方法' AddToBasketClicked'正在执行两次,弹出窗口加载两次。 首次打开弹出窗口后,执行操作并关闭窗口后,在执行事件方法后再次加载窗口' AddToBasketClicked'试。

 [Export(typeof(IFigureDetailView))]
public partial class FigureDetailsView : IFigureDetailView
{
    protected IEventAggregator EventAggregator
    {
        get { return MefFactory.CompositionContainer.GetExportedValueOrDefault<IEventAggregator>(); }
    }

    public FigureDetailsView()
    {
        LoggingManager.Debug("Entered into FigureDetails of FigureDetails.xaml.cs-TMSSS.PIT.Modules.Tempo.Views");
        InitializeComponent();

        var viewModel = MefFactory.CompositionContainer.GetExportedValueOrDefault<IFigureDetailViewModel>();

        ViewModel = viewModel;
        viewModel.EventAggregator.GetEvent<AddToBasketClickedEvent>().Subscribe(AddToBasketClicked);
        LoggingManager.Debug("Exited from  FigureDetails of FigureDetails.xaml.cs-TMSSS.PIT.Modules.Tempo.Views");
    }

    private void AddToBasketClicked(Guid figureItem)
    {           
        LoggingManager.Debug("Entered into AddToBasketClicked of FigureDetails.xaml.cs-TMSSS.PIT.Modules.Tempo.Views");
        var addToBasketView = new AddToBasketView();
        var viewModel = ViewModel as IFigureDetailViewModel;
        if (viewModel != null)
        {
            addToBasketView.LoadSelectedPart(viewModel.Asset, viewModel.FigureId, figureItem, viewModel.EventAggregator);
        }

        addToBasketView.WindowStartupLocation = WindowStartupLocation.CenterScreen;

        if (addToBasketView.ShowDialog() != true)
        {
        }

        LoggingManager.Debug("Exited from  AddToBasketClicked of FigureDetails.xaml.cs-TMSSS.PIT.Modules.Tempo.Views");
    }       

    public bool IsFrontView
    {
        get { return true; }
        set { }
    }

    public IViewModel ViewModel
    {
        get { return DataContext as IViewModel; }
        set { DataContext = value; }
    }
 }

1 个答案:

答案 0 :(得分:0)

正如@argaz的评论中提到的,可能是你创建了两个FigureDetailsView实例,因此有两个订阅和两个对话框(最简单的方法是检查你的日志)。

从一般的理解,允许创建两个FigureDeailsView实例是可以的,在这种情况下,你的'AddToBasket'订阅不应该在这个类中。它应该在单个实例窗口中,如'Shell'。如果项目中没有任何特定内容,那么'FigureDetailsView'实际上不应该有代码来显示“AddToBasket”对话框。

希望有所帮助。