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'.
Related
Ok, I busting my head on this for few hours now and still cannot find a solution.
first I shall explain the simple test case I created:
Solution
- ClassLibrary1
- Dictionary1.xaml
- WpfApplication3
- App.config
- App.xaml
- Dictionary2.xaml
- MainWindows.xaml
ClassLibrary1:
That project has the required references to allow me to add wpf-dictionary:
PresentationCore, PresentationFramework, Systam.Xaml, windowsbase
(Along with all standard assemblies for any regular class library)
And this is Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="PrimaryBackgroundColor">#FF030010</Color>
<SolidColorBrush x:Key="PrimaryBackgroundBrush" Color="{StaticResource PrimaryBackgroundColor}" />
</ResourceDictionary>
WpfApplication3:
This project just display a button on a wpf-form.
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button">
<Setter Property="Background" Value="{StaticResource PrimaryBackgroundBrush}" />
</Style>
</ResourceDictionary>
MainWindow.xaml:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Content="aaa" Width="100" Height="40" />
</Grid>
</Window>
That's all - very simple as you can see.
The only thing here is that dictionary2 need to use resource from dictionary1.
And so there are two ways to reference another assembly:
Option 1:
The class-library is a project in your solution and your WpfApplication adds reference to the class library project which is in the same solution. this is done via Add-Reference/Projects, And in that situation all works great.
Option 2:
The class-library is not your solution. (actually it can be like in my example)
however you add reference by adding reference to ClassLibrary1.Dll which resides either in your
bin\debug or bin\release folders.
In that situation a portal to hell is opened.
Dictionary2 complains it cannot find the resource 'PrimaryBackgroungBrush' and upon execution it crush
complaining it cannot find the dictionary1.xaml
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
and the inner exception:
{"Could not load file or assembly 'ClassLibrary1.dll, Culture=neutral' or one of its dependencies.
The system cannot find the file specified.":"ClassLibrary1.dll, Culture=neutral"}
The problem is that using option2 is essential as I want to share the same dictionary among other wpf projects without
having the ClassLibrary1 project as part of their solution.
Suggested way to reproduce:
Create a new solution in Visual studio for WPF application.
Add class library project to the solution.
In class libarary project, Add references to the following assemblies: PresentationCore, PresentationFramework, Systam.Xaml, windowsbase
Add Wpf-Dictionary 'Dictionary1' to your class library project and copy the code. (you can copy one from the wpf project since it will not exist as an option in the add item from the class library)
Add Wpf-Dictionary 'Dictionary2' to your wpf application and copy the code.
Copy the code for MainWindow.
And now:
Add reference to class library (as project, from projects tab in add refernce dialog)
Build everything - all should work.
Remove the refernce to class library.
Add reference to class library (as dll, from browse tab and find it in your classlibrary/bin/debug or release folder)
Build everything - you will notice my problem.
Any solution to this problem?
UPDATE 1
I changed the line in dictionary2.xaml from:
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
To:
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1;component/Dictionary1.xaml"/>
And now the project compiles and execute without an error, However while in design time - the xaml view of dictionary2 indicate that it cannot find the resource: 'PrimaryBackgroundBrush` and puts the ugly curly underline below it.
So its a progress - but i'm still not happy with that.
Any ideas how to solve that?
UPDATE 2
As previously stated - everything compiles and execute now.
However what you see in the following picture annoys me,
I just want to be sure that others who added the class library as .Dll file and not as project 100% sure they don't get that problem which can be seen in the picture, meaning their xaml intellisense can recognize the resource during design time.
I could imagine how documentation about that dll will looks like:
reference dll in the project
add this to resource dictionary in the project:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ClassLibrary1.dll;component/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
add this to each window/usercontrol:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Which looks afwul.
How about making manager in your library which has to be referenced by each window/usercontrol and it will do things automatically?
Here is a cut from theme manager I mentioned in comments (it does merging automatically), think about easy of use.
xaml (add this to each window/usercontrol which has to support theme switching in design/run time):
local:Theme.Theme=""
cs (this part has to be a part of library):
public static class Theme
{
public static readonly DependencyProperty ThemeProperty =
DependencyProperty.RegisterAttached("Theme", typeof(string), typeof(Theme), new PropertyMetadata(null, (d, e) =>
{
var theme = (string)e.NewValue;
// in run-time set theme to specified during init
if (!DesignerProperties.GetIsInDesignMode(d))
theme = _theme;
var element = d as FrameworkElement;
element.Resources.MergedDictionaries.Clear();
if (!string.IsNullOrEmpty(theme))
{
var uri = new Uri($"/MyPorject;component/Themes/{theme}.xaml", UriKind.Relative);
element.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });
}
}));
public static string GetTheme(DependencyObject obj) => (string)obj.GetValue(ThemeProperty);
public static void SetTheme(DependencyObject obj, string value) => obj.SetValue(ThemeProperty, value);
static string _theme = "Generic";
static string[] _themes = new[]
{
"Test",
};
/// <summary>
/// Init themes
/// </summary>
/// <param name="theme">Theme to use</param>
public static void Init(string theme)
{
if (_themes.Contains(theme))
_theme = theme;
}
}
P.S.: functionality is primitive (it is sufficient in my case), but should give you an idea.
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.
I wrote an application for Server-client connection.
In WPF project, there is an App.xaml that have start up method.
I want to do the same with an App2.xaml by copy and change the name + Startup URI for client UI
The code in App2.xaml like this
<Application x:Class="assembly_line_balance_demo_ga_dp_tttn09_2013.App2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="View/MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
But after I rebuild my project, it does not show new start up object in Properties solution.
Because Client & Server share the code inside, so I need to config both UI in one project and build to 2 different apps.
Please help
I think that it is allowed just One App.xaml per Project.
You can change the Start-Up Uri
like this:
On the file App.xaml
Remove the StartupUri attribute:
<Application x:Class="WpfApplication4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<!-- StartupUri="MainWindow.xaml" -->
<Application.Resources>
</Application.Resources>
</Application>
on the file App.xaml.cs
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
// Add this override function
protected override void OnStartup(StartupEventArgs e)
{
if(e.Args.Contains("Client"))
this.StartupUri = new Uri("View/MainWindow.xaml", UriKind.RelativeOrAbsolute);
else
this.StartupUri = new Uri("View/MainWindowServer.xaml", UriKind.RelativeOrAbsolute);
}
}
Then you can call
YourApplication.exe Client
or
YourApplication.exe Server
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.
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.