Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApplicationViewModel : INotifyPropertyChanged
- {
- public Uri Address{ get; set; }
- public void ConnectTo( Uri address )
- {
- // Connect to the address
- // Save the addres in persistent storage for later re-use
- Address = address;
- }
- ...
- }
- public class ConnectionViewModel : INotifyPropertyChanged
- {
- private ApplicationViewModel _appModel;
- public ConnectionViewModel( ApplicationViewModel model )
- {
- _appModel = model;
- }
- public ICommand ConnectCmd
- {
- get
- {
- if( _connectCmd == null )
- {
- _connectCmd = new LambdaCommand(
- p => _appModel.ConnectTo( Address ),
- p => Address != null
- );
- }
- return _connectCmd;
- }
- }
- public Uri Address{ get; set; }
- ...
- }
- public interface IApplication
- {
- Uri Address{ get; set; }
- void ConnectTo(Uri address);
- }
- public class App : Application, IApplication
- {
- // code removed for brevity
- }
- public class ConnectionViewModel : INotifyPropertyChanged
- {
- public ConnectionViewModel(IApplication application)
- {
- //...
- }
- //...
- }
- public class ServiceProvider
- {
- public void Connect(Uri address)
- {
- //connect to the server
- }
- }
- public class SettingsProvider
- {
- public void SaveAddress(Uri address)
- {
- //Persist address
- }
- public Uri LoadAddress()
- {
- //Get address from storage
- }
- }
- public class ConnectionViewModel
- {
- private ServiceProvider serviceProvider;
- public ConnectionViewModel(ServiceProvider provider)
- {
- this.serviceProvider = serviceProvider;
- }
- public void ExecuteConnectCommand()
- {
- serviceProvider.Connect(Address);
- }
- }
- public class ServiceProvider
- {
- public event EventHandler<ConnectedEventArgs> Connected;
- public void Connect(Uri address)
- {
- //connect to the server
- if (Connected != null)
- {
- Connected(this, new ConnectedEventArgs(address));
- }
- }
- }
- public class SettingsProvider
- {
- public SettingsProvider(ServiceProvider serviceProvider)
- {
- serviceProvider.Connected += serviceProvider_Connected;
- }
- protected virtual void serviceProvider_Connected(object sender, ConnectedEventArgs e)
- {
- SaveAddress(e.Address);
- }
- public void SaveAddress(Uri address)
- {
- //Persist address
- }
- public Uri LoadAddress()
- {
- //Get address from storage
- }
- }
- public class ServiceProvider : IServiceProvider
- {
- ...
- }
- public class ConnectionViewModel
- {
- private IServiceProvider serviceProvider;
- public ConnectionViewModel(IServiceProvider provider)
- {
- link|improve this answer
- answered Apr 29 '09 at 17:00Martin Harris12.3k11843
- feedback
- up vote
- 2
- down vote
- Yes, you are on the right track. When you have two controls in your system that need to communicate data, you want to do it in a way that is as decoupled as possible. There are several ways to do this.
- In Prism 2, they have an area that is kind of like a "data bus". One control might produce data with a key that is added to the bus, and any control that wants that data can register a callback when that data changes.
- Personally, I have implemented something I call "ApplicationState". It has the same purpose. It implements INotifyPropertyChanged, and anyone in the system can write to the specific properties or subscribe for change events. It is less generic than the Prism solution, but it works. This is pretty much what you created.
- But now, you have the problem of how to pass around the application state. The old school way to do this is to make it a Singleton. I am not a big fan of this. Instead, I have an interface defined as:
- public interface IApplicationStateConsumer
- {
- public void ConsumeApplicationState(ApplicationState appState);
- }
- public interface IApplicationStateConsumer
- {
- public void ConsumeApplicationState(ApplicationState appState);
- }
Add Comment
Please, Sign In to add comment