In a WPF Window Application, I can override OnStartup method to add my own logic when the app is launched.
My my app is a add-on of another app, actually, its a UserControl.
Where to add my own logic to the app when the app is launched?
Now, I write the code in code behind. Is there a more sensible place to do that?
Are you just trying to define startup logic for a UserControl?
Then it depends what you're trying to initialize:
Are you trying to initialize all sorts of UI-related data?
The best solution would be either in the constructor, or in the Loaded event in code behind
Are you trying to initialize the underlying data for its ViewModel?
Then you should create it when you call the UserControl from your parent app. It should initialize your ViewModel and its data, and ilnk it to the UserControl you're trying to load
Should be enough to get you started!
Related
I am building an app using UWP and I have MainPage.cs which contains a Frame control where I load different child pages. I would like to pass a listener to the child page to notify my parent page about event that happen in the child. On android I would pass a callback listener which I will have to trigger in the sub-pages so I can get notified in parentl The issue here is that pages are started Frame.Navigate(typeOf(LoginPage)) for which I cannot set a callback since login page is not a referece/object but a type class.
Any help would be greatly appreciated.
It seems like the MainPage is a singleton instance. In such case you could just expose this instance by creating a static MainPage Instance property in MainPage and set it in its constructor. Then you could access MainPage directly from any other page.
The proper solution however would be to use a MVVM framework and instead of communicating between the views directly put the communication within the view models. In MVVM you usually have a sort of navigation service which allows passing parameters/return values and also you can have a event aggregator to raise messages to which other components can subscribe. Good examples of MVVM frameworks are MvvmCross, MvvmLight and Prism. It is definitely worth learning to use and integrate them early on, because it will make your life significantly easier in terms of code maintenance in the long run.
I have a class that is inheriting from a UserControl. I am showing this class in a WPF TabControl as a tab. The tab has a small x and can be closed by clicking on it. I need a way to do some cleanup code before the tab is destroyed.
I don't believe I can use the Unloaded event to do this because the Unloaded event is called when the UserControl is being destroyed and it is also being called when you click on another tab.
Any ideas on how to deal with this situation?
EDIT:
Here is more info.
In my UserControl class I have a 3rd part control that I am using. Basically a graphing control. There are a couple lines of code I would like to run to ensure that there are no memory leaks. If you want to read more about it then this would be the web address that talks more about it:
http://support.scichart.com/index.php?/News/NewsItem/View/21/wpf-xname-memory-leak--how-to-clear-memory-in-scichart
You can have a look at the way I have done this in the dragablz TabControl on GitHub.
Essentially the TabControl listens to a RoutedCommend raised from the close button, then calls an optional callback which enables a MVVM view model (or old-style control type code) to dispose an associated view model, or perform any other tidy up code you want to do (or indeed cancel the close operation).
In the example project file on GitHub, look for ClosingItemHandlerImpl and work back from there.
http://github.com/ButchersBoy/Dragablz/blob/master/DragablzDemo/BoundExampleModel.cs
ClosingItemHandlerImpl is bound in from the XAML, and the tab control will call it prior to removing a tab.
I have a mvvm wpf application that loads up a window consisting of many controls in one go. I want this window to load up the controls separately and asynchronously.
Any suggestions?
For that matter, I use a Singleton pattern. If you're familiar with PRISM and its Bootstrapper, it is kinda similar.
The main idea here is to override the method OnStartup in your App.xaml. Default behavior shows in your xaml StartupUri="MainWindow.xaml", you'll have to remove that property.
In the OnStartup override method, I use a class which creates all my objects (Views, ViewModels, link DataContexts... ) and fires an event when initialization is complete.
At this time I dismiss the splashscreen and show a fully loaded app (the InitializeComponent is called when you create your MainWindow, so it'll be already called at this time).
For more extended use, you can add events in your windows and EventHandlers in your bootstrapper class. I use it for example when I want to fully refresh my app (reboot it), and also for database requests (which are, in my case, performed only on application startup to load the referential).
Hope it helped :)
Unfortunately, if your controls are defined in XAML, they're going to be initialized with the InitializeComponent call and go through the loading process. All of the events in loading a XAML window happen whether or not you subscribe to them.
You could add controls dynamically to the form in code behind, but if you have a separate thread do the work, you would need to use the Application Dispatcher CheckAccess and Invoke methods to ensure the controls are loaded on the thread which owns them.
I'm using AvalonDock in a WPF application, and need to persist the layout of the AvalonDock the user has setup (moving, docking, detaching of the panels).
The function to do that are on the control itself (SaveLayout, RestoreLayout).
What is the correct way to do that ?
Right now I have a command at the mainWindowViewModel that gets created by the application when creating the instance of the window view and the viewmodel. It basically makes the relay command call an anonymous method that calls the needed function on the view control. This works since the application creates the window and the viewmodel for it.
But how would i approach this if some lower level view and viewmodel had to do this? If using this method I'd have to still create the Command at application level and sending it through the viewModels down to where it is needed to be bound to? Inside it I'd have to search the usercontrol that is a view then the avalonDock control inside it and use that at application level, which is bug prone. Somehow it seems dirty...
Thanks!
You might introduce an interface IView so that the ViewModel can call a method on the View. This way the ViewModel doesn’t need to know about the concrete View class and can still be unit tested.
How this can be accomplished is shown by the sample applications of the WPF Application Framework (WAF).
You can use decoupled messaging to communicate between view models
http://www.bradcunningham.net/2009/11/decoupled-viewmodel-messaging-part-1.html
How could you create an application that could search through a directory, find user controls in the form of paired .xaml / .xaml.cs files, and compile/execute these on the fly? Is this possible? Is there an .exec() or .compile() class somewhere that could do this?
Basically what I want to do with this is have a WPF application that is running and by having the user fill out a form describing the kind of page/functionality he wants, the application could generate XAML and code behind files which are then immediately usable without having to recompile the application.
I'm assuming that this is to change the behaviour of the UI on a known application rather than a XAML/CS component generator for use in another application - after all there's already applications that do this - Expression Blend anyone?
Do you really need to recompile the underlying CS? As far as I can see it all you'll be doing is changing the apparent behaviour and look of the application and UI. This could be achieved by command binding within the xaml and styles for the components.
The reality is that in order to perform the functionality that you require you'll be giving the user a finite choice as to behaviour. You'll need to decide what behaviour is application and what is the UI.
Application bahaviour is governed by fixed commands (they could accept parameters to change behaviour).
UI behaviour (appearance, animation etc) is covered by the xaml. If the application runs in a browser window you could auto generate the xml needed as requried, linking it to the underlying app. commands and allow the browser to execute the new behaviour for you.
Is this a good idea? I can see a few problems with this.
How will the code behind 'know' how to interact with the rest of the application
Security? You will be allowing somebody to make system API calls on behalf of the main application
App domains???
Rather build up the forms using ItemsControls and DataTemplates. In the form where the user specifies what functionality he wants in the form, you will be presenting him with a list of 'building blocks' anyway. Make each 'building block' a ViewModel and associate each ViewModel with a DataTemplate 'UserControl'.