"Clear" window to make room for new user controls? - c#

I'm usually working with Windows Forms Applications, but I'm currently needing Windows Presentation Foundation for design purposes.
What I am needing to do is clear out the current open window and fill it with new user controls as though a new window has been opened. In short, open a new window without actually opening a new window, similar to navigating to a new page in a web browser. (Still the same window open in the Taskbar, no extras.)
I was unsure if there was a specific class or control that made this easy to do. If someone could enlighten me on the way to do this in WPF, I would be very pleased.
Thanks.

There are a number of possible ways - here are a few that come to mind:
Navigation
In WPF, you can actually navigate to different xaml pages. In this scenario, you would define a number of pages that a main page could navigate to.
http://msdn.microsoft.com/en-us/library/ms750478.aspx
Programmatic
You can do it the old school way and just clear out all of the controls in a window. For example (in the context of a window):
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(new MyUserControl1());
stackPanel.Children.Add(new MyUserControl2());
this.Content = stackPanel;

Related

Accessing SplitView control from a different page than it's host - C# XAML Windows 10 UWP

Pre-warning, I'm new to C# and XAML, but I'm really enjoying Windows 10 UWP apps. I've got a question on how to appropriately handle a SplitView.
I've got a Main Page, in which I have a SplitView control. In the SplitView Content, I've added a Frame for navigation to other pages. I want to add the Hamburger button on the child page to open the SplitView on the Main Page, but I can't access the SplitView control from the child page. How can I make the SplitView control accessible so that the hamburger button within the sub-page can open the SplitView pane?
The alternative is to add a header in the Main Page and have a static hamburger button there, but I don't like this option as it makes handling the text header content more difficult. Another is to copy the SplitView to each page. I don't want to do this either.
Any advice would be fantastic! Thank you.
I would highly recommend you take your alternative option of including the hamburger button in the main page. Users always expect it to be in the same location every time and changing this approach will probably result in a bad user experience.
You also don't want to be repeating code and thus you don't want to recreate the button on every page as well as any additional functionality like the open/close commands.
Rather than referencing elements from one page to another, a better practice is to keep things loosely coupled. This can be done with a messenger plugin which sends an event from one page to the other which can give it instructions on what you want to do. That way the other page only has to listen for the event instead of holding strong references. To streamline some of this process you could inherit from a base class which implements the messenger functionality.
That would provide a solution to your button and your header text situations but setting them up is out of the scope of this question. Depending on the size of you app and your goals, you might like to look into existing frameworks which helps in designing maintainable apps. A good Mvvm framework I would recommend checking out is MvvmCross which also cross platform and contains a messenger plugin.
Good luck with your app.
I found that solution :
In the MainPage, in your SplitView pane button method, add a SplitView reference as parameter in Navigate() :
private void SlitViewPaneButton_Tapped(object sender, TappedRoutedEventArgs e)
{
var frame = contentFrame;
Page page = frame?.Content as Page;
if (page?.GetType() != typeof(ChildPage))
{
frame.Navigate(typeof(ChildPage), SplitViewName);
}
}
In your ChildPage.xaml.cs :
protected override void OnNavigatedTo(NavigationEventArgs e)
{
SplitView sv = new SplitView();
sv = e.Parameter as NavigateControls;
}
You can now do sv.IsPaneOpen = false, in your ChildFrame code.
Note : if you want to pass several Controls, create a Class with these Controls as variables, and use an instance as parameter.
As stated above, it is better to keep your hamburger button in your main page for a couple of reasons. One is the consistency mentioned above. Second, you would have to recreate the hamburger button in each of your content pages instead of just once in the MainPage.xaml. Additionally, keep in mind, there are different kinds of interactions with the SplitView menu in terms of how it pops in and out and how it is displayed, all listed below.
Inline – When the menu pane is opened, it pushes the content over. When it’s closed, the content goes back to its original location
Overlay – When the menu pane is opened, it lays on top of the content. When it’s closed, it is invisible.
Compact Overlay – When the menu pane is opened, it lays on top of the content. When it’s closed, the pane is still visible in Compact Mode.
Compact Inline – When the menu pane is opened, it pushes the content over. When it’s closed, the content goes back to its original position but the pane is still visible in Compact Mode.
You can also see a quick intro into the SplitView here.
http://jamesqquick.com/windows-10-splitview-intro/

Switching between forms (closing one form, then opening another)

So I'm trying to learn a thing or two about coding with c# and something i find quite annoying is the way to switch between forms.
Lets say for a game you want to go to the options panel and when you click the button to get there it closes that window(form1) and opens a new window(form2) for my app.
It doesn't look very nice having windows opening and closing like that so I'm wondering what i can do in order to make it switch from form1 to form2 without closing form1 and not open form2 in a new window (Everything switched on the main window(form1).
Might sound a bit confusing but hopefully you understand what i mean.
The code I'm using so far to switch between forms:
ChangeOptions optionchanger = new ChangeOptions ();
this.Hide();
optionchanger.Show();
You could add two panels to a single form, each of which contains the controls you would otherwise have added to one of the two forms. Then switch between the panels by changing their visibility or Z-order. This is slightly tricky in the Windows Forms Designer because you'll have to design the two panels, then position them in the same spot on the containing form.
As #ryanyuyu points out, you can set the Dock property to DockStyle.Fill and switch which panel is on top using Control.BringToFront or Control.SendToBack(). This is also a decent way to interact with the two panels in the designer, as you can switch which is on top from a context menu option.
To truly have two forms, your only option is to show a dialog. Hiding your current window is of course optional.
However, you can:
Group all the controls on a given "form" into a Panel or GroupBox, then show/hide the container control.
Put all the controls into UserControls and have an instance of each UserControl on the main form. You can then show/hide the control.
I prefer the second method as it keeps the encapsulation tighter. Since you already have two forms, its easy to convert to user controls.

Window Navigation using <Window> tag only

Is it possible to go to next window or go back to a window using just <window> tag. I searched through internet and didn't find it anywhere. Whenever navigation is needed, I found <Window.Navigation> is in use. But using but this browser like tab on top I want window to navigate when I click m Ok or cancel button on my <Window>.
P.S. I am new to WPF. So I don't know much about it.
If NavigationWindow would work for you except for the navigation tab on top, the simplest thing to do is set ShowsNavigationUI=false. Alternatively (a bigger hammer, but more flexible) you can replace the Template on your window and that will also remove it.
It is possible to navigate by using the Window class, but it depends what you want to achieve. You can assign your newly created Windows to the applications MainWindow.
EDIT: I did some tests: Other than the documentation states you cannot assign a new Window to the MainWindow property like this:
Application.Current.MainWindow = new MyWindow(); // does not work!
The main window is special, when it is closed the application will normally be closed.
However it might be easier to use the class as it has a NavigationService property which makes navigation a easier. You finde some documentation under http://msdn.microsoft.com/en-us/library/ms750478.aspx

how to launch a default browser instance within a program window?

I am creating console app, involving lots of services. I want to display a webpage within my program window. I know how to launch a new window ( http://dotnetpulse.blogspot.com/2006/04/opening-url-from-within-c-program.html )but I was wondering can I ask a browser to render a webpage withing my program window (in C#) ?
The best solution would likely be to use the WebBrowser control.
This can be placed on a form and allows the web page to appear inside your application.
Here is a nice example of how to go about implementing it http://ryanfarley.com/blog/archive/2004/12/23/1330.aspx
Hope this helps
Use the WebBrowser control and pass it the URL of the web page to render.
In your Windows form, something like (off the top of my head so may need tweaking to get it to compile):
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
this.Add(browser);
browser.Navigate("http://www.myurl.com");
In a Form you can use WebBrowser Control... in a Console Application there is no way wihout opening a new form.... but you could:
design a form with a Webbrowser Control
hide its border and showintask = false
Open it at a position in your Console-Window

Desktop applications dynamic GUI problem

I have asked the question "Is there something like master page in desktop applications?" Now I am in position that I have to extend the question. Thanks for understanding.
I have add one MDI master form into my project and several inherited forms that inherit MDI master one. I was using this code.
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Form child in this.MdiChildren)
{
child.Close();
}
Search childSearchForm = new Search();
childSearchForm.MdiParent = this;
childSearchForm.Text = "Search ";
childSearchForm.Show();
}
This code is triggered when I press some button on master form and the new in this case Search form is opened inside master.
Now my question is the right way to build desktop applications or there is some other more elegant way where content of user interface can be dynamic and switch from view to view by clicking on the buttons inside. For instance clicking on "Search" button on some search form will take you to search results grid, all that happening in one master form.
And if this is right way (which I doubt) how can I achieve to open other inside forms by clicking on buttons inside them. Also if I put some controls on masterpage they will appear two times in master form and in inherited form.
Thanks.
PS
I am using Visual Studio 2008 and MS SQL 2005.
If you are only wanting to show one view at a time you could create a new user control for each view you require. e.g One for your search results.
Then you could add a panel and clear the controls contained within the panel and add the new one to display the view you are after.
Or as described, the other option is to use a tab control, hide the tabs and set the visible index programatically.
Not sure how you would easily do this in winforms, but in WPF you could create a navigation app - a link driven navigation similar to a web browser experience but still a stand alone application. See http://msdn.microsoft.com/en-us/rampup/cc514215.aspx for details of how to get started.

Categories

Resources