How the parent View-Model will gain access to the child View-Model? - prism

<Page x:Class="ParentView">
<view:ChildView/>
<view:ChildView/>
...
</Page>
Prism attaching the child View-Models as AutowireViewModel=True. How can I gain access from ParentViewModel to the child View-Models ? (They are actually fabricated in one factory delegate that configured in the IoC container.)
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved);
method isn't available in Page control, which could have been invoked upon a UIElement is added ?

How can I gain access from ParentViewModel to the child View-Models ?
The easiest way is to go the other way round: make the parent view model create the child view models, expose them as a collection-valued property, bind that to an items control and map the items to views through data templates. Everything else is unnecessarily complex and more or less hacky.
Use AutowireViewModel=true where it makes sense (i.e. if you want to use Prism's navigation), not everywhere just because you can.

Related

What is the proper way to break views into components in Xamarin Forms? How to pass bindings and contexts?

I want to create resuable components, so naturally I want bindings to pass from parent custom view to child and child of child custom views.
To achieve this I don't use MVVM pattern for custom views, but rather use
BindingContext = this
in my custom views constructors.
Of course I would implement BindableProperty for each property I want but oh boy was I surprised by BindableProperty not working at all!
All because of my "BindingContext = this".
Basically each time I have to write my bindings by hands and not by
<!-- Label in child view -->
<Label x:Name="InnerLabel" Text={Binding LabelText} />
I suspect I'm deeply mistaken somewhere, and there is an easier way to break views into smaller hierarchy of parent-child dependent views, without writing huge amount of boilerplate code like
InnerLabel.SetBinding(Label.TextProperty, new Binding(... etc etc etc
How to write reusable views properly in Xamarin Forms? Why my BindableProperty stopped working with BindingContext = this in my custom views constructors?
Generally speaking, any Forms element will inherit it's parent's BindingContext unless otherwise specified. By assigning BindingContext = this; you are breaking that built in inheritance.
As #Jason properly mentioned in the comment section, the problem with
BindingContext = this
inside custom views constructors.
In chase for easy way wanting to use {Binding CustomViewProp} I was breaking chain of parent -> child context, making BindableProperty properties to receive nothing from parent contexts.
THE PROPER WAY TO USE CUSTOM VIEW PROPERTIES IN XAML, example:
<ContentView x:Name="Self" ... ... ... all other initializing parent element stuff
<Label Text="{Binding CustomViewProp, Source={x:Reference Self}}" />

Show specific presenter instance to flex panel gwt mvp

I'm still learning GWT, yet already have to face some kind of challenge for a work I have to do. Can't show any specific code so I'll try to explain it well.
Here's the situation: A certain class "Navigator" creates and save the Presenter instances of my architecture to allow reusing them. There is a method show() inside that same class that actually displays the view related but that system only works full screen by calling RootPanel.get().
What i'd like to do is showing that presenter instance's view inside of a flex panel element declared in a class myView (related to a class myPresenter) that basically uses Flex Panel to structure it's content.
To make it maybe more clear:
class myView{
...
flexPanel.setWidget(firstWIdget)
flexPanel.setWidget(secondWidget) //secondWidget to be replaced by a "thirdWidget"
...
}
I'd like the secondWidget to be replaced by another one, let's call it thirdWidget, that consists of a specific presenter instance's view.
To resume, I'd like my presenter instance's view to not go full screen but only occupy a certain area of the screen.
The displaying is managed almost entirely programmatically, means very limited use of css files and no use at all of xml ui files.
How can I manage this ?
Thanks
Use a SimplePanel as a container for your views returned by your Navigation class instead of adding them directly to root panel, and use that instance of SimplePanel where ever you want.

Processing child Control Events in Windows Forms Components in C++

I am a noob to Windows Forms so this is likely a remedial question. I have a child component with a button and a text field. I want to use multiple instances of these in a parent component or form. At runtime, when the user clicks one of the buttons, I want the parent to get the event to decide what to do with the associated text.
Coming from the long lost world of Borland C++ Builder, during design time, I would simply double-click on the buttons and handlers in the parent would be created which I could just elaborate the code. With Windows Forms, the component controls are not clickable (at design time) from the parent and are "frozen". It is not obvious to me how to pass any child button clicks to a parent. I've tried things like changing the button modifier from private to public but that doesn't help. How is this best accomplished.
Note I am using C++ as I am sharing header file definitions with an associated C++ embedded app.
-Bob
UPDATE:
My apologies, I thought I was still in my C# search :(
This is slightly complicated if you actually want to bubble up an event, or very easy if you use methods.
Methods:
In your child container, add a constructor or property that takes in and stores the parent. Then in the button handler, call this.Parent.ButtonClicked(this); and of course in the parent, add a ButtonClicked(ChildType child) method. That should get you there that way.
Custom Event:
To use events, you need to add a few things. Firstly, add a custom EventArgs class as such:
class ChildClickedEventArgs : EventArgs
{
// include a ctor and property to store and retrieve the child instance.
}
Then add a public delegate for it:
public delegate void ChildClickedEventHandler(object sender, ChildClickedEventArgs e);
Then to your child class, add a ButtonClicked event:
public event ChildClickedEventHandler ChildClicked;
And finally, a helper method to raise it:
private OnButtonClicked()
{
if (this.ChildClicked != null)
{
this.ChildClicked(this, new ChildClickedEventArgs(this));
}
}
Then when you add the child class to the parent, add an event handler for each, and you can handle your custom event for your custom control.
Alternatively:
If you can expose the Button in your child class, simply do the above but register it to this.child.Button.Clicked, saving adding the event handler yourself.

Deactivate composed viewmodel

There is a lot of questions about this but I cannot find any answer that works with the latest durandal version (2.1.0).
I am showing a child viewmodel inside my page using this:
<div data-bind="compose: { model: activeScreen, activationData: {id:selectedId}}"></div>
activeScreen is an observable to which I pass an string like this: viewmodels/child
It works, and the child viewmodel gets activated and shown on the screen. But when I change activeScreen, I need the child viewmodel to run deactivate. Is it possible? How?
So long as your child views are instance modules and not singleton modules, you can move that code to the detached handler. That's what we do as well: All of our child views are managed through dynamic composition, not child routing (which just doesn't work for the enterprise-style app). The deactivate handler comes into play in the context of routing.
By dynamic composition I'm referring to the swapping in and out of child views/viewModels through an observable.
But, again, the key to making this work is that the child views must be instance modules. That way they'll actually get unloaded from memory. If you go with singletons, the modules will never get detached (although there are ways to force this to happen).

Some questions about Prism navigation in WPF

I thought that if you registered a view with the IoC container as "Singleton" then the same instance would be reused each time you navigate to it, while registering the view as "Transient" would create a new instance each time you navigate to it. Unless I'm doing something wrong, I've found that the IoC "lifestyle" makes no difference, and it's the IRegionMemberLifetime.KeepAlive property that dictates whether the view is re-used or recreated each time. Is this correct? (I'm using Castle Windsor IoC).
When Prism documentation talks about a view being "deactivated", is this simply the process of hiding the view when it is navigated from? And if KeepAlive=False, does the view get disposed at this point?
What about nested views/regions? If a view contains a region with another view inside of it, and I navigate away from the parent view, do both views get deactivated/destroyed (depending on the value of KeepAlive)? What about ClearChildViewsRegionBehavior - where does this fit into things?
I don't know if this apply to your situation but I implement the interface INavigationAware.
If a view should be reused for every navigation I always return true from the IsNavigationTarget method.
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}

Resources