MvvmCross: Calling Close on my viewmodel crashes Store app - exception

I have an application that crashes when I call Close(this) in the viewmodel at level 2 (meaning can go back one page). One at level 3 (meaning can go back two pages) goes back fine.
I call this in the main app:
// Register the main view model
Mvx.RegisterSingleton<IMvxAppStart>(new MvxAppStart<StartScreenViewModel>());
In StartScreenViewModel I call
this.ShowViewModel<AddNewViewModel>();
And that viewmodel has a ICommand binding in the XAML that calls a function that calls:
this.Close(this);
When calling this Close(), I end up in the UnhandledException handler.
The exception message is:
The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsPage.LoadStateBundle(NavigationEventArgs e)
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsPage.<>c__DisplayClass1.<OnNavigatedTo>b__0()
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsExtensionMethods.<>c__DisplayClass1.<OnViewCreate>b__0()
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsExtensionMethods.OnViewCreate(IMvxWindowsView storeView, Func`1 viewModelLoader)
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsExtensionMethods.OnViewCreate(IMvxWindowsView storeView, MvxViewModelRequest viewModelRequest, Func`1 bundleLoader)
at Cirrious.MvvmCross.WindowsCommon.Views.MvxWindowsPage.OnNavigatedTo(NavigationEventArgs e)
What could cause this?

After some more digging and wondering about navigation, I figured it out.
I had overridden
OnNavigateFrom(...)
in the StartScreenViewModel, but had not called
base.OnNavigateFrom(...)
in the function body! Adding that called now allows navigation to work correctly. Noob!

I believe that by default Windows Store apps dispose of pages that are not on the top of the navigation stack. You can override this behaviour.

Related

JavaFX FXML Parameter passing from Controller A to B and back

I want to create a controller based JavaFX GUI consisting of multiple controllers.
The task I can't accomplish is to pass parameters from one Scene to another AND back.
Or in other words:
The MainController loads SubController's fxml, passes an object to SubController, switches the scene. There shall not be two open windows.
After it's work is done, the SubController shall then switch the scene back to the MainController and pass some object back.
This is where I fail.
This question is very similar to this one but still unanswered. Passing Parameters JavaFX FXML
It was also mentioned in the comments:
"This work when you pass parameter from first controller to second but how to pass parameter from second to first controller,i mean after first.fxml was loaded.
– Xlint Xms Sep 18 '17 at 23:15"
I used the first approach in the top answer of that thread.
Does anyone have a clue how to achieve this without external libs?
There are numerous ways to do this.
Here is one solution, which passes a Consumer to another controller. The other controller can invoke the consumer to accept the result once it has completed its work. The sample is based on the example code from an answer to the question that you linked.
public Stage showCustomerDialog(Customer customer) {
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"customerDialog.fxml"
)
);
Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(
new Scene(
(Pane) loader.load()
)
);
Consumer<CustomerInteractionResult> onComplete = result -> {
// update main screen based upon result.
};
CustomerDialogController controller =
loader.<CustomerDialogController>getController();
controller.initData(customer, onComplete);
stage.show();
return stage;
}
...
class CustomerDialogController() {
#FXML private Label customerName;
private Consumer<CustomerInteractionResult> onComplete
void initialize() {}
void initData(Customer customer, Consumer<CustomerInteractionResult> onComplete) {
customerName.setText(customer.getName());
this.onComplete = onComplete;
}
#FXML
void onSomeInteractionLikeCloseDialog(ActionEvent event) {
onComplete.accept(new CustomerInteractionResult(someDataGatheredByDialog));
}
}
Another way to do this is to add a result property to the controller of the dialog screen and interested invokers could listen to or retrieve the result property. A result property is how the in-built JavaFX dialogs work, so you would be essentially imitating some of that functionality.
If you have a lot of this passing back and forth stuff going on, a shared dependency injection model based on something like Gluon Ignite, might assist you.
I've used AfterBurner.fx for dependency injection, which is very slick and powerful as long as you follow the conventions. It's not necessarily an external lib if you just copy the 3 classes into your structure. Although you do need the javax Inject jar, so I guess it is an eternal reference.
Alternately, if you have a central "screen" from which most of your application branches out you could use property binding probably within a singleton pattern. There are some good articles on using singleton in JavaFX, like this one. I did that for a small application that works really great, but defining all of those bindings can get out of hand if there are a lot of properties.
To pass data back, the best approach is probably to fire custom Events, which the parent controller subscribes to with Node::addEventHandler. See How to emit and handle custom events? for context.
In complex cases when the two controllers have no reference to each other, a Event Bus as #jewelsea mentioned is the superior option.
For overall architecture, this Reddit comment provides some good detail: https://www.reddit.com/r/java/comments/7c4vhv/are_there_any_canonical_javafx_design_patterns/dpnsedh/

Close view model (navigate back) in Init

I'm attempting to call Close(this) in Init in a ViewModel to navigate back, but I'm receiving the following from Mvvmcross in the debug output when running on Android.
mvx:Warning: 7.59 Ignoring close for viewmodel - rootframe's current page is not the view for the requested viewmodel
Is it possible to Close here?
Maybe I'm approaching this wrong. I want to open a connection based on a string I'm passing in Init and if it fails, show a toast (or equivalent notification) and go back.
No. Nor can you do it in Start.

Mvx ShowViewModel not triggering init

On a slide out drawer I have a link which performs a ShowViewModel passing in parameters. I use the MvxBundle to set flags to clear the back stack. If the view is already shown and I click on the slide out drawer and click the link again which executes the ShowViewModel, the Init is not subsequently called. Further, the way our screen is designed the user would have to hit the back button to leave the screen. Is it not possible to invoke Init via ShowViewModel if that view is already shown?
Init is a certain step of ViewModel lifecycle.
You cannot trigger it outside after your ViewModel is create though you can call it manually if you have a reference to it.
make sure your init looks like that:
public void Init(//any number of parameters you passed at ShowViewModel)
{
}

Navigation Pattern in WinRT

My doubt is when we navigate from one page to another page in WinRT the Constructor() and On Navigated to() methods are called.
Can we restrict the constructor calling.
As my requirement is i have to attach a new object to the datacontext while navigating again.
You can't create an object without calling its constructor. You can set the DataContext in OnNavigatedTo().

When should I load data in a Windows Phone 8 application?

I've seen a lot of questions here related to the OnNavigatedTo method, but none of them seem to answer the basic question of, "At what point should I load data?" The documentation on MSDN doesn't explicitly answer this question, as far as I can tell.
If I need to load a list of data from the local database, which method is the most appropriate to use? Should I use the OnNavigatedTo method, or the Loaded event?
What I've been using so far is this pattern, which seems to work well:
protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
if (NavigationMode.New == e.NavigationMode) {
var data = LoadData();
this.DataContext = data;
}
}
What this means is that for a new instance of a page, load the data synchronously. This also means that the page will not be rendered until the data has finished loading and the profiler complains that I'm using too much UI thread time.
An alternate approach is this pattern:
protected override async void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
if (NavigationMode.New == e.NavigationMode) {
var data = await LoadData();
this.DataContext = data;
}
}
But with this pattern, it seems to me that navigation, and therefore page rendering may occur before I've loaded the data and set the DataContext, meaning unnecessary re-paints and what-not.
I usualy bind to a ViewModel directly in XAML. Then in OnNavigatedTo I trigger the view model to fetch its data async.
This allows me to show basic values from start (page title etc.). Then when I start fetching the data I can also activate a progressbar directly in the ViewModel and then remove it once the data is fetched.
I recommend you load your data asynchronously. OnNavigatedTo is one place where you can start the loading. If you're talking about a page that the user is almost certainly going to navigate to, then you may be able to start loading earlier.
I have a series of blog posts that look at how async has some friction with traditional OOP. There are a couple of posts that look at data binding in particular, e.g., asynchronous construction (the section on asynchronous initialization) and asynchronous properties (the section on data binding).
Just a few hours ago I announced the first stable release for my AsyncEx library, which includes the NotifyTaskCompletion types that you can use to kick off an asynchronous loading operation and have your view respond automatically (via data binding) when it completes.
But back to the core problem: you do have to show something while the data is loading. I recommend you do not consider this "unnecessary", but rather accept it as an opportunity to provide a better user experience. Think about what you want your app to look like on a slower phone or if there is an error loading the data. Any time there's I/O, design the "Loading..." and "Error" states as well as the "Loaded" state.