Is there a WinForms way to do Application.Activate()? - c#

In a WPF-Application you can simply do this:
<Application x:Class="Activate.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Activate"
Activated="App_OnActivated"
Deactivated="App_OnDeactivated"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
The intresting lines are:
Activated="App_OnActivated"
Deactivated="App_OnDeactivated"
Is there a way to get the same effect with a WinForms-Application?
Edit
It seems I have not explained myself well, so here is an example project which I originally made for a collage.
Please try to note the difference between switching to a different application and just switching windows inside the application.
https://www.file-upload.net/download-13074329/Activate.zip.html

On the form itself, you've got the following events available:
this.Activated += new System.EventHandler(this.Form1_Activated);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);

Related

XamlParseException when attempting to open a WPF Window from Winforms

I have a single Winforms project and multiple WPF projects in one solution. From the Winform application I'd like to open one of the WPF Windows (it's a MetroWindow from Mahapps, if it matters).
After looking at the accepted answer to this stackoverflow question I ended up with this piece of code:
OpenWPFAppButton_Click(object sender, EventArgs e)
{
WPFApp wpfApp = new WPFApp();
ElementHost.EnableModelessKeyboardInterop(wpfApp);
wpfApp.Show();
}
Unfortunately if I click the button a XamlParseException occurs, which points to the first Style="{StaticResource ... }" line in WPFApp.xaml (the Main Xaml File).
Does this mean I cannot open WPF Windows from Winforms that include static resources? Or am I missing something simple here?
EDIT: Here is the content of the App.xaml file:
<Application x:Class="WPFAppProjectName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
StartupUri="WPFApp.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/ButtonStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/MenuStyles.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/TextBoxStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
It is most likely that some common resources are defined in the App.xaml. That file isn't loaded when running through Windows Forms and thus those resources are unavailable. Hence you get this error.
You could include the resource definitions in the Window.xaml files, or an own common style resource file (aka Resource Dictionary) which is included in every Window.

wpf classes global?

IM not a C# programmer by nature but I do ok with it for what i need. Im trying to Learn WPF and my question is, Can I Declare my classes in App.xaml and access it from all of my separate controls? Im primarily interested in this because I would like ot be about to have one LogWriter and let all of them talk to it.
<!-- App.xaml -->
<Application x:Class="MyAPP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:LogWriter"
StartupUri="MainWindow.xaml">
<Application.Resources>
<src:LogWriter x:Name="LogWriter"/>
</Application.Resources>
</Application>
Set x:Key, use StaticResource to reference. Application.Resources are accessible anywhere in the application.

Create Application from code and load resource dictionnary from xaml

This might be an extremely dumb question, but I simply can't understand the problem at the moment.
I have an App.xaml file that defines application level resources :
<Application x:Class="WpfMPManager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionnary.xaml"/>
</ResourceDictionary>
</Application.Resources>
For various reasons, I start this application through a .cs file like that (there actually is a lot more going on in this file).
[STAThread]
static void Main(params string[] args)
{
App myApp = new App();
MainWindow myWindow = new MainWindow();
myApp.Run(myWindow);
}
However, when I start the application in this way, my application resource dictionnaries are empty (and they are indeed filled if I start the application through my App.xml).
Should I call a specific method on the Application object to force it to load the resource dictionaries defined in the .xaml file ?
Thanks in advance.
Answering my own question in case anyone stumbles on that
It seems it's enough to call myApp.InitializeComponent() which is not called by the default constructor. Could have figured it out faster.

Window implemented in subfolder not avaiable for StartUri

A novice question about WPF.
I'm just at the beginning of a draft project.
I have define a really simple window testWindow1.xaml, located in the solution subfolder Tests.
In App.xaml I cannot do:
StartupUri="testWindow1.xaml"
(unless I move the testWindow1.xaml back to the root of the project)
I have also tried defining my namespace into the App.xaml tag, but without success, this wont work either.
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myprojectNS="clr-namespace:MyProject"
StartupUri="myprojectNS.tests.testWindow1.xaml">
At run time, the exception message complains about not finding the ressource *testWindow1.xaml
Try this -
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myprojectNS="clr-namespace:MyProject"
StartupUri="tests\testWindow1.xaml">
You just need to specify the hierarchy.

Expression Blend 4 accidently deleted App.xaml

Im new to Expression and by accident I deleted the App.xaml file. I think this is an important file and I cannot workout how to create an equivalent.
Please help,
Andy
Create a new project and copy that one.
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Unless you had Application Resources defined, then you may be in trouble.
Might be a good argument for Source Control.
You can create a new Page and call it App.xaml.
Replace its markup as benPearce indicated with this:
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Replace "Test" above with your namespace and "Window1" with the name of the first page you want shown in your project.
Replace the class in the App.xaml.cs code-behind with this:
public partial class App : Application
{
[STAThread]
public static void Main()
{
YourNamespace.App app = new YourNamespace.App();
app.InitializeComponent();
app.Run();
}
}
Ensure that your Project Properties are set in the "Application" area such that your startup object is YourNamespace.App.
Perform a build and you shouldn't get anymore errors related to 'App'.

Categories

Resources