WPF window is not opening? - c#

i am working with wpf application ,i have changed my entry point Main() to another file ,there is no error but MainWindow.xaml is not opening .Code is :
class AppStart : Application
{
[STAThread()]
static void Main()
{
//Console.WriteLine("New entry point added");
new App();
}
public void App()
{
StartupUri = new System.Uri("MainWindow.xaml", UriKind.Relative);
Run();
}
}
Can anyone explain what could be the reason?

Solved it i was using wrong constructor name ..it should be AppStart() but i was writing App()..

Related

Launching a WPF window from a Console application

I'm trying to use a WPF window as a message popup that will close once a task has been performed. All the documentation I've seen says that this can't be done with a messageBox, that's why I'm going with the WPF. I found one code snip that allowed me to open the WPF window but it wouldn't progress the application to the next process. Below is the last code example I found that I thought showed promise but the window isn't opening -
[STAThread]
static void Main(string[] args)
{
try
{
string filePath = "my new directory";
var popup = new PopupTest();
popup.Dispatcher.BeginInvoke
(System.Windows.Threading.DispatcherPriority.Normal,
(Action)(() =>
{
popup.Show();
}));
// Do some console application stuff
do
{
Directory.CreateDirectory(filePath);
} while (!Directory.Exists(filePath));
popup.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
The cs.xaml file is just the default
/// Interaction logic for PopupTest.xaml
/// </summary>
public partial class PopupTest : Window
{
public PopupTest()
{
InitializeComponent();
}
}
I feel like this should be simpler than I'm making it. Anything that can point me in the right direction is appreciated.
You need to reference the WPF assemblies and create and run a System.Windows.Application on the STA thread:
[STAThread]
static void Main(string[] args)
{
var app = new System.Windows.Application();
app.Run(new PopupTest());
}
The Run method blocks and doesn't return until the app is shut down.
If you want to do some stuff while the app is running, you need to do this on another thread:
[STAThread]
static async Task Main(string[] args)
{
Task t = Task.Run(() =>
{
string filePath = "my new directory";
do
{
Directory.CreateDirectory(filePath);
} while (!Directory.Exists(filePath));
});
var app = new System.Windows.Application();
app.Run(new MainWindow());
await t;
}

WPF has no entry point

I'm using SharpDevelop and had by my bad moved the App.xaml to an Subdirectory.
When i try to start/debug the Application, C# says, that my Application has no entry points or a static main method (CS5001).
An Edit < Undo or an movint to the default main folder will be not working.
Whats wrong?
Edit
On Project-Settings, no Classes/Methods are listened:
App.xaml
<Application x:Class="SongManager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Boot">
</Application>
App.xaml.cs
using System;
using System.Windows;
using SmuleTools;
namespace SongManager {
public partial class App : Application {
private Account user;
public App() {
}
public Account getAccount() {
return this.user;
}
[STAThread]
private void Boot(object sender, StartupEventArgs e) {
Login login = new Login();
login.AuthSuccess((Object result) => {
this.user = (Account) result;
Manager window = new Manager(this);
window.Show();
login.Close();
});
login.Show();
}
}
}
The Solution is a little bit tricky, but easy.
After moving the main .xaml file, the Build action will be lost - These are not in the Project/Compiler settings!
Step-by-Step:
(red mark) Click on your .xaml file (for sample, App.xaml)
(blue mark) Go to the Properties window (on the right side!)
(green mark) Change Other > Build action to ApplicationDefinition
Screenshot
That is it!
Try to add a Main() method to your App class:
[STAThread]
public static void Main()
{
App application = new App();
application.Run();
}
By default, there should be one generated when you build the application.

Why Window.ShowDialog runs the application?

I try to open dialog window before the runnig wpf application:
public class Program
{
[STAThread]
public static void Main(string[] args)
{
var app = new App();
var win = new MainWindow();
if (win.ShowDialog().GetValueOrDefault())
{
app.Run();
}
}
}
class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var win = new Window1();
win.Show();
}
}
Why does win.ShowDialog() run the app (app.OnStartup is executed)?
But, win.Show() doesn't run the app
[STAThread]
public static void Main(string[] args)
{
var app = new App();
var win = new MainWindow();
win.Show();
app.Run();
}
Why is this behavior???
Thanks
ShowDialog starts its own message loop. It does almost the same thing as Application.Run, and since you already created an instance of the application, the startup message (well, dispatch) has already been sent, and will be interpreted by the message loop (dispatcher) in ShowDialog. Show basically only sends a message to the queue, so it doesn't do anything unless there's a message loop processing the messages.
If you want to show a dialog before the startup is registered, don't create the application instance before showing the dialog.

Windows Explorer Context Menu click function is not working

I want to let the users to select only the following extension files: .jpg,.png, .tiff, .gif, .png. using Windows Explorer Context Menu I followed this link:http://www.codeproject.com/Articles/15171/Simple-shell-context-menu?msg=4779433#xx4779433xx and I could register and un-register successfully for .jpg file.
When I click on the command fileCopytoDirAnothing is happening i.e the function is not working. (I followed the same approach using console application with my function it works).
Where & How should i call the function during the 'fileCopytoDirA click?? Any help?
![enter image description here][1]
Code to register in the registry:
InitializeComponent();
string menuCommand = string.Format("\"{0}\" \"%L\"", Application.Current);
FileShellExtension.Register("OISjpegfile", "fileCopytoDirA", "fileCopytoDirA", menuCommand);
Function to be executed during click:
static void fileCopytoDirA(string filePath)
{
try
{
File.Copy(filePath, System.IO.Path.Combine(#"C:\Test\Directories\", System.IO.Path.GetFileName(filePath)), true);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
return;
}
}
Function to un register the registry entries during WPF application close:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
FileShellExtension.Unregister("OISjpegfile", "fileCopytoDirA");
}
[1]: http://i.stack.imgur.com/eAN5F.png
Edit afterMetadingsanswer:
App()
{
InitializeComponent();
string menuCommand = string.Format("\"{0}\" \"%L\"", System.Reflection.Assembly.GetExecutingAssembly().Location);
FileShellExtension.Register("OISjpegfile", "fileCopytoDirA", "fileCopytoDirA", menuCommand);
}
[STAThread]
public static void Main(string args)
{
if (string.IsNullOrEmpty(args))
{
// Run your Main Form
// (blocks until Form1 is closed)
Window3 window = new Window3();
App app = new App();
app.Run(window);
}
else
{
// Run the context menu action
fileCopytoDirA(args);
}
// exit
}
static void fileCopytoDirA(string args)
{
try
{
File.Copy(args, System.IO.Path.Combine(#"C:\Test\Directories\", System.IO.Path.GetFileName(args)), true);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
return;
}
}
The (simple) registry setting is that your application is executed by arguments, as it would be called in a console:
app.exe "TheJpegFile.jpg"
So the entry point is static void Main(string args), from there you can call fileCopytoDirA(args). There is no magic way Explorer calls a function by its name. You can either implement the COM interfaces, as for example this project does, or you go the quick and dirty way, by redirecting your Main; if there is not an argument, run the (windows forms) application - if there is an argument, do the action and exit:
using System;
using System.Windows.Forms;
public static class Program {
public static void Main(string args) {
if (string.IsNullOrEmpty(args)) {
// Run your Main Form
// (blocks until Form1 is closed)
Application.Run(new Form1());
}
else {
// Run the context menu action
fileCopytoDirA(args);
}
// exit
}
}
The FileShellExtension.Register function is defined as
public static void Register(string fileType,
string shellKeyName, string menuText, string menuCommand)
So the arguments are
string fileType - the HKC registry key for the file extension
string shellKeyName - just a registry key name for Explorer to distinguish shell extensions
string menuText - what the user can see in Explorer's context menu
string menuCommand - the shell command Explorer executes just like you can do in a console or by a link
P.S: In WPF it's similar, but you create new YourApp class (derived from System.Windows.Application) and then call Run.
Assuming Application.xaml looks like
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
</Application>
and your application class is in namespace WpfApplication1 named App, and you have a Window1.xaml, the q'n'd looks like
namespace WpfApplication1
{
public partial class App : Application
{
App()
{
InitializeComponent();
}
[STAThread]
static void Main(string args)
{
if (string.IsNullOrEmpty(args)) {
// Run your Main Form
// (blocks until Form1 is closed)
Window1 window = new Window1();
App app = new App();
app.Run(window);
}
else {
// Run the context menu action
fileCopytoDirA(args);
}
// exit
}
static void fileCopytoDirA(string args) {
// this your part ;)
}
}
}
Btw. I took the WPF Main part from this source and it seems to be important that you remove the StartupURI="Window1.xaml" attribute from your Application.xaml, that it looks now like
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

Splash screen appears everytime the project's starting window is invoked..

I have implemented a splashscreen for my project, it works well as desired.. but in my project i have an option of logout for user,this displays start page where a different login is provided(which is the starting screen..i.e, "chooselogin.xaml"). So when the user clicks on "choose a different login" while he already selected one in the application.. again the splashscreen appears, which is not required and looks odd.
the following code is what i think leading to problem... guys
public partial class Chooselogin : Window
{
public Chooselogin()
{
new SplashWindow().ShowDialog();
InitializeComponent();
}
......
This code is my "App.xaml"..
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Chooselogin.xaml">
<Application.Resources>
<ResourceDictionary Source="/Themes/ExpressionDark.xaml"/>
</Application.Resources>
The splash screen code is as follows..
public partial class SplashWindow : Window
{
Thread loadingThread;
Storyboard Showboard;
Storyboard Hideboard;
private delegate void ShowDelegate(string txt);
private delegate void HideDelegate();
ShowDelegate showDelegate;
HideDelegate hideDelegate;
public SplashWindow()
{
InitializeComponent();
showDelegate = new ShowDelegate(this.showText);
hideDelegate = new HideDelegate(this.hideText);
Showboard = this.Resources["showStoryBoard"] as Storyboard;
Hideboard = this.Resources["HideStoryBoard"] as Storyboard;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
loadingThread = new Thread(load);
loadingThread.Start();
}
private void load()
{
Thread.Sleep(1000);
this.Dispatcher.Invoke(showDelegate, "Loading assets...please wait");
Thread.Sleep(2000);
//do some loading work
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(2000);
this.Dispatcher.Invoke(showDelegate, "Loading profiles..");
Thread.Sleep(2000);
//do some loading work
this.Dispatcher.Invoke(hideDelegate);
Thread.Sleep(2000);
this.Dispatcher.Invoke(showDelegate, "Loading Data... almost done");
Thread.Sleep(2000);
this.Dispatcher.Invoke(hideDelegate);
//close the window
Thread.Sleep(2000);
this.Dispatcher.Invoke(DispatcherPriority.Normal,
(Action)delegate() { Close(); });
}
private void showText(string txt)
{
txtLoading.Text = txt;
BeginStoryboard(Showboard);
}
private void hideText()
{
BeginStoryboard(Hideboard);
}
}
The splash screen is supposed to be opened at start of application.. please help guys..
How about something simple like this?:
public partial class Chooselogin : Window
{
private static bool isFirstTime = true;
public Chooselogin()
{
if (isFirstTime)
{
new SplashWindow().ShowDialog();
isFirstTime = false;
}
InitializeComponent();
}
...
}
Now it will only display the splash screen once.
I recommend reading this post by Kent Boogaart
Example from the post
"WPF provides a SplashScreen class. It is simple by design and addresses the main goal of splash screens: immediate feedback. By virtue of forgoing the WPF stack and instead relying on Windows Imaging Component (WIC) to display images, it provides the quickest path to getting a splash on the screen short of writing your own native bootstrapper."

Categories

Resources