简体   繁体   中英

WPF Event Method is executing twice

I am using wpf event to show popup window. As my event method 'AddToBasketClicked' is executing twice, the popup window is loaded two times. After popup window is opened first time, after performing operations and closing the window, the window is loaded again after executing the event method 'AddToBasketClicked' again.

 [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; }
    }
 }

As mentioned in the comments by @argaz, the possibility is that you are creating two instances of FigureDetailsView , hence two subscriptions and two dialogs (easiest way would be to check your log).

From general understanding, it's okay to allow creating two instances of FigureDeailsView and in that case your 'AddToBasket' subscription shouldn't be there in this class. It should rather be in a single instance window, like 'Shell'. If nothing specific in your project, then 'FigureDetailsView' shouldn't actually have he code to show "AddToBasket" dialog.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM