I try to implement Splash Screnn in WPF. I have found some nice ehample in MSDN, but there is one place:
private void _applicationInitialize(SplashScreen splashWindow)
{
Thread.Sleep(1000);
// Create the main window, but on the UI thread.
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate
{
Window1 MainWindow = new Window1();
Helper.setWin(MainWindow);
MainWindow.Show();
});
}
The problem is Helper, whats the class is there and how it must be implemented. Someone could paste an example or smth?
There is an even easier way:
http://msdn.microsoft.com/en-us/library/cc656886.aspx
Add the image file to the WPF Application project. For more information, see How to: Add Existing Items to a Project.
In Solution Explorer, select the image.
In the Properties window, click the drop-down arrow for the Build Action property.
Select SplashScreen from the drop-down list
You can use code like this do display an image on startup:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup">
in the code behind:
private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreen screen = new SplashScreen("Images/splash.bmp");
screen.Show(true);
}
Related
I was working on a project using WPF, I want to modify the MainWindow to have some extended window styles. I have referred some docs and found out that I can set the window styles using SetWindowLong(), but in my case it will not work because I am trying to use WS_EX_NOREDIRECTIONBITMAP as the extended window style. when I use SetWindowLong() it does nothing. I have read somewhere that WS_EX_NOREDIRECTIONBITMAP can only be set while creating the window. So is there any way that i could modify the CreateWindowEx() of the WPF MainWindow.
I have found another alternate method to achieve this but it is by using the Undocumented API function.(SetWindowCompositionAttribute()). I need my project to be stable so is there any other method to achieve this?
If SetWindowCompositionAttribute() can set WS_EX_NOREDIRECTIONBITMAP at runtime, there must be a workaround to do this.
Here is the screenshot of what i want to achieve :
It is possible to do it in C++ but in C# WPF i haven't found a way to do this.
If you want to set WS_EX_NOREDIRECTIONBITMAP during creating MainWindow then you can create MainWindow manually. Just remove StartupUri from App.xaml and create new event handler for Startup event.
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
App.xaml.cs
void App_Startup(object sender, StartupEventArgs e)
{
// replace with your code
MainWindow window = new MainWindow();
window.Show();
}
But it would be useful if you can shore more code.
Update 3
This was actually being caused by a Post-Build action I included which uses ILMerge. See here for more details
Update2
It seems this was not directly caused by adding the command-line support, but I still don't know what did cause it. See SO question for more details.
Update
After making the below changes to allow command-line support, I cannot step through the program with this message on all my breakpoints:
The breakpoint will not currently be hit. No symbols have been loaded for this document
I checked this SO answer and found that I am missing the file Microsoft.VisualStudio.Debugger.Runtime.pdb but I have no idea where it has gone ..
Is there any reason why this would happen because of the App.xaml update?
I have a WPF application for which I need to implement command-line arguments.
Following the answer at this SO question, I amended App.xaml to remove the StartUpUri attribute:
<Application x:Class="WpfFileDeleter.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfFileDeleter"
>
<Application.Resources>
</Application.Resources>
</Application>
I then added an override method to App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
e.Args.Contains("MyTriggerArg")
{
// Do some stuff
}
}
But after inserting a break-point at the top of OnStartUp and debugging the application in Visual Studio, it just hangs in a Ready state but never actually allows me to step through the program.
I tried the following values for StartUpUri:
StartUpUri = "App.xaml"
StartUpUri = "App.xaml.cs"
StartUpUri = "App.xaml.cs.OnStartUp"
But the application just throws a "Cannot locate resource" IOException
According to ethicallogics's answer, it is enough to define startup parameter in
app_start event handler. if you delete starupuri from xaml you need to define somethingelse in sratup handler instead of that
How to start WPF based on Arguments
So App.xaml looks like this:
<Application x:Class="MonitorTool.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MonitorTool"
StartupUri="Views/SplashScreen.xaml"
Exit="Application_Exit">
</Application>
in my App.xaml.cs I got this code:
public partial class App : Application
{
private void Application_Exit(object sender, ExitEventArgs e)
{
//Some settings savin here...
}
}
When you use the XAML way for StartUp make sure the Namespace is right. I got my MainWindow.xaml in a folder called Views.
Or you create the startup like this: Startup="Application_Startup"
and create a Method in the App.xaml.cs file. Again check for namespaces to make sure everything is right here.
Since your app is building I guess this should work and you should at least reach this method.
private void Application_Startup(object sender, StartupEventArgs e)
{
MainWindow window = new MainWindow();
window.Show();
}
Note
When working with arguments you don't need to use the override OnStartup()simply do it like this:
private void Application_Startup(object sender, StartupEventArgs e)
{
string[] args = e.Args;
//Check for some value (for/foreach-loop) and do some stuff
MainWindow w = new MainWindow();
w.Show();
}
I currently have one window designed in WPF and coded in C#. I want one of my buttons to open another window, which I would also like to design in WPF. What is the best way for me to do this? Can I make multiple xaml files and call them from the same .cs class? Or should I just have one xaml file? I tried to add a new window into my xaml but it won't allow me to do that. I want all the code to be in the same C# class.
Yes, you can have multiple XAML files and call them from the same .cs file.
For exemple, let's say you have Window1.xaml and Window2.xaml. Window1 is your main window, and the code behind will look like this :
public partial class Window1 : Window
{
public MainWindow()
{
InitializeComponent();
}
}
In Window1 you have a button named btnOpenWindow. On click, you may do that to open Window2 :
private void btnOpenWindow_Click(object sender, RoutedEventArgs e)
{
var window = new Window2();
window.Show();
}
Then a new Window2 is opened.
However you won't be able to get events or others things coming from Window2 in Window1.xaml.cs, obviously you will control that in Window2.xaml.cs for exemple.
You should use the MVVM pattern in your project.
So you have different windows and just one ViewModel to handel these views and your data.
Have a look on: MVVM: Tutorial from start to finish?
I have a WPF window in a project with a XAML file and associated C# code behind file. If I set "StartupUri=MainWindow.xaml" in App.xaml to this window the window opens as expected when I start my application.
However, I want my application to to take command line parameters and then decided if it should open the GUI or not. So instead I've set "Startup=Application_Startup" in my App.xaml file which is defined as shown below.
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length > 1)
{
//do automated tasks
}
else
{
//open ui
MainWindow window = new MainWindow();
this.MainWindow = window;
window.Show();
}
}
Yet when I run this the window displayed is totally blank.
Adding window.InitializeComponent() seems to do the trick:
MainWindow window = new MainWindow();
Application.Current.MainWindow = window;
window.InitializeComponent();
window.Show();
I usually like to have a little explanation on why something does or doesn't work. I have no clue in this case. I can see that the examples online don't include InitializeComponent, and yet I produce the same exact error as you do (event without checking for args).
I created a sample application, and removed the StartupUri and set the Startup to the method you provided. Everything seems to work as expected, the content of the window is displayed, so maybe, as Daniel mentioned, you're missing the call to InitializeComponent method in your MainWindow constructor.
I am using Visual Studio 2012 C#. I have created a WPF application project with a main window and added a login window to my project. I want to change the startup window to be my login window but can't seem to do so.
I went to the properties but all I see there is Myproject.app - should it not display the forms of my project?
Anyway I have tried running the window from code as well like so :
Application.Run(new Login());
But that does not seem to work. It gives an error saying :
Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Application.Run(System.Windows.Window)'
To change startup window update App.xaml by changing Application.StartupUri:
<Application ... StartupUri="MainWindow.xaml">
To change the startup window programmatically go to App.xaml
remove the line StartupUri="MainWindow.xaml" (This will remove the default startup window configuration), now add the startup event Startup="Application_Startup", in App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
If(somecase)
{
MainWindow mainWindow = new MainWindow ();
mainWindow.Show();
}
else
{
OtherWindow otherWindow= new OtherWindow();
otherWindow.Show();
}
}
use Application.Current.Run Instead of Application.Run