How to enforce Prism Region A loads prior to Region B? - prism

There are two different view models (Sender, Receiver) are located in two different Prism regions and those intend to communicate. But on application start up the view model that would receive the events is not yet created. How can I enforce to load and activate first the region holds the receiver view model ? I attempted to
containerRegistry.RegisterForNavigation<ReceiverView>(RegionNames.X);
regionManager.RequestNavigate(RegionNames.X, RegionNames.X);
Or shall I take the region and region.Activate(object view); ?

Related

Interchange Children between two Layout<View>

I am developing in Xamarin for iOS, Android and UWP. I created a custom control called DragAndSwapContainer which implements two custom behaviors: DragBehavior and DropBehavior.
DragBehavior basically adds a PanGestureRecognizer to the View and emits an event when the View is being 'dragged' (and also translates the view according to the Pan done by the user). On the other side, DropBehavior listens to this events and emits other events when a View is passing over the container or when a View is dropped inside.
DragAndSwapContainer acts as a container that can receive other views, and as a draggable and droppable view at the same time. The goal of this container is to enable the user to swap between two views by dragging and dropping one into the other one.
When a View is dropped inside this Container what I do is, basically, to swap the Children Views that both containers have (the one with the view that was dragged, and the one receiving the dropped view).
Here's a piece of code for the DragAndSwapContainer (which inherits from Grid):
private void OnViewDroppedInside(object sender, DroppedEventArgs e)
{
var draggedViewContainer = e.DroppedView.Parent as DragAndSwapContainer;
draggedViewContainer.Children.Add(this.Children.First());
this.Children.Add(e.DroppedView);
}
Since each View can have only one parent, this works fine: Child View is exchanged by the DragAndSwapContainers.
Everything works fine as respects to iOS and Windows, but I am facing some problems with Android.
I am getting the following error https://github.com/xamarin/Xamarin.Forms/pull/8888 when swapping two views. After some debugging I conclude that the problem arises because while swapping children (in between the two lines of code of Children.Add) Android native renderers are disposed, so I lose track of the View.
Also it is something that doesn't happen always, seems that there's some timings issues. If I attach my Analytics service (which reports to a cloud server and adds some overhead) the issue happens way more often.
The error mentioned seems to be fixed in the Xamarin.Forms version I have (since it was merged already in v4.4.0) but I'm still having the exact same error call stacks.
Given this situation, is there another better way to exchange children between two Layout?

Complex views in MVC

In the MVC model, where does the view differentiate between the model when the views are sufficiently complex?
My question arises from my attempt to develop a desktop application with a Canvas view. The user has a click mode (e.g. select, add object A, add object B, add object C, etc.). When performing actions, this changes data in the model. The state of the model can later be saved to a file via another view control.
When starting the project, I attempted to encapsulate as much of the Canvas-specific state as possible into the Canvas view. However as I attempt to fit the MVC pattern, it seems as though most of it belongs in the model.
I feel as though the Canvas class is complex enough to contain its own state, which is where my confusion arises. It contains the location of several types of objects on the Canvas. Where does the boundary lie between view data and model data? Or is this a case where there is MVC within an MVC (i.e. M V(MVC) C)?
I have chosen MVVM after looking into some patterns. It seems to be exactly what I needed. The ViewModels hold data specific to the views or controls, that will never be saved but still need to be bound to by the UI.

MFC Application Updating Only Current View

I divided the Main View of my VC++6 MFC application using Static Splitting with rows and columns like (1x2) or (3x3) and so on.
One of these parts, when clicked on, is the Active View.
When I draw a shape say, a circle/rectangle on the Active View, all the other Views ape the shapes I drew on the Active View. How do I avoid that?
I tried UpdateAllViews() with the Active View as first argument. It still does the same.
Any ideas?
If you are using the same class for all views this is expected behavior, since splitter wires all views to the same document object. I presume that you are use document object for drawing data storage.
UpdateAllViews is used for to update views if data in the document change. Each view then uses document’s data to render different visual interpretation of this data. Hence each view would be a different type (represented by different classes) knowing how to visualize data.
For example: document is used to store number array. Three views are showing those numbers as decimal, hex and binary representation.
If one number is changed, all views are notify to update own representation.
In your case working solution would be to move drawing data to the view rather than the document. Most likely your application does not need a document at all.
UpdateAllViews() calls the OnUpdate() function for each view. The default implementation of OnUpdate() invalidates the client area (talking about simple "graphics" views like CView() or CScrollView()). You can override the OnUpdate() member and encode the desired behaviour (as far as invalidating/updating is concerned) in the lHint and/or pHint parameters.

Qt Flowchart Application Architecture

I want to build a flowcharting application in Qt to get some practice modeling GUI applications. All it has are draggable boxes and circles that can be connected with straight lines.
As this is my first GUI application, I am unsure how one typically designs such a project. Here are my two designs.
1) Build a bunch of model classes (Box, Circle, Line, etc) and associated views (e.g. BoxView, CircleView, etc). The model objects have properties like color, x, y, width, height. The view classes subclass Qt UI elements. Then there are controllers like BoxMoveController that receives mouse events from the UI and updates the box view and box model appropriately. Or perhaps it is better if the box view receives the event, updates itself, and then passes the event to the controller to update the model? Now, I create application logic for the flowcharting logic that works on the model (like connect lines to boxes). The UI updates itself accordingly by the model notifying the view objects when an update to the model occurs.
2) Forget about the model stuff and build a "view-centric" application. Build a bunch of classes (Box, Circle, Line, etc) that subclass Qt UI elements. Then build application logic for the flowcharting stuff on top of these classes.
Which is better? What would you do differently?
I would go with the graphics view framework.
Have a look at the DiagramScene example provided with Qt.
It is located in your Qt examples folder/graphicsview/.
I'm sure it will give you a good idea on how to implement such an app the Qt-way.

Options for keeping models and the UI in sync (in a desktop application context)

In my experience I have only had 2 patterns work for large-scale desktop application development when trying to keep the model and UI in sync.
1-An eventbus approach via a shared eventbus command objects are fired (ie:UserDemographicsUpdatedEvent) and have various parts of the UI update if they are bound to the same user object updated in this event.
2-Attempt to bind the UI directly to the model adding listeners to the model itself as needed. I find this approach rather clunky as it pollutes the domain model.
Does anybody have other suggestions? In a web application with something like JSP binding to the model is easy as you ussually only care about the state of the model at the time your request comes in, not so in a desktop type application.
Any ideas?
I am currently using the event bus approach to synchronize the models and the UI in my application, but I have hit a hurdle with it in that it's difficult to make it very fine grained, for example, at the property level where you are just interested in knowing if property x of an object gets updated, and there are hundreds or thousands of such cases.
For such a fine grained control, you might want to checkout how KVC (Key Value Coding) and KVO (Key Value observing) works in Cocoa. It basically allows an object to observe any other object's properties as long as it uses some basic principles of KVC. The interested objects automatically get notified upon changes, and you don't have to explicitly notify the observing objects on each property change as that is taken care of by the underlying implementation of KVO. It is somewhat similar to the PropertyChange listeners in Java beans.
If there is too many observations going on, and writing the glue code to update models/views on property changes becomes problematic, you might want to take it a step further and have data-binding to keep models and views synchronized. Built upon the concepts of KVO, the idea is to bind properties of objects, so that a change in one automatically updates the other, and vice versa. For example, you could bind the text in SO's answer field, to the answer preview that we see right below.
.bind('answer.value', 'answerPreview.text')
Both happen to be view elements in this case, so data-binding is a generic approach, and can be used to bind objects more appropriately and not just UI with models.

Resources