Root class of the solution - c#

I am trying to build a network with all the solution classes as nodes - connected to their parents (classes using them) and children (classes they use). From there I want to get the "critical paths" from the root-class(es). By critical paths I mean the paths in the network that originates at the root(s).
I know you can set a specific project in a solution as the startup project. When the solution is built and run, what class in the startup project is the root or main class than is run first? - that would be my root class.
All projects in the solution are WPF projects.

In a wpf project,by default, the first class that is called is the App class.
The class looks like this:
App.xaml.cs
public partial class App : Application
{
}
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="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
As you can see there is StartUpUri in there which contains what window will show up first.

In general this is set for each project separately and it differs depending on the technology. It is easy accessible by Visual Studio, just go to the project configuration, there in Application tab you will see Startup Object, that can be selected from those that fit the requirements. See screenshot.
For Winforms this is persisted in the App.config as:
<StartupObject>OBJECT_NAME</StartupObject>
For WPF you will find it by default in App.xaml. However this could be customized in ApplicationDefinition section of the csproj file and other files could be potentially used. For Web or WCF it will be even more different as there is no real startup object, just default page.
As you can see there is no one simple answer; however for only WPF you should be able to get a fairly complete solution. Bear in mind that there are usually class libraries projects included as well.

Related

How to reference/use Resource Dictionary between multiple projects inside solution

I am building a WPF desktop app with .net framework 4.7.2. and need some help for merging resource dictionaries.
I have 1 solution with 2 projects:
Main parent project (ProcesingDesktopHub) which only has a nav bar and depending on user click its injecting a view/viewmodel from child project (DopisiUzPovrate).
Inside the parent project I have created a ResourceDictionary.xaml with some colors etc. and I want to be able to propagade that dictionary to the child project so it can be used if I run the child project separately from the parent (testing, designing etc.)
I have tried putting this inside the child but it does not work:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://ProcesingDesktopHub:,,,/ProcResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I hope I described my question correctly.
A solution cannot have circular references between projects.
Since the parent project refers to the child, then, accordingly, the child cannot refer to the parent in any way.
One of the solutions: move the shared resources (if you need not only resources, but also types, controls, etc.) to a third project that will be referenced by the parent and child projects.
Second variant: if you do not provide for the use of the child project outside the parent project, then you can use the design-time resource creation technique described here: https://stackoverflow.com/a/17712041/13349759
And in runtime, the resources of the parent project will be used.
Design time sounds good but I would need to run the parent project for debugging purposes each time if I understood it correctly.
If you want to separately debug a child project, then creating a third project with shared resources will be the best varaint.
In addition, if you ALWAYS use the parent project only together with the child, then pay attention to the answer from #MuhammadSulaiman.
Move ResourceDictionary.xaml to child project (i.e. DopisiUzPovrate), and the structure of resources in ProcesingDesktopHub (i.e. Application.xaml) would be like this
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/DopisiUzPovrate;component/Path/To/ResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Converters, Colors and styles -->
</ResourceDictionary>
</Application.Resources>
</Application>
Update: In case you have one application (ex. ProcesingDesktopHub) and several modules (DopisiUzPovrate1, DopisiUzPovrate2, etc...), and all of these would use shared resources, then you have to create a SharedModule that hosts the shared resources and once you reference the ResourceDictionary.xaml in desktop application, it'd be available to the modules and application at runtime..

Cannot locate 'app.xaml', IOException until modifying app.xaml

Basically I'm having the same issue as this (unanswered) question: IOException was unhandled - Cannot locate resource app.xaml
When I open my project in Visual Studio 2010 and start debugging, I get "IOException was unhandled: Cannot locate resource app.xaml". Rebuilding the solution DOES NOT work, I have to somehow modify my app.xaml file (adding an empty line, for example) in order to run my project successfully.
Additional details:
My solution consists of two projects: My main (WPF) application and a test project.
I have read about issues if the solution was converted from Visual Studio 2005. This is not my case. My project was originally created with Visual Studio 2010. Maybe (I don't remind exactly) it was sometime targetted at .Net Framework 3.5 or .Net Framework 4.0 Client Profile, but it currently is configured to work with .Net Framework 4.0
Both projects have different names, if that matters. The first is called MyApplicationName (assembly name and default namespace) and the second MyApplicationName.Test (assembly name and default namespace too)
Most classes in my main project are internal (including the View classes), but my App class is public
The main project exposes its internal components to the test project (using [assembly: InternalsVisibleTo("MyOtherProject")])
I'm using MVVM (with MVVM Light), but I'm not using a ViewModel 'resolver'/container/bootstrapper/MEF or related things. I just map my ViewModels to their respective views using DataTemplates and create my ViewModels manually (if that matters).
My App.xaml includes other three xaml resource files. I include my App.xaml here:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StyleDictionary.xaml" />
<ResourceDictionary Source="Resources/CommonResources.xaml" />
<ResourceDictionary Source="Resources/ViewModelMappings.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I override the OnStartup method from my App class to manually handle the startup logic, if that matters. I also wrote a static constructor there to initialize the DispatcherHelper from MVVM Light:
public partial class App : Application
{
static App()
{
GalaSoft.MvvmLight.Threading.DispatcherHelper.Initialize();
}
...
}
Things like this could happen when you change the target framework of a project. You could recreate the projects as new applications which target .Net 4 from the start, and add all your code files as existing items.
Another thing that comes to mind is the casing of the app.xaml file name. It might be case sensitive in some contexts and not case sensitive in others, so have you tried changing that? On my system, by default it is App.xaml. Since in the error message it is app.xaml, it might be worth while doing a case sensitive search through the project files with a text editor and changing all occurences of the name to App.xaml.
I don't know if this is a definitive solution, but it seems to be working so far:
Open your csproj file with Notepad
Search App.xaml and App.xaml.cs. Ensure every reference to this file has the same case. The files were called App.xaml and App.xaml.cs, so I left the references like that.
Previously I had something like this:
<Compile Include="app.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
So I modified it and now it is:
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
Another thing I did was look for BootstrapperPackage and remove the old versions of .Net Framework from there, though I don't think that's a good idea if your project uses libraries that depend on old versions of .Net. I'm not sure that was causing the problem too.

WPF IOException Cannot locate resource

I have a WPF application.
The page that opens when the app runs in MainWindow.xaml, as set in the StartupUri attribute of the App.xaml file. This page opens fine.
However, if I try to open any other windows using the Show or ShowDialog method I get an IOException in the InitializeComponent method saying "Cannot locate resource 'Window1.xaml'" (or whatever the file is called). This happens with every single window I create. I've searched online but all the solutions seem to say "make sure the StartupUri attribute of the App.xaml is correct" and mine is, hence MainWindow opening.
Any idea what's going on?
The above did not work for me but what did work was as follows. Open up the App.xaml
<Application x:Class="dotDiff2013.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>
You then need to change the start-up URI to the fully qualified one. In my case I had moved my MainWindow.xaml to a folder called 'Main', so changing the above URI to
StartupUri="Main/MainWindow.xaml"
Solved my issue.
I had this problem when the "AssemblyName" and the "Default Namespace" on the project settings had the same value. Changing the AssemblyName to something else solved the problem.
If you open up the code-behind for the Window1.xaml file (i.e. Window1.xaml.cs), you can right click on the InitializeComponent method call and select "Goto Definition". There will be code like the following:
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/TestApp;component/mainwindow.xaml", System.UriKind.Relative);
#line 1 "..\..\..\MainWindow.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
If the Uri in the code above is not correct, then you would receive that error you got.
In addition, if the Build Action of the XAML file is not set to "Page", then you would also have that problem. To check this, you can select the file in the Solution Explorer and press F4.
Finally, if something is renaming the XAML file as part of your build process (such as obfuscation), then again you would receive that error.
Other than that, I would try a "Clean Solution" and "Rebuild Solution" to ensure the file with the InitializeComponent definition is rebuilt.
I had the same issue. The reason for me because I moved the MainWindow.xaml without adjusting the the App.xaml. If you move your MainWindow.xaml for example into a folder called "UI" you have to adjust following line in the App.xaml
StartupUri="UI/Mainwindow.xaml"
<Application x:Class="RuleSetEditor.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="/Forms/RuleEditor.xaml">
<Application.Resources>
</Application.Resources>
</Application>
here /Forms/ is my folder structure in my project. Make sure this is mentioned in app.xaml
My issue was quite trivial:
The Build Action of my file was set to "None".
If you don't set it to "Resource" this will be the exception.
This IOException can be caused by assembly name ambiguity. For example, I named an assembly myproduct.dll, then added a reference to it in the WPF app myproduct.exe. This gave the IOException for mainwindow.xaml.
If this helps anyone, I was facing this problem without any obvious problem in the resource path. One thing was that I was using this in a WPF Control Library which was then referenced by the main application assembly.
I simply changed my simple URLs (i.e. file names) to pack:// URIs as everything started to work correctly. Like this:
Source="pack://application:,,,/MyLib;component/SettingsPage.xaml"
instead of:
Source="SettingsPage.xaml"
Check in App.xaml the StartupUri tag (if you moved the MainWindow).
Also if you happen to override OnStartup(StartupEventArgs e) in your app.xaml.cs you must also have to remove the line StartUri="mainwindow.xaml" from app.xaml.
Otherwise, you will get "Cannot locate resource 'MainWindow1.xaml'" as well in this case.
Even I had the same problem, first I went on digging up the issue still it was pointing to InitializeComponent(); I finally found out that I updated Resources.resx file contents, but in my application folder I did not updated it.
So later copied the new resx file and tried it. Now it works fine.
Just anyone in case come with this issue look at this once.
To resolve this issue please go to App.Xaml and change the StsrtUpUri which you want to run when the application run.
Change the startup Uri
And if the Xaml is inside any Folder you can add as follow
StartupUri="View/MyView.xaml"
Make sure you haven't accidentally moved the file MainWindow.xaml
Mine had somehow got dragged into Views by mistake - oops
Find the file app.g.cs and edit it in Notepad, in Visual Studio it will ask you to reload the file, click OK and voila​
Same issue but yet another solution:
For me my assembly name and default namespace were the same but different from the project name. I actually updated the default namespace and assembly name to match the new project name and it fixed it.
I noticed this problem after I localised my application. I ended up with a satellite resource which I did not include in my installer script. So while it worked in Visual Studio, building the installer separately caused this problem.
Including the satellite dll did the trick.
You may have renamed your namespace globally (entire project/solution etc.) or locally, but your solution obj\Debug folder some content (xaml classes, ending w/ [original_xaml_file_name].g.i.cs) did not take your changes. Next time make sure select "Entire Solution ( Including External Items )" if this was your case.
Simply go to Build and Then "Rebuild" and "Clean"

How to build library with WPF forms

Is it possible to build a Class Library dll which also includes WPF forms?
When I try to build one I get following errors:
Error 1 Library project file cannot specify ApplicationDefinition element.
Error 2 The project file contains a property value that is not valid.
If I set my project as Windows Application it compiles & runs tho. Is there a way to get arround this? I'm using VS2010 & C# .NET 4.0 if that might be of any interest to you guys.
Thanks
Choose WPF Custom Control Library or WPF User Control Library when creating/adding new project to your solution.
Change the build action of your UserControl from ApplicationDefinition to Page.
Remove the App.xaml and App.xaml.cs in your WPF Project. Then you will be able to run the project with an output type of Class Library.
This is also occur when we copy paste an Image to ClassLibrary Project.
Change the Build Action Property of that image to resources
Extending #2493490 answer.
Right click on the app.xaml file and exclude it from the project.
Change the project output type to in project properties to Class Library.
Click on the run icon, the solution will successfully build but will not run/execute any code. A error message will be displayed explaining as such.
problems after copying XAML controls from WPF Application to class library
The first answer work for me.
I changed App.xaml from ApplicationDefinition to Page and worked

WPF - inherit from System.Windows.Application

I would like to have my own application class that inherits from System.Windows.Application. The problem is that in the Application.xaml file, I have to declare the app like this :
<src:MyBaseApplication x:Class="MyApplication"
xmlns:src="clr-namespace:MyApplication;assembly=WpfTestApplication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="FPrincipal.xaml">
<Application.Resources>
</Application.Resources>
</src:MyBaseApplication>
It works great at runtime, but when I edit the project properties, I receive the following error message by the project properties editor of Visual Studio :
An error occurred trying to load the application definition file for this project. The file '[...]\Application.xaml' could not be parsed. Please edit the file in the XAML editor to fix the error.
Could not find the expected root element "Application" in the application definition file.
The project is a VB.NET project. Somebody have a workaround? I want to keep the application.xaml file.
I've tried it with Visual Studio 2013 but I did not experience any problem. So, it should be VS 2008 specific. Did you try to reorganize your application class into a separate class library? Maybe it is a locking issue.
UPDATE
The steps I did in VB.NET VS2013 in order to make it happen:
Create the custom application class. I created it in the same assembly. My assembly name was: WPFVBTest
Public Class CustomApplication
Inherits System.Windows.Application
End Class
Modify the Application.xaml. Add the namespace reference and change the root container to the custom application class.
<self:CustomApplication x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:WPFVBTest"
StartupUri="MainWindow.xaml">
</self:CustomApplication>
Modify the code behind file: Application.xaml.vb to inherit from the CustomApplication class
Class Application
Inherits CustomApplication
End Class
Build the application. One error should occur indicating that MyWpfExtension.vb is not in sync with the application class name. This is a generated class, but not regenerated at every build. I know its cheating, but this was the only way I could manage it. So I modified it manually:
Partial Class Application
Inherits CustomApplication
This way the application builds and runs without a problem. I hope it helps you to solve your error.
I know it's not going to be a popular answer, but... why don't you just upgrade to Visual Studio 2015? Its free!
Hell, they announced VS 2017 just yesterday; also free.
Should it say < src:MyBaseApplication.Resources > ?
Applications usually points to a Window and tries to get this information from the xaml file; but you have a reference in your xaml file that Visual Studio cannot determine..
xmlns:src="clr-namespace:MyApplication;assembly=WpfTestApplication"
So VS cannot parse your xaml file, and thus gives that error...
My suggestion: override the Window, or use extensions on Application, like so :
public static class ApplicationExtensions
{
public static string GetAwesomeName(this Application)
{
return "I am an awesome application";
}
}
Use it like this:
Application.Current.GetAwesomeName();

Categories

Resources