Laden...

MVVM Prism EventAggregator

Letzter Beitrag vor 9 Monaten 2 Posts 527 Views
MVVM Prism EventAggregator

Hallo

Ich habe eine View wo nach Daten gesucht wird. Aus der View soll am anfang eine ander View geöffnet (nur ein mal) werden und über

_eventAggregator.GetEvent<DataUpdatedEvent>().Publish(value); eine Daten Klasse übermittelt werden.

Wenn andere Daten gesucht werden, soll dann nur der _eventAggregator.GetEvent<DataUpdatedEvent>().Publish(value);

gestartet werden und denn Datensatz an das Datagrid übermittelt.

Leider klappte es nicht. Was mache ich Falsch ? Oder wie kann man das besser machen ?

Danke für die Hilfe schon mal.

public partial class RemiSearchView : Window
    {
        private IEventAggregator eventAggregator;
        RemiSearchViewModel remiSearchViewModel = new RemiSearchViewModel();
        public RemiSearchView(string user)
        {
            InitializeComponent();

            this.eventAggregator = new EventAggregator();
            this.DataContext = new RemiSearchViewModel(eventAggregator);
            remiSearchViewModel.InitializeData(user);
        }
    }

RemiSearchViewModel:

private readonly IEventAggregator _eventAggregator;
public RemiSearchViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            this.ScanClickCommand = new DelegateCommand
            (
                (o) => Shipment != null,
                (o) => { ScanCommand(o); }
            );
            this.NameClickCommand = new DelegateCommand
            (
                (o) => Shipment != null,
                (o) => { NameCommand(o); }
            );
        }  

Denn nächsten schritt lass ich aus.

private void OpenViewRemi(HouseShipmentResults value)
        {
            if(count == 0)
            {
                DifferentMarketplacesView differentMarketplacesView = new DifferentMarketplacesView();
                differentMarketplacesView.Show();

                _eventAggregator.GetEvent<DataUpdatedEvent>().Publish(value);

                count++;
            }
            else
            {
                _eventAggregator.GetEvent<DataUpdatedEvent>().Publish(value);
            }
        }  
              
 public partial class DifferentMarketplacesView : Window
    {
        private IEventAggregator eventAggregator;

        public DifferentMarketplacesView()
        {
            InitializeComponent();

            this.eventAggregator = new EventAggregator();
            this.DataContext = new DifferentMarketplacesViewModel(eventAggregator);
        }
    } 
    
public DifferentMarketplacesViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            _eventAggregator.GetEvent<DataUpdatedEvent>().Subscribe(OnDataUpdated);

            this.MenuClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { MenuCommand(); }
            );
            this.PrintClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { PrintCommand(); }
            );
            this.ArticleClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { ArticleCommand(o); }
            );
            this.FocusClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { FocusCommand(o); }
            );

            articleInfo = mentionRepository.GetArticleInfo();

            TextBoxOrderFocus = true;
        }

OnDataUpdated wird nicht aufgerufen.

private void OnDataUpdated(HouseShipmentResults updatedData)
        {
            User = updatedData.Worker;

            ShipmentReturn.Add(updatedData);

            countToPrint = (int)ShipmentReturn.Sum(x => Convert.ToInt16(x.CustomArticleCount));
        }

Hallo

Ich hatte Prism mehrmal initialisiert. Deswegen ging es nicht.

RemiSearchViewModel:

private readonly IEventAggregator _eventAggregator;
public RemiSearchViewModel()
        {
            this._eventAggregator = new EventAggregator();

            this.ScanClickCommand = new DelegateCommand
            (
                (o) => Shipment != null,
                (o) => { ScanCommand(o); }
            );
            this.NameClickCommand = new DelegateCommand
            (
                (o) => Shipment != null,
                (o) => { NameCommand(o); }
            );
        }
        
public partial class DifferentMarketplacesView : Window
    {
        private IEventAggregator eventAggregator;

        public DifferentMarketplacesView(IEventAggregator eventAggregator)
        {
            InitializeComponent();

            this.DataContext = new DifferentMarketplacesViewModel(eventAggregator);
        }
    }   
    
public DifferentMarketplacesViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;

            _eventAggregator.GetEvent<DataUpdatedEvent>().Subscribe(OnDataUpdated);

            this.MenuClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { MenuCommand(); }
            );
            this.PrintClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { PrintCommand(); }
            );
            this.ArticleClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { ArticleCommand(o); }
            );
            this.FocusClickCommand = new DelegateCommand
            (
                (o) => ShipmentReturn != null,
                (o) => { FocusCommand(o); }

            );

            articleInfo = mentionRepository.GetArticleInfo();

            TextBoxOrderFocus = true;
        }
        private void OnDataUpdated(HouseShipmentResults updatedData)
        {
            User = updatedData.Worker;

            ShipmentReturn.Add(updatedData);

            countToPrint = (int)ShipmentReturn.Sum(x => Convert.ToInt16(x.CustomArticleCount));
        }