[英]WPF Event Method is executing twice
我正在使用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; }
}
}
如@argaz的注釋中所述,可能是您正在創建FigureDetailsView
兩個實例,因此將創建兩個訂閱和兩個對話框(最簡單的方法是檢查日志)。
從一般的理解來看,可以創建兩個FigureDeailsView實例,並且在這種情況下,您的“ AddToBasket”訂閱不應存在於此類中。 它應該在單個實例窗口中,例如“ Shell”。 如果您的項目中沒有特定內容,則“ FigureDetailsView”實際上不應具有顯示“ AddToBasket”對話框的代碼。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.