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)
{
}
Related
I have a Spark Window that I use as a popup window in front of my main application. I prefer this method to the PopUp Manager. However, I run into a strange error when calling the windows close() method. In the closing event handler, I have a function which does the following
public function closeHandler(event:Event):void {
this.owner.removeChild(this);
}
The window's owner is WindowedSystemManager, but at some point during the removeChild phase, the stage becomes null, and Flex throws an error trying to close the nativeWindow because it cannot find a reference to it. Two things fix this,
Instead of calling window.close(), call window.nativeWindow.close();
Do not call this.owner.removeChild(this) in the closing handler.
I'm trying to understand what is going on, does anybody have any idea??
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.
I have an Android app which uses a SupportActionBar that contains a bunch of tabs. These tabs each have a fragment that in turn are connected to a ViewModel in my core project. This works great and when i start the app they are all initialized right away and setup correctly.
What i would like to do is to call on a method on one of these ViewModels from my main activity that contains all the tabs and fragments.
I read in another post that in WP you could cast the DataContext to the ViewModel but that might not work in Android. I haven't been able to do this, maybe because my DataContext is not the currently displayed ViewModel but the MainViewModel connected to my main activity. Or maybe it's not supposed to be done that way, i'm not sure.
I'm trying to do this:
var test = (MessagesViewModel)this.DataContext;
test.GetViewDataFromApi();
To update the data in the view when i press the tab. I can't use the Init function for this for example since the ViewModel isn't recreated everytime i show the view.
Are you trying to update some data in the tab's fragment when tab is selected?
If that's the case, one way to do it is to
1) handle the tab selection event to get the current tab(maybe using TabListener),
2) get the fragment (MvxFagment) in the selected tab
3) get the (IMvxViewModel) view-model from the fragment
4) call the method you need to update data on the view-model
I assume you are using a MvxFragment (https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Droid.Fragging/Fragments/MvxFragment.cs?source=cc) so you can access the view-model from the MvxFragment's ViewModel property.
I have a custom component called shopView which is an MXML skinnable component. I have a controller class called ShopController which takes care of showing this component in popup, updating info shown in the component etc.
Now, I wanted to maniupate some of the subcomponents of this ShopView after it has been created from the controller after the ShopView is created (creationComplete() event)
So, I have attached an event listener which intern does some initialization process
creationComplete="init(event)"
the init() function
private function init(event:FlexEvent):void{
event.stopImmediatePropagation();
initMenus();
initSlots();
dispatchEvent(event);
}
Attached another creation complete event from the controller class
_shop.addEventListener(FlexEvent.CREATION_COMPLETE,onShopCreated);
*_shop is the instance of ShopView*
Now, if you see the init() function, there I am stopping the event propagation, doing some initialization process and after that I am dispatching the event (for the shop controller do the rest of the job)
Now, this is crashing the app because the crationComplete event of the ShopView is recursively called. I was thinking the dispatchEvent will propagate to the other listerners but seems like it is propagating back to the same component.
I have fixed it by removing the e.stopImmediatePropagation() and dispatchEvent(event) lines from the init() function. But I want to know why it is happening like this?
Is it a known issue for the mxml/flex components? OR it is expected behavior?
Update: I am not doing same in .as as I said below. Got answer, basically its my stupidity :)
because I have not seen this behavior when I write .as classes where I
stopevent propagation and dispatch the event based on business logic.
Thanks in advance.
This is expected behavior.
When you redispatch an existing event dispatchEvent automatically clones it (since you can't dispatch the same event twice.) This clears any propagation-related flags.
May I ask why you want to redispatch CREATION_COMPLETE in this situation anyway? Both handlers will function just fine without the two lines you removed.
I am working on an AIR application:
The main window is like a dashboard. With the menu bar, I can open other windows with dashboard details. When I close these, I'd like to refresh the main window.
I tried to use an event listener, but the result is not good. If I open detail windows directly from the main window, I know how to add an event listener - and it works - but I don't know how to do it, if the detail window is opening from the menubar!
Thanks for helping me.
A Singleton is what you are looking for. Just put an event dispatcher inside and you will be able to listen from everywhere in the application.
A Singleton is like having a unique instance of an object in memory, so anyone modifying a variable inside that object ( or sending events throught ) will be modified for everyone.
Here is an example of code on how to use it.
http://life.neophi.com/danielr/2006/10/singleton_pattern_in_as3.html
Note: Singletons are powerful and dangerous at the same time, there is a lot of talk about how to use them, please read a little more about that if you are considering building a big project.
Hope it helps!
The issue is that you're performing business logic from a View. Don't do this. Instead, dispatch an event from each menu rather than directly opening the window from within it. Listen for those events at a higher level, and then you can either directly listen to the new windows you have opened, or you can create a base window Class that exposes a variable of type IEventDispatcher. If you populate that variable with the same event dispatcher, what you wind up with is called an "event bus," and you can listen on that for events.
This architecture requires a little more thought than using a Singleton, but it avoids the tight coupling and other issues you'll run into by introducing one into your project.
You can listen to an object (EventDispatcher) directly by adding an event listener to it, or if the dispatcher object is on the displaylist, such as a Sprite, you could listen at the stage level with the capture parameter set to true.
But the main caveat is that the dispatcher must be on stage for you to catch this event.
Your main window listens to stage (with capture = true):
stage.addEventListener("MY_CUSTOM_EVENT", handle_custom_event, true);
private function handle_custom_event(e:Event):void
{
var sub_window:Object = e.target;
// do something to your sub_window
}
Your sub window can dispatch events like this:
dispatchEvent(new Event("MY_CUSTOM_EVENT"));
But (ab)using the stage as a message passing infrastructure for custom events in this way is a little messy. You could consider a more formal message passing architecture if you really want this kind of communication. Even a static MessageBus class would at least quickly help you identify where you use this in your codebase. Either way, you'll have to be careful about references and memory leaks.