Broadcast Applicationwide Event

Apr 23, 2012 at 4:46 PM
Edited Apr 23, 2012 at 4:51 PM


In in WPF application, I have multiple views with BingMaps. In case user doesnt have an Internet connection, I hide the mapping control. I am using NetworkInterface initially in shellview model to determine if Internet connection is available or not. I use it to populate my custom INetworkConnectionAvailabilityDataProvider interface. I create a static instance of this interface and insert this as a dependency in all the user controls with BingMap. However I also use NetworkChange.NetworkAvailabilityChanged event to determine if network is available while user is using the application. In the event, I set my INetworkConnectionAvailabilityDataProvider's boolean property to network availability. This way when my other viewmodels get the static instance of this interface, they can determine whether to show/hide map. Is this arrangement good enough for determining network availability in all view models? 

I need all the viewmodels which have BingMaps to be notified when network availability changes so that I can hide/show maps. Should I broadcast an event in shellViewModel's NetworkChange_NetworkAvailabilityChanged method and all view models with map subscribe to it?

Or I will have to hook the NetworkChange.NetworkAvailabilityChanged event in all my view models with map?

Here is the Shell view model constructor where I am passing my custom interface as a dependency and also hooking the NetworkChange.NetworkAvailabilityChanged event.






 public ShellViewModel(IWindowManager windowManager, IEventAggregator events, 
            DashboardViewModel dashboardViewModel, 
            PlanSummaryViewModel planSummaryViewModel,
            MappingViewModel mappingViewModel,
            IContainer container,
            IAccomplishmentDataProvider accomplishmentDataProvider,
            INetworkConnectionAvailabilityDataProvider networkConnectionAvailabilityDataProvider
            Dashboard = dashboardViewModel;
            Mapping = mappingViewModel;
            PlanSummary = planSummaryViewModel;
            Events = events;
            this.Container = container;
            this.windowManager = windowManager;
            this.NetworkConnectionAvailabilityDataProvider = networkConnectionAvailabilityDataProvider;
            this.NetworkConnectionAvailabilityDataProvider.IsNetworkConnectionAvailable = NetworkInterface.GetIsNetworkAvailable();
            NetworkChange.NetworkAvailabilityChanged+=new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);

  private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs args)
            this.NetworkConnectionAvailabilityDataProvider.IsNetworkConnectionAvailable = args.IsAvailable;
Apr 24, 2012 at 7:47 AM

Sounds like a perfect possibility to use the EventAggregator:


Apr 25, 2012 at 10:15 PM

Thanks! I am using EventAggregator to publish event whenever network is available from ShellViewModel and then all other view models with map subscribe to that event.