In my MainPage.xaml, I created a Pivot Control: <controls:Pivot Title="Powder God" Name="PivotControl">.
My first pivot view is a HubTile that summarize all other individual pages. So my application bar will be different between the first pivot view and all other ones.
That's why I put my application bar in App.xaml's resource section, then load based on selected index of my pivot.
My question is:
In the application bar I will be using for all individual pages, I want to have a delete option, where I will remove that specific item (a view model) from my data context.
I know I can use PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame; to access navigation services, but I don't know how can I reference to my pivot, so that I can get the selected index and proceed forward.
Thanks!
Using MVVM you SHOULDN'T do this:
((PageType)Application.Current.RootVisual).PivotControl. //Blah
PageType is whatever type PhoneApplicationFrame is that contains your PivotControl. If this doesn't work you need a Property in the RootVisual
PAGE
public Pivot MyPivot
{
get
{
return PivotControl;
}
}
APP
((PageType)RootVisual).MyPivot. //Blah
On one level Microsoft's suggestion of putting the ApplicationBar in App.xaml is great as it can be referenced from everywhere and would appear to encourage code reuse: however this question highlights the limit to this approach. An application bar is typically used to provide actions which are specific to the current page (or pivot item) and just because the buttons are the same you may not want the exact same code to run in each case.
In this case I think it would better to create a factory method that creates your common ApplicationBar with the click handlers you specify specific to your page/pivot item. For bonus points put the method in a new class (not App) so it doesn't get lost in all the boilerplate code there. Call this factory method in your page constructor and remember your ApplicationBar in your class. For multiple app bars, create them all up front and you can then easily switch between these app bars in your Pivot SelectionChanged code.
The alternative of creating the ApplicationBar in App.xaml and then retrieving this from the App.xaml.cs "Resources" ResourceDictionary in code, modifying the click callbacks, is more complicated in my opinion.
I wish they'd done a better job of implementing the ApplicationBar so people wouldn't want to do this. I've found that using the ApplicationBar forces you to add code to your Page.xaml.cs even if you use a framework like MVVM Light. This is still OK in MVVM as it's UI specific code that belongs in the View, but it makes things inconsistent if you're using ICommand everywhere else. Last time I decided it was better to create the entire ApplicationBar in code rather than hack this kind of thing via App.xaml.cs.
Update: There is a UserVoice request for a data bindable ApplicationBar.
Related
The NavigationService system that comes with MVVM Light seems to assume that I want to make the whole window navigate to another page, however, I would like to have a lateral bar from which I can select to which page to go, something like a hamburger menu in cellphone apps. That is, I only want to navigate using this frame that is inside a NavigationView.Content.
I could get it going by binding the content frame to the MainPageViewModel and navigate using it, but I would rather use the NavigationService provided by MVVM Light to learn it better.
Thanks for the help!
I could get it going by binding the content frame to the MainPageViewModel and navigate using it, but I would rather use the NavigationService provided by MVVM Light to learn it better.
You're on the right direction. Even if you use the NavigationService class provided by MVVMLight, you still need to provide your content frame instance for its CurrentFrame property. You could check its source code on GitHub.
For example,
var nav = ServiceLocator.Current.GetInstance<NavigationService>();
nav.CurrentFrame = YourframeControl;
Here, I wanted to provide an easy way to you. You could try to use the WindowsTemplateStudio to create your UWP project. You could choose 'Navigation Pane' project type and 'Mvvm Light' design pattern. Then, you would find that the 'WindowsTemplateStudio' has helped you do all relevant things. On the 'ShellPage.xaml', it has a NavigationView control for navigation. It also has implemented a NavigationServiceEx class for you.
In the ViewModelLocator.cs:
public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();
In the 'ShellPage.xaml.cs':
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);
This will do the same thing as my code above. Then, when you use the NavigationService to navigate to other page, it will use the 'shellFrame' control to navigate.
I have a a PopUp with a Region that contains another Region. This popup is invoked through the WPF Prism(MEF) InteractionRequest methodology. The structure looks like so:
PopUpUserControl
- ContentControl : Region(UserCatalogsCreateRegion)
- PopUpStageUserControl
- StackPanel
-ContentControl : Region(UserCatalogsCreateStackRegion) <--Disappearing Region
The problem manifests itself like this. When the application starts up and is running normally, I can list the Regions in the application and I can see that the RegionManager contains the Region named "UserCatalogsCreateStackRegion".
Now when I click the button that sets off the InteractionRequest for PopUpCreation, I can see that the list of Regions no longer contains "UserCatalogsCreateStackRegion". I verified that something is removing my Region because I added a CollectionListener to the Regions property of the RegionManager, and as soon as the Popup is created, my breakpoint is hit and the Notif..Action is "Remove" and the OldItem is the Region in question.
TL;DR Region disappears from RegionManager.Regions when the popup that contains said Region is created and invoked.
Any help is greatly appreciated. And I will try to answer as many other questions as possible as there is A LOT that can go wrong with a Region manager.
EDIT
Brian Lagunas' links pointed right to the doggone solution. This was the solution. My final working code for the PopUpStageControl looks like this, where ContentControl is the Region that kept "disappearing":
[ImportingConstructor]
public PopUpStageUserControl(IRegionManager regionManager)
{
InitializeComponent();
this.regionManager = regionManager;
//Fix Begin
RegionManager.SetRegionName(ContentControl, AppRegions.UserCatalogsCreateStackRegion);
RegionManager.SetRegionManager(ContentControl, regionManager);
//Fix End
RegionManager.SetRegionManager(this, regionManager);
RegionManager.UpdateRegions();
}
This is because a popup is not part of the visual tree, so the region manager will not be able to find the region. You will have to manually register the region. See these posts:
Region not loaded by the RegionManger
How to register regions inside user controls or control templates in RegionManager?
PRISM 6 Regions in WPF using DataTemplate/CustomControl
https://github.com/PrismLibrary/Prism/issues/251
Going off of my comment and then a quick Google result (old version of PRISM).
The IRegionMemberLifetime interface: Note also that the ModuleARibbonTab class implements the IRegionMemberLifetime interface. This interface is provided by Prism, and it controls whether a View is removed from a region when the user navigates away from the View.
By the sounds of it you might want to implement IRegionMemberLifetime and set KeepAlive appropriately--that might have an effect on when the RegionManager removes/persists the region.
Although you do not stated it in your posted code, I assume that you are using something like this when setting up InteractionRequest:
<prism:PopupWindowAction.WindowContent>
<inf:PopUpStageUserControl/>
</prism:PopupWindowAction.WindowContent>
So you have to be aware that at runtime Prism will replace all popup content with the one you specified in PopupWindowAction.WindowContent.
I need to build some kind of phone emulator to test our webservice and have a general idea how the flow will go.
Doesn't needs to be anything fancy.
Now I made a WPF form with a phone image in it. In the display of the phone I have placed a frame. That looks good so far :)
But what now? How can I fill the frame with say the startscreen? Do I need to create a usercontrol for each page I want to view or show? Or is there another approach?
And how about databinding? I want to use MVVM, so I need to create a viewmodel for each usercontrol like we do normally? Or a viewmodel for each page?
I just discovered the WPF Frame Control and it seems there's not so much info on the net to find, or not what I want.
Thx for any pointers in the right direction.
Here is an image how it now looks:
For each view (screen) that the end user is going to see you will need to make a page that will hold all your logic and functions per page. All you do in Cycle through the pages in your own way.
FrameName.Navigate(PageName);
Remember to initialize the pages before you can use them. You can intialize them when you use the Navigate function like:
FrameName.Navigate(new PageName());
This works well for smaller projets, bigger projects you might want to use some clever way of initializing the pages because you navigate, like:
if(TimeToLoadPage == True)
{
PageName pagename = new PageName();
RunPageNavigation()
}
public Void RunPageNavigation()
{
FrameName.Navigate(pagename);
}
You can data bind the width and height of the page by inheriting from the Frame (Going through Ancestry route). You can do all the content based stuff in a page exactly the same as a window. You can't however control the page like a window.
Hope this helps a bit more.
I am working on windows application form. I have a CustomControl (say MasterControl) on which i put a split panel and now my MasterControl is split into three parts say:
Pannel1
Pannel2
Pannel3
Now i develop three custom controls and put one in each of pannels e.g
Pannel1 have CustomControl1
Pannel2 have CustomControl2
Pannel3 have CustomControl3
Now somewhere in CustomControl3 I need to access a public member of CustomControl1. For which i wrote the following code:
((MasterControl)this.Parent)._oCustomControl1.PublicMember = this.PublicMember;
The code above doen't work in my case. When this line of code is executed in debug mode then a message box appears and states that "There is no code available for current location"
It's a really bad design for your controls to depend on how are the arranged on the parent container.
e.g. inside your third control, you are quering the property of the first one by accessing it from the parent, and then it's child control by name.
Your code will break very easily, if it can be compiled at all - I think the problem you're having is the order of compilation: in order for your parent form to be compiled, it needs to have child user controls finished. On the other hand the user controls you created need to have finished form.
It would be far better to set whatever behaviour you're after from the container of those controls - for example, by reacting to events from the control, and setting appropriate stuff on appropriate other controls (there are other ways as well ofcourse - the point is in the direction and flow of information - who's setting and using what).
If you have a split panel in your master control, you should go two levels up to find your master control:
((MasterControl)this.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;
I found the answer by myself. I am positing here because it might help some one else.
The exact code is:
((MasterControl)this.Parent.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;
Basically my coustomcontrol3 lies inside a split container panel, so when i wrote:
this.Parent then it points to Panel In which it is residing and if i wrote
this.Parent.Parent then it points to the spliter container in which above panel reside and if i wrote
this.Parent.Parent.Parent then it points to control in which this split container resides
I got the idea from "Farzin Zaker" answer, so thanks to him for his contribution
I'm working on an application that presents the user with varied data, depending on the object being viewed. The objects are all of the same interface just with extended properties beyond once distinguished.
I'm looking for the "best" way to display a type-dependent control to the user. I would like to use tabs but I'm stuck with .NET 2.0 and from what I can gather the only way to hide/show tabs are to remove them and re-add them. That might be the best way but that leads to issues regarding blinking of the GUI components, keeping tabs on the active tab when reloading, etc.
I could make custom controls for each and either have them all loaded and hide/show when necessary (which I have done in the past on projects), or dispose and re-instantiate them...
To clarify best, I would say the closest balance between code elegance and program efficiency.
I have used and have had the best luck with loading them all and then showing/hiding the ones needed.
Disposing and re-instantiating everything always made things very messy.
In order to not have load time be horrible, you can instantiate them on first use. Something like:
IView LoadView(Type dependantType)
{
// get the view or create one
IView view = GetView(dependantType);
if (view == null)
{
view = InstantiateViewAndAddToForm(dependantType);
AddView(view);
}
//
// do some binding to your model or whatever here
//
// make the correct view visible
foreach (IView v in Views)
view.Visible = v == view;
}
Could you just create a panel for each object and have a dictionary associate the object type and the panel?
You could just tell the panel to bring to front if they are all the same size, or set all Panels.Visible to be false, and just set the one you need to be true.
I have used DockPanel Suite for applications that require multiple tabs.
It is an open source project, so you can actually modify the code if you wish.
The Suite has many functions, however, if you can just use the Tabs.