I am trying to make a little Program were I want to open a new Window in WPF with a Button. It goes, but it like the mainWindow is closed and then another window is opening and I want ,that it goes "fluent",so you think that it is one window..
This is the code that I wrote for it:
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 w = new Window1();
this.Hide();
w.Show();
}
How would you do it?
I didn't have too much experience on WPF. But being too much similar cases I've seen before, and Windows Form & WPF, in such issue, does not have too much differences. I'm here to try to answer, with tested on my computer and see if these opinions I give may helps you.
TL;DR: see last paragraph.
There will be 3 ways (or more?) to achieve:
n windows , in which 1 is the main window
Just like the code that you have pasted to us. Hide() the main window, and Show() the sub-window.
However this is really the last option I would suggest you to use, as it would become when you have many windows to close/Hide() and to Show(). You will be resulting in not knowing how your stack of opening windows goes.
1 main window + n User Controls
This requires all the contents to be created and behaviours to be implemented inside User Controls. The main window will then be the container for the user controls to be docked into it UserControl.Dock = DockStyle.Fill.
The main window may have a menu bar docked on top, which will always be visible over different 'screens' if not otherwise be configured.
Changing the docking UserControl of window from UserControl:
C# Windows Form
this.Parent.Controls.Add(new UserControl1()
{
Dock = DockStyle.Fill
});
this.Parent.Controls.Remove(this);
WPF
The main window should contains a DockPanel named for example mainDock:
if (this.Parent != null)
{
var childs = ((DockPanel)this.Parent).Children;
childs.Add(new UserControl1());
childs.Remove(this);
}
Completely replacing MainWindow
C# Windows Form
Since the C# Windows Form thing requires a message loop to hold a WinForm application to live, choosing another option instead of Form as message loop like ApplicationContext is required.
Entry point
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(MainAppCxt.Instance);
}
MainAppCxt signleton ApplicationContext
internal class MainAppCxt : ApplicationContext
{
#region Singleton --- we need only one instance for this
private static readonly object Mutex = new object();
private static volatile MainAppCxt _instance;
public static MainAppCxt Instance
{
get
{
if (_instance == null)
{
lock (Mutex)
{
if (_instance == null)
_instance = new MainAppCxt();
}
}
return _instance;
}
}
#endregion
public MainAppCxt()
{
ReplaceMainWindow(new Form1());
}
internal void ReplaceMainWindow(Form wnd)
{
var oldMainFrm = this.MainForm;
this.MainForm = wnd;
this.MainForm.Show();
if (oldMainFrm != null && !oldMainFrm.IsDisposed)
oldMainFrm.Close();
}
}
To replace the main form inside forms, simply
MainAppCxt.Instance.ReplaceMainWindow(new Form1());
WPF
The App.xaml can still have the StartupUri be set to MainWindow.xaml for example. But creating new window will be like
var cApp = ((App)Application.Current);
cApp.MainWindow = new SubWindow();
cApp.MainWindow.Show();
this.Close();
in which to replace the "main window" role. So, if you close the window when for example SubWindow is the main one, the application will close!
I would load all of my existing content into suitable containers (Borders or Grids perhaps). I'd then set the Visible property of the first container to False, and the Visibility of the second container to True. Exactly how you achieve that setting will depend on whether you're using a view/codebehind pattern or MVVM. From the looks of things you're using the former, so it would be something like this:
In your XAML:
<Window>
<Grid>
<Grid x:Name="ContentGrid1" Visibility="Visible">
<!-- Insert the first load of content here -->
</Grid>
<Grid x:Name="ContentGrid2" Visibility="Collapsed">
<!-- Insert the second load of content here -->
</Grid>
</Grid>
</Window>
In your codebehind:
private void Button_Click(object sender, RoutedEventArgs e)
{
//Collapse the first Grid, show the second:
ContentGrid1.Visibility = System.Windows.Visibility.Collapsed;
ContentGrid2.Visibility = System.Windows.Visibility.Visible;
}
Related
I'm writing an application that uses a wizard-like series of 5 simple forms. The first form, NewProfile, is opened from a menu item on the main application, MainForm, so is a subform of MainForm. The second form, TwoProfile, is opened from a button on NewProfile. The third form, ThreeProfile is opened from a button on TwoProfile, and so on for all 5 forms. Here is the sequence:
MainForm --> NewProfile <--> TwoProfile <--> ThreeProfile <--> FourProfile <--> FiveProfile. My problem is that when any form (NewProfile, TwoProfile, ThreeProfile, FourProfile or FiveProfile) is open, I don't want a user to be able to create an instance of NewProfile.
I started out by implementing a Singleton pattern, which half-way works. It works if NewProfile is open and I go to MainForm and try to create another instance of NewProfile. It does not work if NewProfile has been destroyed, by advancing to the next form and one of TwoProfile, ThreeProfile, FourProfile or FiveProfile is open. It tells me that NewProfile.IsDisposed is true, giving me a bad reference to the Singleton instance.
What I can't figure out is how to do my logic so that NewProfile won't be created if one of TwoProfile, ThreeProfile, FourProfile or FiveProfile is open or if NewProfile itself is open.
I hope this made sense. I don't really have much code to post, except what I did for my Singleton.
private static NewProfile _instance = null;
public static NewProfile Instance
{
get
{
if (_instance == null)
{
_instance = new NewProfile();
}
return _instance
}
}
Thank you :)
As suggested in the comments, each "form" could actually be a usercontrol you swap. That way, you only have one form and multiple pages. Alternatively, you can hide the form.
If you want multiple forms instead, then you could loop through all the open forms and see if the the ones you want to check are open. If not, you can open NewProfile.
bool shouldOpenNewDialog = true;
foreach (Form f in Application.OpenForms)
{
//give each dialog a Tag value of "opened" (or whatever name)
if (f.Tag.ToString() == "opened")
shouldOpenNewDialog = false;
}
if(shouldOpenNewDialog)
np = new NewProfile();
It's untested, but it should loop through all open forms and look for any that has a Tag saying opened. If it comes across one, then it set the shouldOpenNewDialog flag to false and NewProfile won't be called.
The way that we handle this is to have a static window manager class that keeps track of the open form instances. When the user performs an action that would cause a new window to open, we first check the window manager to see if the form is already open. If it is, we set focus on it rather than creating a new instance.
Every form that is opened inherits from a base form implementation that automatically registers itself with the window manager when it is opened and removes its registration when it is closed.
Here is a rough outline of the WindowManager class:
public class WindowManager
{
private static Dictionary<string, Form> m_cOpenForms = new Dictionary<string, Form>();
public static Form GetOpenForm(string sKey)
{
if (m_cOpenForms.ContainsKey(sKey))
{
return m_cOpenForms[sKey];
}
else
{
return null;
}
}
public static void RegisterForm(Form oForm)
{
m_cOpenForms.Add(GetFormKey(oForm), oForm);
oForm.FormClosed += FormClosed;
}
private static void FormClosed(object sender, FormClosedEventArgs e)
{
Form oForm = (Form)sender;
oForm.FormClosed -= FormClosed;
m_cOpenForms.Remove(GetFormKey(oForm);
}
private static string GetFormKey(Form oForm)
{
return oForm.Name;
}
}
And you can use it as follows:
Form oForm = WindowManager.GetOpenForm("Form1");
if (oForm != null)
{
oForm.Focus();
oForm.BringToFront();
}
else
{
oForm = new Form1();
WindowManager.RegisterForm(oForm);
// Open the form, etc
}
I'm wondering whether it is possible to 'turn off' my main Window from loading automatically when my program starts with a command-line argument (i.e. when a file name is passed). The problem I have is that my program loads when a file associated with it is clicked, but does so by opening another main window and using that. The problem I have is that the program still launches the MainWindow afterwards, thus opening two Windows, one with the file contents and one that is empty.
How do I prevent the blank Window? As I see it, I either stop it from opening the main Window, close the main Window or make the program pass the file to the main Window. My problem is that I don't know which of these would be the best or how to to do it.
This is the code:
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args != null && e.Args.Count() > 0)
{
this.Properties["ArbitraryArgName"] = e.Args[0];
}
base.OnStartup(e);
if (Application.Current.Properties["ArbitraryArgName"] != null)
{
string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
MainWindow mw = new MainWindow();
mw.Show();
mw.readVcard(fname);
Application.Current.Windows.
}
}
EDIT:
My solution is at the bottom.
I believe you can add a separate class with its own Main method and set that to be the entry point of your executable. Then you can parse the method arguments there and either bring up the main window or not.
(I'm assuming this is a WPF app - it's simpler in a WinForms app as you can modify the original Main method directly.)
I assume you use WPF? You'll want to replace the entry point (Main) that WPF supplies for you. Then, you can start WPF or not depending on the command-line arguments. See this question for more info:
Replacing the WPF entry point
Remove the WindowUri from APP.XAML page.
That will not show any window. Also, add your logic on app() constructor or startup event.
I'd rewrite your code as follows:
protected override void OnStartup(StartupEventArgs e)
{
// start application window
MainWindow mw = new MainWindow();
mw.Show();
// store argument and read card info
if (e.Args != null && e.Args.Count() > 0)
{
this.Properties["ArbitraryArgName"] = e.Args[0];
string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
mw.readVcard(fname);
}
}
This assumes that the method MainWindow.readVcard(string) simply loads data into the current instance.
Hi everyone and thanks for getting back to me, sorry I've not come back sooner. Part of what Nate said was correct in that I needed to call my Window earlier and then, if there was the command-line argument, parse the file name. The issue as I saw it was that it still started up a main Window afterwards because that was set as my startup, So I used the information suggested by Qwertie to alter my app.xaml, which meant that it pointed to a different startup, which in turn meant that the Window wasn't opened unnecessarily.
In ' App : Application ' class in App.xaml.cs:
private void OnStartUp(object sender, StartupEventArgs e)
{
OnStartup(e);
}
protected override void OnStartup(StartupEventArgs e)
{
MainWindow mw = new MainWindow();
if (e.Args != null && e.Args.Count() > 0)
{
this.Properties["ArbitraryArgName"] = e.Args[0];
}
//base.OnStartup(e);
if (Application.Current.Properties["ArbitraryArgName"] != null)
{
string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
mw.Show();
mw.readVcard(fname);
//Application curApp = Application.Current;
//curApp.Shutdown();
}
else if (e.Args.Count() == 0)
{
mw.Show();
}
}
In App.xaml:
<Application x:Class="Vcardviewer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="OnStartUp"
>
<Application.Resources>
</Application.Resources>
</Application>
<!--StartupUri="MainWindow.xaml"-->
Thanks again to everyone for their answers. Regards to you all.
I edit the app.xmal to remove the start URL. I then edit the app.xaml.cs and add a constructor for App and do my processing there - I use "Shutdown()" to close the application.
You can open windows as needed. When I launch other windows, I use the OnStartup event to do it...
I am developing a project and I want to add to it a splash screen. I have checked questions here on Stackoverflow and other blogs and MSDN etc. but could not find the solution I am looking for.
I want my SplashScreen,
1- appear and stay on the screen 3-4 seconds but at the same time I want my Main Window NOT TO appear. When my splash screen fades out completely then Main Window should appear. Many of the examples I have checked out do not implement this. Even though I set SplashScreen.Close.(TimeSpan.FromMiliseconds(4000)) MainWindow still apeear immediately front or back of SplashScreen. They say "add an image to your project, make it's Build Action SplashScreen or Resource, if you want to handle fade out time go App.xaml.cs file and implement your own Main() method and put your logic." I know that already. It does not work.
2- If possible I want my splashscreen NOT TO fade out slowly. I want it to disappear suddenly.(if this is not possible or really hard for a intermediate developer to do it, it is ok. you may disregard.)
And please I want C# code not Xaml. My project is based on WPF adn .NET 4.0 client profile.
Thank you.
Why don't you make your splash screen a fully qualified XAML <window> and in your App.xaml set it up as your StartupUri:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="SplashWindow.xaml">
Then in your splash window's Load Event you initialize the main window (preferably somewhere else so the instance sticks around when you close the splash). From here you can also specify a timer for x-seconds to go off and show the main window / hide the splash window.
using System.Threading;
/// -------------------------------
private Timer t;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
App.MainWindow = new MainWindow(); // Creates but wont show
t = new Timer(new TimerCallback(CloseSplash), null, new TimeSpan(0,0,10), new TimeSpan(0,0,0,0,-1));
// Do Other load stuff here?
}
private void CloseSplash(object info)
{
// Dispatch to UI Thread
Dispatcher.Invoke(DispatcherPriority.Normal, x => CloseSplashMain());
}
private void CloseSplashMain()
{
App.MainWindow.Show()
this.Close();
}
You'll have to change your app's main window behaviour though, otherwise closing the splash window will cause the app to close.
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
App.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
}
}
Also don't forget to dispose your timer when you're done. It's an IDisposable and will keep firing that method unless it's stopped.
There are answers but I find a an easier one. Just use the Thread.Sleep(int miliSeconds) method in the main window's constructor. This will delay your app in order to open specified miliseconds later.
In the constructor of App.xaml.cs open your splash screen, wait for a few seconds, then close before proceeding with rest of the app. I am using Unity, so I close the splash screen somewhere after the Boostrapper has initialized some services.
public partial class App : Application
{
private static SplashScreen _splashScreen;
public App()
{
OpenSplashScreen();
new Bootstrapper().Run();
}
private void OpenSplashScreen()
{
_splashScreen = new SplashScreen("SplashScreen/splash.jpg");
_splashScreen.Show(false);
}
internal static void CloseSplashScreen(double time)
{
_splashScreen.Close(TimeSpan.FromSeconds(0));
_splashScreen = null;
}
}
where Bootstrapper.cs is listed below:
public class Bootstrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(Container);
// initialize some services before
App.CloseSplashScreen(0);
}
protected override IModuleEnumerator GetModuleEnumerator()
{
return new ExtendedConfigurationModuleEnumerator();
}
protected override DependencyObject CreateShell()
{
MainWindow shell = new MainWindow(Container);
shell.Show();
return shell;
}
}
The best way and using the API is
SplashScreen splash = new SplashScreen("splashscreen.jpg");
splash.Show(false);
splash.Close(TimeSpan.FromMilliseconds(2));
InitializeComponent();
I have a WPF window that I am launching from inside of a winform app. I only want to allow once instance of that WPF window to be open at a time, and not warn that user if they try to open it again.
I am having a problem however trying to search for that WPF window being open because the window is being launched from a winform. What I normaly do is when searching for a winform, I search for any instances of that winform existing in the Application.Current.OpenForms, and when in WPF I search for Application.Current.Windows
The problem I have is that System.Windows.Application.Current is null when launched from inside of a winform, so I can't search for the WPF window that way. Is there any better way of searching for an existing instance of an open window?
My Code:
if (System.Windows.Application.Current != null)
{
foreach (System.Windows.Window win in System.Windows.Application.Current.Windows)
{
if (win is frmCaseWpf)
{
MessageBox.Show("You may have only one active case open at a time.", "Open Case",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
win.WindowState = System.Windows.WindowState.Normal;
win.Focus();
win.Activate();
return;
}
}
}
Instead of searching the static application objects, you could instead just track this within your window, with a single static variable. Just keep a variable in the window:
private static frmCaseWpf openWindow = null; // Assuming your class name is frmCaseWpf
When you create a window, either in the initialize routines, or OnLoaded, depending on how you want it to work..:
partial class frmCaseWpf {
public frmCaseWpf {
this.OnLoaded += frmCaseWpf_OnLoaded;
}
private void frmCaseWpf_OnLoaded(object sender, RoutedEventArgs e)
{
if (this.openWindow != null)
{
// Show message box, active this.openWindow, close this
}
this.openWindow = this;
}
}
If you want this window to be reusable, make sure to set this.openWindow = null; when you close the window, as well.
Here's something that's working for me.
private About aboutWin;
private void AboutOpenClicked(object sender, RoutedEventArgs e)
{
if(aboutWin == null)
{
aboutWin = new About();
aboutWin.Closed += (a, b) => aboutWin = null;
aboutWin.Show();
}
else
{
aboutWin.Show();
}
}
It would be better make the frmCaseWpf class a singleton. That way you can't create another instance
Rather than try to search for a Window instance, many people use a session- (or system-) wide "Mutex" or a Mutual Exclusion lock. I was going to rewrite one for you, but I found a good codeproject article demonstrating the technique. It's not complex and very simple.
http://www.codeproject.com/KB/cs/SingleInstanceAppMutex.aspx?msg=2908697
Sneak peek:
[STAThread]
static void Main()
{
bool onlyInstance = false;
Mutex mutex = new Mutex(true, "UniqueApplicationName", out onlyInstance);
if (!onlyInstance) {
return;
}
Application.Run(new MainForm);
GC.KeepAlive(mutex);
}
Hope this helps.
(edit: of course you'll have to modify this slightly for your particular use-case, but it demos the general idea)
I am not really a 'proper' programmer, however I have achieved this in a WPF application (not from a winforms one) by using the following:
Dim wdwDetails As New detailsNew()
Private Sub openNewDetails(ByVal recordID As String)
wdwDetails.Owner = Me
wdwDetails.recordID = recordID
wdwDetails.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner
wdwDetails.Show()
End Sub
Essentially because I am creating the window object outside of the sub that opens it, there will only be a single window. Any new call to the window open sub will use the same object. But I guess that is what Thomas is referring to also.
Like I said, not sure if this will help you or not though.
You can use XXXwindown.isLoad to check if window is loaded before you create a new window:
if ( !ChildWindow.IsLoaded)
{
childWindow= new ChildWindow();
childWindow.Show();
}
I just want a c# application with a hidden main window that will process and respond to window messages.
I can create a form without showing it, and can then call Application.Run() without passing in a form, but how can I hook the created form into the message loop?
Is there another way to go about this?
Thanks in advance for any tips!
Excellent! That link pointed me in the right direction. This seems to work:
Form f = new Form1();
f.FormBorderStyle = FormBorderStyle.FixedToolWindow;
f.ShowInTaskbar = false;
f.StartPosition = FormStartPosition.Manual;
f.Location = new System.Drawing.Point(-2000, -2000);
f.Size = new System.Drawing.Size(1, 1);
Application.Run(f);
To keep it from showing up in Alt-Tab, you need it to be a tool window. Unfortunately, this prevents it from starting minimized. But setting the start position to Manual and positioning it offscreen does the trick!
In the process of re-writing a VC++ TaskTray App, in C# .NET, I found the following method truly workable to achieve the following.
No initial form dislayed at startup
Running Message Loop that can be used with Invoke/BeginInvoke as needed as IsWindowHandle is true
The steps I followed:
Used an ApplicationContext in Application.Run() Instead of a form. See http://www.codeproject.com/Articles/18683/Creating-a-Tasktray-Application for the example I used.
Set the Form's ShowInTaskbar property to true within the GUI Designer. (This seems counter productive but it works)
Override the OnLoad() method in your Form Class setting Visible and ShowInTaskbar to false as shown below.
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
I know this is old question, but it ranks well in google, so I will provide my solution anyway.
I do two things:
private void Form_Load(object sender, EventArgs e)
{
Opacity = 0;
}
private void Form_Shown(object sender, EventArgs e)
{
Visible = false;
Opacity = 100;
}
The best way is to use the following 1-2 lines in the constuctor:
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false; // This is optional
You can even set the Minimized property in the VS Property window.
You can create a class that inherits from System.Windows.Forms.NativeWindow (which provides basic message loop capability) and reference the Handle property in its constructor to create its handle and hook it into the message loop. Once you call Application.Run, you will be able to process messages from it.
I solved the problem like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Main main = new Main();
Application.Run();
//Application.Run(new Main());
}
This code resides in the Program.cs file, and you can see the original Application.Run method call commented out. I just create a Main class object (my main form class is named Main) and start application message loop w/o any parameters. This starts the application, initializes any form components but doesn't show the form.
Note: you have to have some method to get your window showing (like system tray icon, hotkey or timer or anything you might like).
public partial class Form1 : Form
{
private bool _isApplicationRun;
public Form1(bool applicationRun)
{
InitializeComponent();
_isApplicationRun = applicationRun;
}
protected override void SetVisibleCore(bool value)
{
if (_isApplicationRun)
{
_isApplicationRun = false;
base.SetVisibleCore(false);
return;
}
base.SetVisibleCore(value);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.Run(new Form1(true));
}
}
Why can't you just pass the form when you call Application.Run? Given that it's clearly a blocking call, on what event do you want to show the form? Just calling form.Show() should be enough.
Using Kami's answer as an inspiration, I created a more complete concept. If you use this solution, don't ever show the hidden window. If you do, the user might close it and then you've lost the ability to control the application exit in an orderly way. This approach can be used to manage a Timer, NotifyIcon, or any other component that is happy living on an invisible form.
using System;
using System.Windows.Forms;
namespace SimpleHiddenWinform
{
internal class HiddenForm : Form
{
private Timer _timer;
private ApplicationContext _ctx;
public HiddenForm(ApplicationContext ctx)
{
_ctx = ctx;
_timer = new Timer()
{
Interval = 5000, //5 second delay
Enabled = true
};
_timer.Tick += new EventHandler(_timer_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
//tell the main message loop to quit
_ctx.ExitThread();
}
}
static class Program
{
[STAThread]
static void Main()
{
var ctx = new ApplicationContext();
var frmHidden = new HiddenForm(ctx);
//pass the application context, not the form
Application.Run(ctx);
}
}
}
Form1 f1=new Form1();
f1.WindowState = FormWindowState.Minimized;
f1.ShowInTaskbar = false;
in the Form1 code file add this.Visible = false; to the constructor.
This will hide the window but it will flash for a sec before it is hidden. Alternatively you can write your own Application.Run command.
for more info http://social.msdn.microsoft.com/forums/en-US/winforms/thread/dece45c8-9076-497e-9414-8cd9b34f572f/
also you may want to set the this.ShowInTaskbar to false.
You should look at creating a 'service' as this is an application without a form.
See http://support.microsoft.com/kb/816169