简体   繁体   中英

Composite WPF GUI Interface with Event

Using Prism4 and MEF I have created a shell and two modules(M1,M2).

I do want to open a serial port in M1 and by using an interface, with an datareceived event from the opened serial port, I want that M2 gets notified and receives the data from the serial port.

To be more specific, I use the MVVM pattern, therefore I would like to open the serial port within the M1's ViewModel, and inform the M2's ViewModel when data are received.

Unfortunately, I'm quite unsure how to use the interface within the PRISM workflow. I'm thankful for every help. I really need an example for this issue. I added the code just to make my question clear.

Thanks in advance.

Module A.cs

[ModuleExport(typeof(ModuleA), InitializationMode = InitializationMode.OnDemand)]
public class ModuleA : IModule
{
    [ImportingConstructor]
    public ModuleB(IEventAggregator eventAggregator_)
    {
        EventAggregator = eventAggregator_;

    }

    [Import]
    public IRegionManager RegionManager { get; set; }


    public void Initialize()
    {

        this.RegionManager.RegisterViewWithRegion("RegionA", typeof(ZeroGrid1));

    }
}

Module B.cs

[ModuleExport(typeof(ModuleB), InitializationMode = InitializationMode.OnDemand)]
public class ModuleB : IModule
{
    [ImportingConstructor]
    public ModuleB(IEventAggregator eventAggregator_)
    {
        EventAggregator = eventAggregator_;

    }

    [Import]
    public IRegionManager RegionManager { get; set; }


    public void Initialize()
    {

        this.RegionManager.RegisterViewWithRegion("RegionB", typeof(ZeroGrid2));

    }
}

ZeroGrid1.xaml.cs (similar to ZeroGrid.xaml.cs)

[Export]
public partial class ZeroGrid1
{
    [ImportingConstructor]
    public ZeroGrid1(ZeroGridViewModel1 viewModel)
    {
        InitializeComponent();
        this.DataContext = viewModel;
    }
}

ModuleAViewModel.cs

[Export]
public class ModuleAViewModel: NotificationObject, IDataReciever
{
// OPEN SERIALPORT
//SEND SOMETHING SERIALPORT
//Maybe I also wanna get notification for datareceived here
}

ModuleBViewModel.cs

[Export]
public class ModuleBViewModel: NotificationObject, IDataReciever
{
//GET NOTIFIED WHEN DATARECEIVED FROM SERIALPORT AND RECEIVED DATA
}

IDataReceiver.cs

interface IDataReciever<TData>
{
event Action<TData> DataRecieved;
//some other methods, such as, for example:
//void Open();
//void Close();
}

Define a composite presentation event, by exporting a classed which derives from Prism's 'CompositePresentationEvent' where T is the type of the event's 'payload'.

[Export]
public class DataReceivedEvent : CompositePresentationEvent<object>
{}

Make your two ViewModels import that event:

[Export]
public class ModuleAViewModel: NotificationObject, IDataReciever
{

    private DataReceivedEvent _dataReceivedEvent;

    [ImportingConstructor]
    public ModuleAViewModel(DataReceivedEvent dataReceivedEvent)
    {
        _dataReceivedEvent = dataReceivedEvent;
        _dataReceivedEvent.Subscribe(OnDataReceived);
    }

    private void OnDataReceived(object payload)
    {
        // Handle received data here
    }

    // This method gets called somewhere withing this class
    private void RaiseDataReceived(object payload)
    {
        _dataReceivedEvent.Publish(payload);
    }
}

Do the same in ViewModelB and both will get notified if the event is raised anywhere in the application.

There is a QuickStart solution available in MSDN which describes how publishing the event from one module and subscribing to it from the other is performed. You can find the Event Aggregation QuickStart in the following Prism Guide Appendix :

For more information of how EventAggregator works you can refer to the following Prism Guide chapter:

Regards.

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