The best approach to show/hide windows indpendently - wpf

I am a student and am building a C# WPF application. It has three windows:
Sign in window
Create account window
and Main application window.
I uploaded a figure to show the type of navigation I am trying to implement:
I do not think it is correct to make a window show up/hide inside the close/load event of another window.
Can someone show me the right way to implement this navigation?
Also, is it a good practice to make the three windows private properties of the application class?
The last window has a frame control to support page navigation. Again, is it better to make the three pages private properties of MainWindow application?
I am sorry if this is so obvious or easy to do.
Thanks

I would not have the three windows as properties of the application. I'd instanciate a copy of the sign-in window and use that as my central point of control.
When the user logs in, hide the sign in window, show a new main window and add a hook on the main windows Closed event.
e.g
if (logonSuccess)
{
var mainWindow = new MainWindow();
mainWindow.Closed += ReshowSignupWindow;
}
I'd also have the sign-in window do the same for the create account window. Thus, I'd have the create account window return to the signup window which would either reshow itself or start the main window if an account was created.
e.g.:
// In sign-in window, handle the create window being closed
private void CreateWindowClosedHandler(object sender, EventArgs e)
{
if (accountCreatedOK)
{
ShowMainWindow();
}
else
{
ReshowSignupWindow();
}
}
I'd probably look at having the create account window shown as a dialog window via a call to ShowDialog().
Hope that helps...

Something like this code might do it (untested, I just typed it in visual studio to autoformat the code)
The XAML is for the Login Dialog. The RegistrationDialog should be similar, except for the button and handler for the registration Button.
<Window x:Class="WpfApplication1.LoginWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="LoginWindow">
<StackPanel>
<Button IsDefault="True" Content="Submit" Click="SubmitButton_Click"/>
<Button IsCancel="True" Content="Cancel" />
<Button Content="CreateAccount" Click="CreateAccountButton_Click"/>
</StackPanel>
</Window>
//Handler of LoginWindow and RegistrationWindow
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
//Handler of LoginWindow only
private void CreateAccountButton_Click(object sender, RoutedEventArgs e)
{
this.IsCreatingAccount = true;
this.DialogResult = false;
}
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
bool isCanceled;
while (loginWin.ShowDialog() == false && !isCanceled)
{
if (loginWin.IsAccountCreationRequested)
{
if (registrationWin.ShowDialog())
{
isCanceled = true;
}
else
{
loginWin.IsAccountCreationRequested = false;
}
}
else
{
isCanceled = true;
}
}
if (loginWin.DialogResult) MainWindow.Show();
}
}

I am currently working on a Silverlight Application which is more or less similar to your application. What i feel is your can have 2 xaml controls(one for Login and other for your main application). For create account, you can use a child window which will be called from login control. And use a TabControl in your main application which will hold your other 3 xaml controls(Page1.xaml, Page2.xaml and Page3.xaml). Feel free to ask if you have any issues.
Dont forget to mark my reply as answer if it solves your problem.

I suggest you to follow the pattern. Your logic looks tightly binding with UI(user Interface) and logics.
The best pattern i like is for WPF or Silverlight is MVVM(Model View View Model). There are lot of Examples available in google for MVVM.
Just put a glance in anyone MVVM example you will be clear in developing WPF or Silverlight app.
some links are below,
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/
http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute

Related

WPF - Bring application to front from system tray

I am using System.Windows.Forms.NotifyIcon to minimize my app to system tray. I wanted to bring back the application to front when user tries to open another instance of it.
The code from this site works fine if the first instance of app is hidden behind other apps, but not when minimized to system tray.
How can I implement this with WPF?
You Can acheive this by registering click event for the notify icon, you can
private void SetSystemTrayIcon()
{
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon
{
Icon = new System.Drawing.Icon(sri.Stream),
Visible = true
};
notifyIcon.Click += NotifyIcon_Click;
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
var mainWindow = Application.Current.Windows[0];
mainWindow.Show();
}
I hope this can help you

WPF ContextMenu click routed to WinForms application

I am writing a WPF control which is hosted in an Word VSTO AddIn (WinForms). Now I have a problem with the mouse click events on a context menu.
If I click on a context menu item on the left half (the part over the WinForms app), the click goes directly to the WinForms app and my context menu does not receive the event.
If I click the right half of the item (part over the WPF form), everything works as expected.
Can someone out there help me solve this issue?
The answer from the inactive blog is:
Declare a class level dispatcher frame object
System.Windows.Threading.DispatcherFrame _frame;
Subscribe to the GotFocusEvent and LostFocusEvent for the menu:
_menu.AddHandler(System.Windows.UIElement.GotFocusEvent,new RoutedEventHandler(OnGotFocusEvent));
_menu.AddHandler(System.Windows.UIElement.LostFocusEvent, new RoutedEventHandler(OnLostFocusEvent));
Below is the implementation for the event procedures for the GotFocusEvent and LostFocusEvent:
private void OnGotFocusEvent(object sender, RoutedEventArgs e)
{
if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal (DispatcherOperationCallback)delegate(object unused)
{
_frame = new DispatcherFrame();
Dispatcher.PushFrame(_frame);
return null;
}, null);
}
}
private void OnLostFocusEvent(object sender, RoutedEventArgs e)
{
if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu)
{
_frame.Continue = false;
}
}
In my case, the if statements weren't needed and I subscribed to the events like this
<EventSetter Event="GotFocus" Handler="contextMenu_GotFocus" />
<EventSetter Event="LostFocus" Handler="contextMenu_LostFocus" />
After some deep research I stumbled upon the following bug:
https://web.archive.org/web/20101211205036/http://connect.microsoft.com:80/VisualStudio/feedback/details/432998/excel-2007-vsto-custom-task-pane-with-wpf-context-menu-has-focus-problems
It's for Excel 2007, but is still valid for other Office products (2007, 2010). I managed to fix my issue using the method described here:
https://web.archive.org/web/20151231010333/http://blogs.msdn.com/b/vsod/archive/2009/12/16/excel-2007-wpf-events-are-not-fired-for-items-that-overlap-excel-ui-for-wpf-context-menus.aspx

What event is raised on Grid.Children.Add

In my WPF application, I have a single Main window with a Grid. The Login and Shell are 2 separate UserControls added as children to a grid. I need to find out when the Shell is loaded and start a timer from the Main window.
I just need to know as to what event is raised when a UserControl is added using Grid.Children.Add method, so that I can check if Login is loaded or the Shell and start the timer.
I'm not quite sure what you're trying,
but it sounds like you're looking for the Load event:
UserControl MyControl = new UserControl();
MyControl.Loaded += new RoutedEventHandler(MyControl_Loaded);
public void MyControl_Loaded(object sender, RoutedEventArgs e)
{
if (((UserControl)sender).IsLoaded)
{
..... do something
}
}
Hope it helps

Removing a UserControl added at runtime using a button within UserControl

I have seen a few posts addressing how to remove an UserControl that has been added during runtime, but my problem is a little different. I have a UserControl that consists of an image with a small "x" button on the top right corner that is used to remove itself (the UserControl) from its parent canvas. Also to note is that the UserControl is added during runtime when the user doubleclicks on a ListboxItem. I have a Click event handler for the top right corner button but this code is not running at all. I know this because I have a breakpoint in this code which is not reached when I click the button.
So,
Why isn't the click event of the remove button being handled?
Maybe there is a better way to implement this. Please advise.
Here's the code used for adding it:
private void MyListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.OriginalSource.ToString() == "System.Windows.Controls.Border" || e.OriginalSource.ToString() == "System.Windows.Controls.Image" || e.OriginalSource.ToString() == "System.Windows.Controls.TextBlock")
{
Expression.Blend.SampleData.MyCollection.Dataset lbi = ((sender as ListBox).SelectedItem as Expression.Blend.SampleData.MyCollection.Dataset);
var new_usercontrol = new MyUserControl();
new_usercontrol.MyImageSourceProperty = lbi.Image;
MyCanvas.Children.Add(new_usercontrol);
Canvas.SetLeft(new_usercontrol, 100);
Canvas.SetTop(new_usercontrol, 100);
Canvas.SetZIndex(new_usercontrol, 100);
}
}
The following is the cs code for the UserControl:
public partial class ModuleElement : UserControl
{
public ImageSource MyProperty
{
get { return (ImageSource)this.image.Source; }
set { this.image.Source = value; }
}
public ModuleElement()
{
this.InitializeComponent();
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
((Canvas)this.Parent).Children.Remove(this);
}
}
The XAML:
<Grid x:Name="LayoutRoot">
<Image x:Name="image" />
<Button x:Name="RemoveButton" Content="X" HorizontalAlignment="Right" Height="17.834" Margin="0" VerticalAlignment="Top" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="RemoveButton_Click">
</Button>
</Grid>
Thanks in advance,
Bryan
So I tried your code here exactly except for some name changes and could not reproduce your issue. In my personal experience your issue here has to be that for some reason the event for the click isn't subscribed to properly. For this I would go into designer for the user control, wipe out the current event for the button and double click in the designer event textbox such that VS or Blend generates all the code necessary for a proper subscription.
I have created a sample based on your code here. Feel free to pull it down and take a look to see if you can find any inconsistencies.
As far as a better way to implement this, check out the good old MVVM pattern and the MVVM Light Toolkit. With this you can have a central ViewModel class that will handle all of your button commands and binding without code behind.

App xaml assumes the first window instantiated is the main window (showdialog is ignored), I need to show multiple windows

I have the following code in my App.xaml.cs
private void App_Start(object sender, StartupEventArgs e)
{
if ( CompletedInstall())
{
//using show to allow for pacifier if loading is slow
var manager = new WINServiceConfig();
MainWindow = manager;
manager.ShowDialog();
}
}
private bool CompletedInstall()
{
var window = new Initialize();
window.ShowDialog();
return window.DoLaunchManager;
}
and the following in the App.xaml
<Application x:Class="Manager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Start">
When i comment out the line that checks CompletedInstall() the manager.ShowDialog() works fine, and my configuration window shows.
When CompletedInstall() is called the call to manager.ShowDialog() returns right away without displaying the window. I added the main window on the assumption that somewhere along the line someone decided an app should only show one window.
I found a workaround by setting the main window before calling CompletedInstall
private void App_Start(object sender, StartupEventArgs e)
{
var manager = new WINServiceConfig();
MainWindow = manager;
if (CompletedInstall())
{
manager.ShowDialog();
}
but this forces me to develop WINServiceConfig (specifically the constructor) based on its use, because it cannot count on the prerequisites being completed. This is bad form. What else can i do to get around this problem?
Dummy window? That can't be the best answer. Can it??
You should set the ShutdownMode to OnExplicitShutdown (at least while showing the initial dialog).

Resources