I'm working on a simple console app that just show stuff on the screen.
I got into a problem with System.Windows.Input classes, I just cant get to them.
Here is the code I'm trying:
using System.Windows.Input;
public class KeyboardHandler
{
public void UseKeyboard() => System.Windows.Input.Keyboard // Its like the class Keyboard not exsit.
}
I'm using a librery TestStack.White to do some manipulation on a window that I start from my application.
TestStack.White itself has a Keyboard class (White.Core.InputDevices.Keyboard), but could it be that this class is stopping me from accessing the System.Windows.Input.Keyboard class?
Since this is a Console Application, you'll need to add a reference to PresentationCore.dll. This assembly will not be referenced by default in a Console Application.
You can see this in the documentation for Keyboard:
Namespace: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
This is one of the main WPF assemblies, and included by default in WPF projects, but not console applications.
Related
In a Winforms application, we have developed a self updating application launcher and I'm trying to mimic the same with WPF but am facing some issues. The way this worked with Winforms:
Launcher process (with not reference to main application) will check for newer libraries and download as necessary
Launcher will then load the assembly (Assembly.Load) from an STA Thread and then via reflection, call an Init method in that assembly (that performs a bunch of init logic while the launcher is acting as a splash screen and displaying progress)
After Init is complete, Laucher will call a Handoff method via reflection in the loaded assembly which will create a new ApplicationContext with the new MainForm and then call Application.Run(newAppContext).
Launcher will close its Window
I'm trying to mimic the same in WPF but the issues I'm having:
There doesn't appear to be the concept of ApplicationContext that I could transfer the MainForm to
I'm not sure how to handle App.xaml/resources since I can't put a "second" App.xaml in the application dll, so I'm not sure how to handle loading of resources/styles
When I Show a new MainWindow in the "Handoff" call that is called via reflection, the window opens briefly but then disappears
Appreciate any guidance on how I can implement the desired behavior in a Wpf application.
WPF apps derive from a base class of System.Windows.Application. The basic template that VS uses creates a App.xaml and App.xaml.cs. This class has an static entry point of
public static void Main()
This is what I use:
var type = yourloadedassembly.GetType( "YourNamespace.App" );
type.InvokeMember( "Main", BindingFlags::Public | BindingFlags::Static | BindingFlags::InvokeMethod, null, null, null );
For anyone interesting, solving this was actually quite trivial:
Create a Wpf Application (the launcher)
Create an "Application" assembly (can be a dll) that hosts the entry point for your application specific code
Have the launcher load the application assembly dynamically (Assembly.Load)
In your application assembly, have some static entry point that can be called via reflection from the launcher.
When the launcher calls the entry point method via reflection, add your resources and new a MainWindow from the application assembly to assign to Application.Current.MainWindow:
Application.Current.Resources = new ResourceDictionary() {Source = new Uri("pack://application:,,,/MyApp.UI.Styling;component/Common.xaml")};
Application.Current.MainWindow = new MainWindow();
Application.Current.MainWindow.Show();
Back in the Launcher, Close() the Launcher window
I was wondering if we could give the open source community this logic, using which the C# Windows Form App Developers can integrate the copy and move native window box like this -
Can we even do this?
Because whatever threads I have came across while searching for this provide their own Progress Bar in Windows App.
What comes unnatural is that you have to add a reference to the assembly Microsoft.VisualBasic in your C# project.
And then add the correct namespace at the top of your .cs file:
using Microsoft.VisualBasic.FileIO;
Here is the "C#" code (that really is nothing more the a call to the correct method) to invoke FileSystem.CopyDirectory
private void button1_Click(object sender, EventArgs e)
{
FileSystem.CopyDirectory(
#"c:\src",
#"c:\dst",
UIOption.AllDialogs,
UICancelOption.DoNothing);
}
And that will generate the desired eye candy for you with having to deal with any Progressbar handling on your own.
I've been developing using C# from scratch for less than 3 months and what I got at present is a console application made with Visual Studio 2015. This application consumes a web service, the XML is deserialized, formatted and saved in an Access database, and lastly a success or error XML is sent back to the web server. 3 classes make up the application: The main stream class, a SOAP client class and a DB class.
Now the reason why I'm posting this it's because I need to get that console app integrated with a GUI in which some data gotten from the deserialized XML should be shown (this is an extension of the project that the stakeholders didn't think about before stepping on the developing stage), but I have no idea how to do it.
So far I am reading about "Building WPF Applications" and "Windows Forms" but the documentation is overwhelming and I don't know if I'm on the right path, so can you guys give some guidelines before I start wasting time coding stuff that maybe are not the best option to integrate my console application with a GUI? Is there a way to make the console application become a GUI based application?
I would appreciate if you provide me with practical links to tutorials in which they quickly implement GUIs, my time is running out and I need to start coding right now. Thank you very much for reading this and helping me.
If you're just looking for a barebones GUI and aren't too worried about it looking polished, I'd suggest you right click on your project -> add->Windows Form.
Then, turning your your Console App into a GUI based application is as simple as instantiating your Form-derived class and calling .ShowDialog().
Example:
using System.Windows.Forms;
//Note: if you create a new class by clicking PROJECT->Add Windows Form,
//then this class definition and the constructor are automatically created for you.
//Open MyFancyForm.cs, right click on the form, and click View Code to see it.
public partial class MyFancyForm : Form
{
public MyFancyForm()
{
InitializeComponent();
}
}
static void Main(string[] args)
{
var guiForm = new MyFancyForm();
guiForm.ShowDialog();//This "opens" the GUI on your screen
}
It's that simple. I suggest you add add a new class to your project as a WindowsForm instead of just a class so that you have access to the form designer. Of course, it's up to you to add fields to your form class and have it populate the GUI appropriately.
Edit: if you wish replace the Console entirely, right click on your project->properties and then change "output type" to Windows Application.
What you're trying to achieve is actually relatively simple and straight forward. You need to launch your existing console application process from a new Windows Forms or WPF application and subscribe to its input and output streams.
If you don't want to show your current console application as a visible window, remember to set CreateNoWindow to true.
var processStartInfo = new ProcessStartInfo(fileName, arguments);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.CreateNoWindow = true;
// etc.
Here's an article that goes through all the steps: Embedding a Console in a C# Application
The above article's code can be found on GitHub if you want to take a closer look. Here's an example of what sort of embedding can be done:
With the project it's easy for you to either hide the whole console or have it side to side with some custom UI controls that do further processing with your data.
You can put a TextBox inside your form and redirect anything that would go in the console to the TextBox.
see this for more info
Redirecting Console.WriteLine() to Textbox
Without entering in "why" territories, if you have a console application project you can right click the Project -> Properties -> Application and change the Output type to Class Library (or to Widows Application).
If you choose class library then you can consume your library from wherever.
One of the parameters of the Win32 API function FindWindowEx is the Class Name of the window. For example, the Class Name of Microsoft Word is "OpusApp".
If I have developed my own application, what is going to be the Class Name of the windows of the app?
Can I set this Class Name to whatever I want?
You didn't notice when you created your window, that you had to call RegisterClassEx (or plain RegisterClass)? ;)
Each window has a class. When you create your own, you specify its class.
*Edit: given your ambiguous tagging, I'm not really sure how you wrote your app. If you're using .NET then you obviously didn't have to manually call the C++ function RegisterClass.*
Regardless, when created, each window is associated with a "class", which describes certain common properties for all windows belonging to that class.
I made a nice little console app with some 3rd party libraries and stuff.
But now I want to port it to winforms.
I just copy everything from the console to a new class in the winforms, removed the main and hoped it would work.
But no.
I just get the error static types cannot be used as parameters in some of my functions.
Said functions work perfectly well in my console app. And the parameter used in the functions are all from a 3rd party mouse-control library.
What am I missing? : /
Remove static keyword from all your declarations. I'm sure that you put them there to make sure that your static main function for the console app would be able do directly access them. Now as you don't have a static main function, you don't need the static types anymore.
You could just change your existing project into a Winforms project - see How do I convert a .NET console application to a Winforms or WPF application