In WinRT MVVM Using Prism how do I listen for a SetProperty notification? - windows-runtime

I am using Prism for Windows Runtime to help with my MVVM implementation. It has a built in SetProperty command for properties. How do I listen for a property change from within the View Model to make something else happen?

You can handle INotifyPropertyChanged.PropertyChanged event and check the name of the property. If you want to avoid string comparisons - you can define a new event and raise it for specific property. It can either be a regular CLR event or an EventAggregator one if you want to broadcast it to any interested listeners in the app.

Related

Extending the native GamepadAPI?

I'm trying to extend the native GamepadAPI to include custom controller code.
Using TypeScript, I implemented a simple function, to dispatch a "gamepadconnected" event.
// simulate gamepadconnected event
function dispatchGamepadConnectedEvent() {
let gamepad = Object.create(Gamepad.prototype);
console.log(gamepad);
let event = new GamepadEvent('gamepadconnected', {
gamepad: gamepad
})
window.dispatchEvent(event);
console.log('Gamepad connect event dispatched.');
}
However, when dispatching the event, I get an error:
GamepadĀ {}axes: (...)buttons: (...)connected: (...)id: (...)index: (...)mapping: (...)timestamp: (...)vibrationActuator: (...)__proto__: Gamepad
extension.ts:37 Uncaught TypeError: Failed to construct 'GamepadEvent': member gamepad is not of type Gamepad.
at dispatchGamepadConnectedEvent (extension.ts:37)
at extension.ts:48
Even though, the instantiated Gamepad object seems fine, the type of the Gamepad is not correct.
Why is this like that? How can I create a new, proper Gamepad object to fire the native event?
You cannot create a native Gamepad object.
Object.create(Gamepad.prototype) does not create a Gamepad. It creates an object and sets it prototype to Gamepad.prototype. While this trickery often works for application-level APIs, it won't fool native browser APIs.
Your choices are:
Pass null: {gamepad: null}
Pass an actual gamepad reference {gamepad: navigator.getGamepads()[0]}
Dispatch a CustomEvent, not a GamepadEvent.
Try something else entirely.
Not sure about the use case that's behind your question, but one alternative might be to use the WebHID API. I have recently used this API to create a Nintendo Joy-Con driver in JavaScript. If there happen to be reverse engineering efforts for your device in question (hint: check if there is a Linux driver), chances are that you can use this to talk to your device over WebHID.

Get MVVMCross Binding from TextView

From the Click event of a TextView\EditText in Xamarin Android I want to get at MVVMCross's Binding instance for that control.
I guess it would be the equivalent of GetBindingExpression in Windows Store SDK
How do I do that?
in MvvmCross, the bindings are stored in the binding context - not in the individual control. This is because the controls aren't natively Data-Binding aware.
The default context doesn't expose these bindings - see https://github.com/MvvmCross/MvvmCross/blob/bbf9a2ac76e74d9404f4b57036c6e29dfe2cc6c3/Cirrious/Cirrious.MvvmCross.Binding/BindingContext/MvxBindingContext.cs - but you could replace this with your own IMvxBindingContext if you needed to - this implementation could expose "search by control" or other iterator functionality.

MVVM - Share encapsulated model with other VMs

In my Windows Phone App there's a simple hierarchical model consisting of a class containing a collection of other domain objects.
In my xaml i have declared an ItemsContainer control that renders the items in the above mentioned collection as simple rectangles.
Now, at the VM level i have a structure that resembles my model with a parent VM having a collection of children VMs. Each child-VM encapsulates its own model.
Whenever the user taps the view bound to a child-VM a method of the parent-model object should be invoked taking the relevant child-model as parameter. This will in turn change some internal state that will be reflected (possibly) on all the child-views (not just the tapped one).
SO... given that i'm using the MVVM Light framework my current implementation is as follows:
Child-VM exposes a command
The command Execute method will use the messenger to notify the parent-VM of the tap event. The message (GenericMessage class) content will be the domain object encapsulated by the VM
The parent-VM executes the method of the parent-model using the message content as parameter
If the operation succeeds the parent-VM sends a new message to inform child-VMs of this fact. Once again the message content is the model object used as parameter in the method that was just invoked
Child-VMs raise a couple of PropertyChanged events that, finally, will update the bound views
It works but i fill it's a bit cumbersome. The thing that bugs me the most is the fact that when a child-view is tapped the associated VM will broadcast its encapsulated model object. Do you feel that there would be a better way of implementing such a system?
Thanks in advance for your precious help
Could you not just put the command on the parent viewmodel and pass the child viewmodel as the command parameter?
The parent view model can then just call methods on the child viewmodels to update them. I'm not sure I see the need for all these messages?

How do I attach a global event listener?

I am working on an AIR application:
The main window is like a dashboard. With the menu bar, I can open other windows with dashboard details. When I close these, I'd like to refresh the main window.
I tried to use an event listener, but the result is not good. If I open detail windows directly from the main window, I know how to add an event listener - and it works - but I don't know how to do it, if the detail window is opening from the menubar!
Thanks for helping me.
A Singleton is what you are looking for. Just put an event dispatcher inside and you will be able to listen from everywhere in the application.
A Singleton is like having a unique instance of an object in memory, so anyone modifying a variable inside that object ( or sending events throught ) will be modified for everyone.
Here is an example of code on how to use it.
http://life.neophi.com/danielr/2006/10/singleton_pattern_in_as3.html
Note: Singletons are powerful and dangerous at the same time, there is a lot of talk about how to use them, please read a little more about that if you are considering building a big project.
Hope it helps!
The issue is that you're performing business logic from a View. Don't do this. Instead, dispatch an event from each menu rather than directly opening the window from within it. Listen for those events at a higher level, and then you can either directly listen to the new windows you have opened, or you can create a base window Class that exposes a variable of type IEventDispatcher. If you populate that variable with the same event dispatcher, what you wind up with is called an "event bus," and you can listen on that for events.
This architecture requires a little more thought than using a Singleton, but it avoids the tight coupling and other issues you'll run into by introducing one into your project.
You can listen to an object (EventDispatcher) directly by adding an event listener to it, or if the dispatcher object is on the displaylist, such as a Sprite, you could listen at the stage level with the capture parameter set to true.
But the main caveat is that the dispatcher must be on stage for you to catch this event.
Your main window listens to stage (with capture = true):
stage.addEventListener("MY_CUSTOM_EVENT", handle_custom_event, true);
private function handle_custom_event(e:Event):void
{
var sub_window:Object = e.target;
// do something to your sub_window
}
Your sub window can dispatch events like this:
dispatchEvent(new Event("MY_CUSTOM_EVENT"));
But (ab)using the stage as a message passing infrastructure for custom events in this way is a little messy. You could consider a more formal message passing architecture if you really want this kind of communication. Even a static MessageBus class would at least quickly help you identify where you use this in your codebase. Either way, you'll have to be careful about references and memory leaks.

Windsor Castle: Hooking up to container's resolving and releasing mechanism

I'm trying to implement automatic registration of my listeners to a singleton event aggregator when listeners are created by the IoC container - basically what Jeremy D. Miller is doing, but with Castle instead of StructureMap.
So I want to be able to "intercept" Windsor's object creation mechanism and, if the object supports the marker interface (let's say IListener), call the Subscribe method to an EventAggregator (which is also registered in the container) to make the newly created object a subscriber to events. Also, before the object instance has been released by the container, I want to be able to unsubscribe it.
I'm a little bit confused about what mechanism in Windsor Castle I should use to achieve something like this? I started looking at IInterceptor interface, but it seems to intercept all calls to the object, which is not what I really need (and want to avoid for performance reasons).
IKernel exposes various events like ComponentCreated and ComponentDestroyed which you can use to build that. There are many samples on the web.
Otherwise you could just use the event wiring facility, but it's not convention based.
You could also use OnCreate like this:
container.Register(
Component.For(typeof (Foo)).OnCreate(
(k, c) => {
// ...
eventAggregator.Subscribe(c);
// ...
}));