keeping all UI logic out of ViewModel? - view

I am quite new to MVVM and i'm curious as to of it's possible to keep all UI specific things out of the ViewModel and therefore making my viewmodel portable across projects? For example, say i want to later port my Application to windows 8 from windows phone 8. I could easily drag over my viewmodel and model, and then design a new view which connects to the viewmodel. If so what are some practices i should be aware of in order to make my viewmodel as portable and as possible?

Rather than dragging your model and VM around, you could build them into assemblies of their own and just re-use the assembly/ies in multiple projects.
It's entirely possible to separate your VM from your view by implementing IPropertyNotifyChanged. This way your VM's properties can check if they're being changed and can notify subscribers to the PropertyChanged event that something's changed. This way, your view can subscribe to the PropertyChanged event on your object and can take action when it needs to update the UI to reflect the changed value of one or more properties of your object.
Be sure to work through the sample at the end of the IPropertyNotifyChanged docs which illustrates how this works.

Related

EventHandlerBehavior vs EventToCommandBehavior

In Xamarin Forms what is the difference between the EventHandlerBehavior and EventToCommandBehavior? I have been using the former successfully, but the latter was suggested to me to use instead. EventToCommandBehavior is less verbose which is a good thing.
About difference between EventHandlerBehavior and EventToCommandBehavior, I have some point, you can take a look:
For EventHandlerBehavior, you need to install Behaviors.Forms in your project firstly, behaviors let you add functionality to controls without having to subclass them. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself. Behaviors enable you to implement code that you would normally have to write as code-behind, because it directly interacts with the API of the control in such a way that it can be concisely attached to the control and packaged for reuse across more than one app.
For EventToCommandBehavior,the EventToCommandBehavior is a custom class, there are need you create EventToCommandBehavior class derives from the BehaviorBase class firstly, then implementing Bindable Properties, for detailed info, you can take a look:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/reusable/event-to-command-behavior
So I suggest you can use EventHandlerBehavior, because you don't need to create many code behind.
2021 Edit
EventHandlerBehavior from community-toolkit is now equivalent to EventHandlerBehavior.
Documentation:
https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/eventtocommandbehavior
In Xamarin Forms what is the difference between the EventHandlerBehavior and EventToCommandBehavior?
Eventhandler like:
OnTextChanged, OnSelectedIndexChanged etc are defined behind the C# code file of XAML. These methods are completely tied to your controls you define behind your pages/controls.
However the Behaviors in Xamarin allows us to ATTACH them to any View. Your behavior will know which controls it is being attached to. Hence, for those Events like OnTextChanged/OnSelectedIndexChanged can be controlled from the Behaviors.
EventToCommandBehaivor: As we already know that Behavior get attached to any view; you have to define the behavior as such that It will expose the BINDABLE property for Commands. EventToCommandBehavior is just a normal behavior control which supports binding the Command and the Eventname. Which in runtime, whenever the Event for the control get fired, your behavior will execute the Command attached making it more ViewModel friendly.
Remember if you use Events; you are writing them behind the code base than ViewModel your logics are in two separate places. If you want to properly separate the logics from View and Model then then you have to use EventToCommandBehavior.
Let me know if you need more info.

How to separate the application bar buttons for each PanaramaItem in the Windowsphone?

I have totally two PanaramaItems in my PanaramaPage of my windows phone application. I need to show two ApplicationBarItems in the first Panrama item and 3 Application Bar items in the second PanaramaItem.. Is there any to do this Xaml page... I don't want to do anything through code behind... Can any one help me ?
I'm pretty sure in this case you'll have to programmatically manipulate the toolbar for each PanoramaItem. If you don't want to do it in code behind and are using a mvvm framework there are ways to do it without coupling the ViewModel to the View. You could create a service that both items could access to manipulate the toolbar. In Caliburn Micro each VM has a reference to the View and manipulation could be done that way. Further in CM an IResult can be created and fired off in the OnActivate for each item.

Binding UserControl unload event to ViewModel

I'm reasonable new to MVVM and have a usercontrol as my View.
I'd like to Bind the "unload" event of that control to a method in my ViewModel.
To clean up stuff when the control is shutting down.
My search so far has only come up with xamples that require you to include some kind of toolkit or libraries from expression blend SDK. I just want to use he generic .net 4.1 libraries.
Can some one show me how to accomplish this ?
I've always used the code shown here to create an AttachedCommandBehavior. It doesn't require any extra libraries and can be used like this:
<local:MyControl
local:CommandBehavior.Event="Unloaded"
local:CommandBehavior.Command="{Binding ViewModelUnloadedCommand}" />

Best practices of building composite custom controls WPF

Can You tell me about subj?
For example I need to create new custom control that must be derived from datagrid and toolbar. And I want that new control to expose/propagate properties of base controls in order they to be accessed easily. The only way I know is deriving a class. Then descendant automatically gets all properties of ancestor. But multiple deriving in C# is prohibited, so I don't know how to expose properties and other behavior of second control... Styling and templating of such custom control is also needed.
Thanks!
In WPF inheritance is "out". ;O) Actually, it was complicated before WPF already, but with WPF you get various really powerful alternatives.
For the basic control I thing you'd better go with composition, create some usercontrol and make it contain a ToolBar and a DataGrid. You can then expose these as public properties, if you need to manipulate them from outside.
For special feature additions, attached properties are a very versatile mechanism.
Watching a window from windowsclient.net is a good place to start.
Make sure you understand dependency properties well.
Reading wpf blogs is my best guest for what to do then.

Altering windows form from userControl

i'm quite new with windows forms and i have a small issue.
i have a form that contains a userControl.
the form also contains a button with enabled = false, and upon some user selection in the userControl sets the button to enabled = true.
basically, i want to know what's the best way to change something in the form upon a change in the userControl.
I saw on the internet that event/delegates might be the answer, but it seems too complicated for such a small thing.
anyone has another solution?
thanks
Bosco
Events are your friends. They're really not that complicated. Just find an apropriate event for your user selection and set the button property.
User controls are meant to be hosted by different forms or other user controls. That's why they should be decoupled from their host controls.
IMHO best way to decouple the user control from its parent is to use events. Another way would be to implement the observer pattern. Events are a special implementation of observer pattern in .NET.

Resources