How to access listview in MainWindow.xaml.cs from NServiceBus subscriber handler class

强颜欢笑 提交于 2019-12-12 05:48:50

问题


I would like to post my NServiceBus subscripiton messages derived from an EventHandler class to a ListView. The ListView is located inside the MainWindow.xaml of the WPF application.

Here is my NServiceBus subscription event handler code. Note: I would like to post the event message to the ListView control in MainWindow.xaml. Any ideas?

namespace EventPublisher.SubscriberDemoWPF
{
   public class PublishTrackEventHandler : IHandleMessages<PublishTrackEvent>
   {
      public void Handle(PublishTrackEvent message)
      {
         Trace.TraceInformation(message.GetType().Name);

         //Need to post event message to ListView control in MainWindow.xaml UI;
      }
   }
}

Here is my MainWindow.xaml code, which is in the same namespace as my event handler code:

<Window x:Class="EventPublisher.SubscriberDemoWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Height="260" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lstEvents" VerticalAlignment="Top" Width="479" />
    </Grid>
</Window>

Here is the MainWindow.xaml.cs code (typical):

namespace EventPublisher.SubscriberDemoWPF
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      //Would normally use listview.items.add("messages"); 
   }
}

回答1:


From your NSB message handler you could fire an event that has been attached to from the Window. Depending on how you are managing threads, be aware of updating UI elements from threads other than the UI thread. Check out this article in MSDN for events in WPF.



来源:https://stackoverflow.com/questions/9550902/how-to-access-listview-in-mainwindow-xaml-cs-from-nservicebus-subscriber-handler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!