I have such usecase:
In my application some instances need to fire event, that they have been changes. But, I dont want to keep references to instances that need to be changed, so I would like to send event and somehow listen it in some places, but how to do it in C# in windows phone?
You're looking for the EventAggregator. There are a few toolkits that you can build upon like GalaSoft's MVVM Light and Caliburn Micro. You can find a good example of this at http://www.mindscapehq.com/blog/index.php/2012/02/01/caliburn-micro-part-4-the-event-aggregator/.
The basic gist of it is to inject an EventAggregator into your ViewModel. This ViewModel would Publish an event. Other ViewModels would subscribe to this event.
Related
I have a Xamarin app. (Only) for the UWP app I need access to the mouse events (PointerMoved- / PointerEntered / PointerPressed-Events).
Until now my entire codebase is shared code.
Can somebody help me with forwarding the mouse events to the shared Code base? I googled a lot and nearly all answers either suggest a paid libary called "MR.Gestures" or say that it is not possible to recieve mouse Events (like this one I am not able to implement MouseUp, MouseDown and mouseMove Events in Xamarin Mobile applications). Sometimes GestureRecogniser are mentioned, but they Arent extensible, so how are they supposed to help?
Is there really no way to recieve mouse Events in xamarin?
You can do this. However it is a very extensive work to the level that you might consider to write your app in the native UWP XAML and share some C# code with the Xamarin project instead.
The way would be to extend each control that you are going to use (yes every single control that needs this effect), add to it those events and then write the UWP custom renderers that would invoke those events and then replace all controls in your XAML with those controls.
As said this may turn to be quite extensive work, but that's how it can be done.
Also if you don't need those events but just some visual effects on those events it may be possible to cut on this work by writing a custom XAML styles for those controls in app.xaml in the UWP project.
I currently have a viewmodel such that it has a grid full of appointments. I would like to double click and open up my CalendarView with the editappointmentdialog opened for the record that was selected. May I ask how would I do that in an MVVM style?
I searched the internet and found this RadScheduleViewCommands.EditAppointment.Execute(appointment, this.scheduleView); but I don't have access to the scheduleView object from the MVVM. May I ask how abouts I should do this?
I think I can achieve this if I relay it back to the view, but I'm trying to look for another approach.
The issue you have is that you can't open a view from a view model as it'll break the MVVM design pattern.
There are a couple of ways you can open a view from the view model without too much hassle:
Implement a Messenger service to publish an event to the view, like MVVMLight does.
Straight up add a Click event handler on your button and open the view in the code behind (It's still view code after all).
However my preferred method is to use an ICommand, or RelayCommand. I have written a repository on GitHub which demonstrates how to achieve this.
Currently I have to develop a very simple WPF user control that allows the user to select several points on a canvas. The difficulty that I encounter, is that uses with a touch screen should be able to so by triggering a TouchDown event whereas users that don't have touch screen should use the mouse and thus trigger a MouseLeftButtonDown event. Is there a simple way to handle these two cases without duplicating code? Also, I need to use Mvvm Light, so code-behind solutions like How to get Touchscreen to use Mouse Events instead of Touch Events in C# app won't do the trick.
Your linked question provides an answer for you, whether you are using MVVM or not. Using MVVM does not mean that you cannot handle UI control events. It just means that you should write an Attached Property to handle them for you. So, your answer is yes, you can handle the two events together and in almost the same way as your linked page suggests.
Your only difference is that the handler must be attached to the events in an Attached Property. Rather than go over the whole story once again here, I'll just briefly explain the procedure and request that you view my answer from the What's the best way to pass event to ViewModel? question for a code example.
First declare your Attached Property with its getter and setter and ensure that it has a PropertyChangedCallback handler attached. The PropertyChangedCallback handler is where you attach your single handler to the events (the code example just attaches a single event). This just means that it will only attach the handler to the events when your Attached Property is set. Finally, just add your single handler to handle the two events.
I have a controller that needs to capture some events(MouseMove, MouseDown, KeyDown, ...) raised by a control(this control could be WinForms, WPF or Silverlight).
Is there a way to implement such a general controller?
*added by ChrisBD *
Background:
I have a MVC application and I have a controller(GeneralController) that has acces to IGeneralControl. This IGeneralControl will be implemented by a WPF, WF and Silverlight specific control(WFControl, WPFControl, SilverlightControl). What I want to achieve is to be able to add my code to the delegates of the control at the level of the GeneralController, without the need to implemet WFController, WPFController and SilverlightController
I'm no forms expert but I'm skepical that you can do this with some works for all GeneralController. WPF/Silverlight use a very different event model, using staticly declared routed events.
I think you'd need to implement your WFController and a separate WPF/Silverlight controller.
Can anyone provide a short example of how to execute an event within the mvvm pattern for silverlight? In reading I have seen references to where silverlight does not support commanding? If that is the case how then what is the most common practice to initate methods in the mvvm pattern? Is a third party framework need or can this be accomplished using routed events? A simple example would be great... say a button wihtin the xaml and then within a viewmodel class the method to open an alert window of change the text of a textblock? I'd appreciate any insight as to what the most common approach is for initiating an event( mouse click events etc) and how these methods could be called.
thanks in advance
Though there isn't an implementation on Silverlight, the ICommand interface is present in the framework. You should take a look at the DelegateCommand class in Prism.