I recently added a splash screen to my WPF app.My app loads fast so the screen is only on for milliseconds.How would i lengthen the time my splash screen stays up.I would like it to be two seconds.
If you trigger the splash screen to show in the Application.Startup Event you will have full control over it. (be sure to call .Show() with false)
private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreen screen = new SplashScreen("splashScreen.png");
screen.Show(false);
}
Then you can call screen.Close() when you would like the splash screen to close.
You can also call
System.Threading.Thread.Sleep() before the InitializeComponent in the main window. this works.
something like that:
public MainWindow()
{
System.Threading.Thread.Sleep(2000);
InitializeComponent();}
The best way and using the API is
SplashScreen splash = new SplashScreen("splashscreen.jpg");
splash.Show(false);
splash.Close(TimeSpan.FromMilliseconds(2));
InitializeComponent();
Related
I have an issue that I am trying to resolve. I have two WPF windows. Those windows house the pages with content. My problem is not navigating page to page but rather window to window. here is an example of the code:
LoginPortal Design
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
</Grid>
LoginPortal Code
public LoginPortalMain()
{
InitializeComponent();
MainFrame.Navigate(new Uri("/Pages/LoginPortal.xaml", UriKind.RelativeOrAbsolute));
}
This is my Login portal that houses 4 buttons. Each button opens the second window which is houses several pages.
private void BtnSales_Click(object sender, RoutedEventArgs e)
{
var w = Application.Current.Windows[0];
w.Hide();
MainWindow mn = new MainWindow();
mn.Content = new SalesPage();
mn.Show();
}
When this button is clicked it opens the second WPF Window which is where several pages are housed. There is no problem with navigation. The problem starts when I wire the Closing event from the MainWindow to go back to the LoginPortalMain
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
LoginPortalMain lgm = Application.Current.MainWindow as LoginPortalMain;
lgm.Show();
}
It then goes back to the start again. Now my problem is if I click on the same button again. It just duplicates everything. Then on closing anything it just adds new windows. If I then exit out of any window, it throws an error in picture below. My question is, is there a way to either dispose of the navigation once the MainWindow is closed. So there is no duplicate windows once you close the mainWindow a second time. I have used navigation before on a single window but not on multiple windows.
I may have to rethink this whole structure and use custom UserControls and figure out the animation sequence from Pages to UserControls. However, any help would be greatly appreciated.
enter image description here
enter image description here
enter image description here
enter image description here
enter image description here
If you only want to be able to open one instance of the new window try showing and hiding a single window instance.
When the open window button is pressed create the window if need and show it.
private OtherWindow mOtherWindow;
private void OpenWindowButtonClick(object sender, RoutedEventArgs e)
{
mOtherWindow ??= new OtherWindow(); //initialize child window on first call
mOtherWindow.Show();
}
When the other window is closed hide it and cancel the close request. You could also reset the other window state here for the next time it is shown.
private void OtherWindowClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
{
((Window)sender).Hide();
Application.Current.MainWindow.Activate();
e.Cancel = true;
}
I am currently trying to make a Loading form which should stay on screen for 7 seconds before switching to the main form
I cant just simply load the main form directly after the loading bar is full because the Main form takes about 7 seconds to start
Is there i way to "pre load" the main form, but hiding it until the loading form is done?
This is the code for the loading form, simplyfied
private void FormLoading_Load(object sender, EventArgs e)
{
tick.Start();
}
private void tick_Tick(object sender, EventArgs e)
{
loadingbar.Increment(1);
if (loadingbar.Value == 700)
{
this.Close();
}
}
I would add a Splashscreen - this way you do not need to use a timer for waiting until the form is loaded. You simply display the splash screen at startup and close it inside the MainWindows Constructor.
You can add a splash screen by Visual Studio -> Right click your project -> Add new item -> Wpf -> Splashscreen.
If you really want that the form does display there for 7 seconds you can use Thread.Sleep before closing the Splashscreen.
I have added a splash screen window to a mahapps metro application with a progress ring. The splash screen is shown and closed before the main window is shown using the following code.
protected async override void OnStartup(System.Windows.StartupEventArgs e)
{
//Other code
MainWindow mainWindow = new MainWindow();
SplashScreen sp = new SplashScreen();
sp.Show();
await Task.Delay(3000);
sp.Close();
mainWindow.Show();
//Other code
}
Splash screen is shown correctly but the main window’s RightWindowCommands and LeftWindowCommands are not working correctly. Styles and data binding of WindowCommands are not working. For example, a button icon on the left is not visible and a button icon on right is visible but data binding is not working.
I have created one windows form as pop up screen. I need to display that form as pop up in my windows application for few time only.
For the time being it will be shown and after some time it will be close automatically.
I need to display that form in MDI parent file as lower right corner.
How can I do that ?
This is the way you can do..It works like a Splash Screen
private void popup()
{
Thread th = new Thread(() =>
{
try
{
Open();
}
catch (Exception)
{
}
});
th.Start();
Thread.Sleep(3000); //you can update this time as your requirement
th.Abort();
}
private void Open()
{
Form1 frm = new Form1();
frm.ShowDialog(); // frm.Show(); if MDI Parent form
}
You can use a System.Windows.Forms.Timer to trigger an that the popup window processes to close itself.
There are other timer classes in the .NET Framework. This timer takes care to call the UI event handler on the UI thread.
I guess what you are looking for is a splash screen. Here's a nice article about it.
I have 2 WinForms
Form2 uses Infragistics ultraTabControl.
On Tab Changing im Showing Form1.
In Form1
I have a PictureBox assigned with animated GIF
In Form2
I am displaying Form1 like this.
Form1 frmOne=new Form1();
frmOne.Show();
Problem
GIF is not playing animation.
Make sure you have your image in the image property NOT THE BACKGROUND IMAGE PROPERTY. That's the mistake I made
It is working fine for me. I have just dragged a new pictureBox on form and setting image property at Form_Load() event and showing GIF animated.
I did same as you on button click:
TestForms.NavBarTest newForm = new TestForms.NavBarTest();
newForm.Show();
At my Test Form:
private void NavBarTest_Load(object sender, EventArgs e)
{
pictureBox1.Image = NavronChartControl.Properties.Resources.url;
}
Note: If your picture box is disabled then it will not animate the Gif
Reference:
How do you show animated GIFs on a Windows Form (c#)
Animated Progress Indicator in C# (Windows Forms)
Try to implement using this link:Animated GIF in picturebox won't animate apprach.
The best way to achieve it is to run the animation in an async task, but accordingly, some limitations are possible to do that on windows form using:
System.Threading.Thread.Sleep(int milliseconds).
My splash view is displayed with a gif (loading)
e.g.: In your constructor,
public partial class MainMenu : Form
{
private SplashScreen splash = new SplashScreen();
public MainMenu ()
{
InitializeComponent();
Task.Factory.StartNew(() => {
splash.ShowDialog();
});
Thread.Sleep(2000);
}
It is imperative to put the Thread.Sleep(int) after starting a new one, don't forget that every action you did on this thread needs to be invoked, for example:
void CloseSplash()
{
Invoke(new MethodInvoker(() =>
{
splash.Close();
}));
}
Now your gif should work!
Solved
My current Thread is busy to Play GIF Animation.
I tried So many ways like Application.DoEvents(); etc.
But every thing can't helped me.
The answer in the following Question, which uses Threading is a very great working idea.
StackOverFlow: Show/Hide Splash Screen
Thanks for every one.
This question has already raised before.
Though the approach is different but the questions concept is the same
StackOverFlow: How do you show animated GIFs on a Windows Form (c#)
Source project where you can use Code Project
I guess that will help you out
I had the same issue and came across different solutions by implementing which I used to face several different issues. Finally, below is what I put some pieces from different posts together which worked for me as expected.
private void btnCompare_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Execution);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
Here is the Execution method that also carries invoking the PictureBox control:
private void Execution()
{
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
Application.DoEvents();
// Your main code comes here . . .
btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
}
Keep in mind, the PictureBox is invisible from Properties Window or do below:
private void ComparerForm_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
If none of the previous answers work for you, another idea is to refresh the image tag or write it on the fly with jQuery.
In my case I needed to show an animated GIF when a button on the form was clicked. Previously it would show up but not animate. I just added a jQuery event when the button was clicked that added the image tag on-the-fly to a div through the parent tag's html() function.
$('.showhidegif').html("<img src='/Content/img/angled-spinner.gif' />");
<div class="col-sm-12 showhidegif"></div>
Worth a shot especially if you're already using jquery on your page.