Prism - Region with IActiveAware not called on initial load - silverlight

I have regions within a TabControl that load fine when I click the tab headers but I cannot set a default view when the page initially loads.
Ex: If I have 3 tabs, I want the first one loaded by default. Easiest way to do this?

After InitializeComponent() in the constructor
TabControl.SelectedItem = TabControl.Items[0];
Did not test it though

Related

Updating Main Window from page in a frame

I'm currently developing a WPF application using the MVVM framework. And I have this functionality:
I have a main window which has a combo box and frame (where I put my pages) and a view model. One of the pages in that frame is where a user can add a data and these data are used to populate the combo box in the main window. My problem is how to automatically update the items in the combo box after adding a data from that page. Btw, this page has a different view model too.
Thanks.
You can establish an event in the page viewmodel for changed data. Then subscribe to those events within the window viewmodel and update the combobox items accordingly.
You have access to the parent window from iframe via window.top. You have to write this code in your page what you have loaded in iFrame.
window.top.document.getElementById("combobox_element_id").value='Your New Value';
best of luck
You can pass the Source DataContext of ComboBox (ObservableCollection) to page ViewModel so you can simply modify the collection from Page view model.

How to load content into dynamically created TabItems On-Demand/Lazy Load?

I have a tab control defined in my app and I'm adding tab items to it dynamically. Lets say I have list of objects with lots of data and for each object I show 5-10 tabs. The ordering and the tabs shown differ from object to object. That is why I have to dynamically create these tabs based on my object.
As far as the content inside these tab items go, I generally set the .Content property of each tab to a UserControl and it loads it as shown in below code.
If ContainsFingerprints() Then
Dim fingerprintT As New TabItem
fingerprintT.Content = New FingerprintTab()
fingerprintT.Header = "Fingerprints"
fingerprintT.Name = "tiFingerprintTab"
viewTab.Items.Add(fingerprintT)
End If
Some of these tabs have lots of images and take up lot of memory at run time. That is why I would like to load Content in those tab items 'on-demand', that is, only when the tab is selected.
I could use the SelectionChanged event of the tab control and then load the required content but is there a better solution that WPF offers?

Navigate between views WPF PRISM application

I am working on a WPF PRISM application that has the following structure (I have simplified to explain better without additional layers). I am using Unity as my DI/IOC
AppMain - Bootstrapper
Gui - Views and View Models
Data - Data using EF.
In Gui, I have views names as below:
Home
EmployeesView
OrdersView
Reports
I have three regions in the shell.
MainRegion - Main Content
TopRegion - Navigation Menu
BottomRegion - Status Bar
I am using the following method to register views to the regions
IRegion region = _regionManger.Regions[RegionNames.MainRegion];
var mainView = _container.Resolve<Home>();
region.Add(mainView, ViewNames.HomeViewName);
region.Activate(mainView);
The first of activation happens in the Module Initialize method for Top, Main and Bottom.
After this, I am activating other views when the button are clicked. It is just code behind for now. Sample code here:
IRegion region = _regionManger.Regions[RegionNames.MainRegion];
var reportView = region.GetView(ViewNames.ReportsViewName);
if (reportView == null)
{
reportView = _container.Resolve<ReportsView>();
region.Add(reportView, ViewNames.ReportsViewName);
region.Activate(reportView);
}
else
{
region.RequestNavigate(ViewNames.ReportsViewName);
}
PROBLEM1: Any advise on how this can be done or the way I am doing is fine.
The top menu has Home, Employees, Orders, Reports buttons.
In the home page I have recent orders by the employee in datagrid as readonly.
I would like to double click to navigate to the OrderView and pass the selected order to show to the user. PROBLEM2 I am not sure where to do the navigation for this.
PROBLEM3: Another issue was if set the RegionMemberLifeTime keepAlive false, INavigationAware methods don't fire. If I don't set the KeepAlive to false, the page does not get refreshed because, the view model does not get called.
I need the pages to refresh when it is navigated to and not be stale and also handle any confirm prompts to the user when the view is navigated away from it.
Your help is very much appreciated.
it's certainly too late but…
Problem 1/2: is there a particular reason why you add content to region in module initializer?
the common way is more like -> in xaml:
<ContentControl prism:RegionManager.RegionName="MainRegion" />
and in ModuleInit.cs -> Initialize()
_regionManager.RegisterViewWithRegion("MainRegion", () => _container.Resolve<MainView>());
Problem 3:
the view has to implements INavigationAware, IRegionMemberLifetime
and to swich region, in the viewModel you do:
_regionManager.RequestNavigate("RegionWhatever", new Uri("TestView", UriKind.Relative));
But to work you have to register it in ModulInit.cs as an object with viewName, like that:
_container.RegisterType<Object, TestView>("TestView");
and a contentControl with the correct RegionName define in xaml of course

How to make views visible and collapsed depending on whether the corresponding module is loaded or not?

I have a prism application already in production.
I need to create a new module in this application.The module can either exist on not exist in the application depending on the configuration file.
This module view should be dispalyed in a region already existing. The view should only be visible when the module is loaded.
Problem: the existing screen has a region showing 2 tab items from 2 different modules. Now the new view should be part of the first tab and it gives the user the option to select whether to view the previous info or the new info from this new module.
I am stuck and dont know how I am going to show these checkboxes when the new module is loaded and hide it when it is not. Where do I put this logic?
Current scenario:
Very vague images to define what i meant?
Here's an option that will work, and isn't too much effort...
Inside your beautifully illustrated Tab1, add another tab control, and make this a Prism region. Fill this region with both the original view, and the view from your new module.
Then, restyle the inner tab control, if necessary, so the tab headers appear like radio buttons, and set up a datatrigger to hide the tab header strip if the number if tabs is less than or equal to 1.
Thanks for all your help :)
After a lot of RND i figure it out.
I have created a two tabcontrol regions one for the main tab and the other for the child tab.
on the child tab i have return style to display tabitem only if item.count is more then 1 as suggested by mark.
and i have put this code in my existing module.

Silverlight navigation app usercontrol state

I have a navigation app with 5 pages. I also have a usercontrol with 3 radio buttons. This usercontrol is used by all 5 pages in my app. By default the first radio button is selected. However if the user clicks the 3rd radio button and I go to another page I want my usercontrol to still show that 3rd radio button as selected. How do I go about accomplishing this task?
There are several ways you could do this. Probably the most simple way is to create a class that has a property for the selected item of the radio buttons. Add an instance of this class as a resource to the application and then bind the radio buttons to this static resource.
public class MyState
{
public string SelectedRadioValue {get;set;}
}
In the App.xaml.cs in the Application_Startup add:
var state = new MyState()
Resources.Add("myState", state);
Then in the bindings you can set:
SelectedValue="{Binding Source={StaticResource myState},Path=SelectedRadioValue}"
There are other approaches you could take as well.
Is it possible to show this control outside of the View control? It sounds like it should be placed on the MainPage.xaml outside of the place where the views are displayed. This way the one control is used and its value can be made available to all of the views.
If you are just looking to save the state of the page as the user navigates between pages then the easiest way of accomplishing this is to set:
NavigationCacheMode="Required"
in the first element of the xaml for your page.

Resources