For UWP application there is quite detailed explanation of separation View from ViewModel, using data binding. Many examples show how to x:bind in XAML. My questions are about Model. It should run in a separate thread, in-process background task is sufficient for my application.
Where should that thread be initiated? App::OnLaunched()? MainPage constructor?
I create instance of ViewModel in MainPage constructor and store it in MainPage. All other pages have a reference to MainPage, so they access ViewModel. Is MainPage a right space for holding ViewModel object?
Then how do Model object and ViewModel object communicate?
A link to a relevant example would work for me.
Related
I am writing a very basic 3D modelling program. I've used LWJGL to render my objects and JavaFX to provide an user interface in a separate window, new thread.
As I saw JavaFX likes to take control over the application, but in my case this was not an option. I tried to pass my already created scene graph to the JavaFX controller class, but I didn't find a way doing this properly.
It's seems to be impossible to pass anything from outside into the main JavaFX class. The start method loads the layout from an FXML file with reflection magic, but this method is called in the constructor, therefore variables are not initialized. Defining a new constructor with parameters throws an exception (class cannot be initialized).
After struggling many hours, I gave up, I've decided to create a new scene graph in the JavaFX controller and created a getter method to it.
public class Toolbox extends Application implements Runnable {
private ToolboxLogic logic = new ToolboxLogic(); //controller, the scene graph is instantiated
...
public SceneGraph getSceneGraph() {
return logic.sceneGraph; // returns the scene graph
}
}
Not a beautiful solution, but it shall work, I said. But it doesn't.
I tried to load a file in two locations:
with code written in the LWJGL renderer
with buttons, calling a method in the controller
If I load a file from the renderer, my objects show up on the screen, but I cannot navigate with the buttons, only the root node appears in the scene graph.
If I load a file from the user interface, I can navigate on the tree, but it doesn't show up in the renderer.
It seems like here
return logic.sceneGraph;
Java would have done a deep copy instead of returning a pointer, and each part of my program is working with its own version of the scene graph.
What is a problem, and how can I make it work properly?
Thank you!
OK, I got it working by setting the scene graph static. Strange! Now, I'm really curious, why it behaves like this.
I created a very simple new Cocoa application, with a new class and custom controller (or object in interface builder, the plain blue cube). I connected all outlets with interface builder and assigned the custom delegate to the class. The problem is, the code does not get executed (checked by setting breakpoints), the window presents itself, and there are no errors.
The code of the class is really irrelevant, I tried once before with a proper set up and it worked there, I couldn't spot the difference however.
The key here is a separate controller together with class instead of the standard App's Delegate, to integrate the project to a bigger one.
In my iphone application, I have multiple view classes and model class and I take the property of view class to my model class via setter but I do not use in other view class this instance via getter. For instance, in viewA class I have text Field instance and in modelA class I have Nsstring object to hold textField instance, and I use the instance of ModelA in viewA and I take the textField instance to ModelA class via Setter, but in ViewB class I have instance of ModelA but I do not take this object via getter, How can I handle this problem?
p.s.I started to programming a little time ago, I am new in objective c programming..
Its really hard to understand the specifics of your question, perhaps an example will be useful here. However, if your purpose is to share data between views you create a Data Model class in code before the views are created (maybe in the app delegate) and pass it to both view classes on creation. They can both hold a reference to the same object.
Here is a simple tutorial I wrote a while back which shows the use of multiple views in a tab bar. It passes the text from one view to the other. There is source code in the tutorial as well. The code design is not the best I have ever made but I was trying to keep it simple.
iPhone Tab Bar tutorial
I am getting incredibly frustrated with interface builder at the moment, and would appreciate some help before I ragequit it and code everything by hand (which seems to be much, much, much easier).
The basic situation is this: I need to make a model variable accessible to each view controller in my application.
The simplest way I can see to do this is to just create a property on the view controllers that retains the model, and to set that after the controller is initialised.
However, I can't find any of the actual initialisation code for the views shown on the storyboard in my project. There's no reference to any of them at all. Does the interface builder really generate not generate any code reference to its controllers in the app delegate?
For that matter, why is there no reference to any of the top level controller objects (tabview, tableview etc) in code at all?
All I want to know is how to force xcode to actually generate the controller creation code in AppDelegate.m - so that I have access to the created instance of the controller - or, failing that, a way to share the model between these amorphous objects.
Maybe it would be easier to create a singleton class where you can store all your global variables and methods. Example here.
You will need to manually create a subclass of your view controller and then override the methods you want to inject code into. In Interface Builder you can then choose to make your View controllers of this custom type.
I have more than one PasswordBox on my view and I want to pass all their SecureStrings to my view model when I click a button.
My guess is that I want to populate an instance of a custom class with all the SecureStrings and pass that object as a parameter to the RelayCommand bound to the button.
If I only knew how...
My current idea for a work around:
In the RelayCommands action for the button: send out a NotificationMessageAction with a callback taking a custom class as parameter.
Register for that message in the views code behind, and then populate an object with the SecureStrings, and then pass that object back to the view model with the help of the callback. Not very nice...
There must be a better way to do this in XAML, right?
Actually, I think what you want to do is implement event handlers, or an attached behavior on your PaswordBoxes that will push the SecureStrings to properties in the same viewmodel object that will be handling the RelayCommand's action. Then your RelayCommand won't need any parameters at all.