如何从 NServiceBus 订阅者处理程序类访问 MainWindow.xaml.cs 中的列表视图 [英] How to access listview in MainWindow.xaml.cs from NServiceBus subscriber handler class

查看:58
本文介绍了如何从 NServiceBus 订阅者处理程序类访问 MainWindow.xaml.cs 中的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将从 EventHandler 类派生的 NServiceBus 订阅消息发布到 ListView.ListView 位于 WPF 应用程序的 MainWindow.xaml 内.

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.

这是我的 NServiceBus 订阅事件处理程序代码.注意:我想将事件消息发布到 MainWindow.xaml 中的 ListView 控件.有什么想法吗?

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

这是我的 MainWindow.xaml 代码,它与我的事件处理程序代码在同一个命名空间中:

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>

这是 MainWindow.xaml.cs 代码(典型):

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"); 
   }
}

推荐答案

从您的 NSB 消息处理程序中,您可以触发已从 Window 附加到的事件.根据您管理线程的方式,请注意从 UI 线程以外的线程更新 UI 元素.在 MSDN 中查看这篇文章,了解 WPF 中的事件.

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.

这篇关于如何从 NServiceBus 订阅者处理程序类访问 MainWindow.xaml.cs 中的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆