Guest User

Untitled

a guest
Jun 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. public class ApplicationViewModel : INotifyPropertyChanged
  2. {
  3. public Uri Address{ get; set; }
  4. public void ConnectTo( Uri address )
  5. {
  6. // Connect to the address
  7. // Save the addres in persistent storage for later re-use
  8. Address = address;
  9. }
  10.  
  11. ...
  12. }
  13.  
  14. public class ConnectionViewModel : INotifyPropertyChanged
  15. {
  16. private ApplicationViewModel _appModel;
  17. public ConnectionViewModel( ApplicationViewModel model )
  18. {
  19. _appModel = model;
  20. }
  21.  
  22. public ICommand ConnectCmd
  23. {
  24. get
  25. {
  26. if( _connectCmd == null )
  27. {
  28. _connectCmd = new LambdaCommand(
  29. p => _appModel.ConnectTo( Address ),
  30. p => Address != null
  31. );
  32. }
  33. return _connectCmd;
  34. }
  35. }
  36.  
  37. public Uri Address{ get; set; }
  38.  
  39. ...
  40. }
  41.  
  42. public interface IApplication
  43. {
  44. Uri Address{ get; set; }
  45. void ConnectTo(Uri address);
  46. }
  47.  
  48. public class App : Application, IApplication
  49. {
  50. // code removed for brevity
  51. }
  52.  
  53. public class ConnectionViewModel : INotifyPropertyChanged
  54. {
  55. public ConnectionViewModel(IApplication application)
  56. {
  57. //...
  58. }
  59.  
  60. //...
  61. }
  62.  
  63. public class ServiceProvider
  64. {
  65. public void Connect(Uri address)
  66. {
  67. //connect to the server
  68. }
  69. }
  70.  
  71. public class SettingsProvider
  72. {
  73. public void SaveAddress(Uri address)
  74. {
  75. //Persist address
  76. }
  77.  
  78. public Uri LoadAddress()
  79. {
  80. //Get address from storage
  81. }
  82. }
  83.  
  84. public class ConnectionViewModel
  85. {
  86. private ServiceProvider serviceProvider;
  87.  
  88. public ConnectionViewModel(ServiceProvider provider)
  89. {
  90. this.serviceProvider = serviceProvider;
  91. }
  92.  
  93. public void ExecuteConnectCommand()
  94. {
  95. serviceProvider.Connect(Address);
  96. }
  97. }
  98.  
  99. public class ServiceProvider
  100. {
  101. public event EventHandler<ConnectedEventArgs> Connected;
  102. public void Connect(Uri address)
  103. {
  104. //connect to the server
  105. if (Connected != null)
  106. {
  107. Connected(this, new ConnectedEventArgs(address));
  108. }
  109. }
  110. }
  111.  
  112. public class SettingsProvider
  113. {
  114.  
  115. public SettingsProvider(ServiceProvider serviceProvider)
  116. {
  117. serviceProvider.Connected += serviceProvider_Connected;
  118. }
  119.  
  120. protected virtual void serviceProvider_Connected(object sender, ConnectedEventArgs e)
  121. {
  122. SaveAddress(e.Address);
  123. }
  124.  
  125. public void SaveAddress(Uri address)
  126. {
  127. //Persist address
  128. }
  129.  
  130. public Uri LoadAddress()
  131. {
  132. //Get address from storage
  133. }
  134. }
  135.  
  136. public class ServiceProvider : IServiceProvider
  137. {
  138. ...
  139. }
  140.  
  141. public class ConnectionViewModel
  142. {
  143. private IServiceProvider serviceProvider;
  144.  
  145. public ConnectionViewModel(IServiceProvider provider)
  146. {
  147.  
  148.  
  149.  
  150.  
  151. link|improve this answer
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. answered Apr 29 '09 at 17:00Martin Harris12.3k11843
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. feedback
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. up vote
  198. 2
  199. down vote
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. 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.
  208.  
  209. 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.
  210.  
  211. 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.
  212.  
  213. 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:
  214.  
  215. public interface IApplicationStateConsumer
  216. {
  217. public void ConsumeApplicationState(ApplicationState appState);
  218. }
  219.  
  220. public interface IApplicationStateConsumer
  221. {
  222. public void ConsumeApplicationState(ApplicationState appState);
  223. }
Add Comment
Please, Sign In to add comment