How to build library with WPF forms - c#

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

Related

Item Templates Sometimes Missing in Visual Studio 2017

Using Visual Studio 2017, sometimes new item templates are missing, and it seems to be tied to the particular project to which I'm trying to add an item.
Although both show up as "C#" projects...
...in "Logging", for WPF items, I have only UserControl (WPF) as an option:
However, in "SanityCheck", I have a much-more-full list of WPF options:
The only difference I can easily see so far is that the one ("Logging") is a Class Library project while the other ("SanityCheck") is a Windows Application project. However, changing Logging to Windows Application doesn't make more options available, and I ought to be able to create a Window in a Class Library, right?
I also noticed that Logging didn't contain all the same references as SanityCheck, so I added all the ones that seemed possibly-related, and that also didn't make any difference in the set of templates available.
So how do I get my Logging project to let me add a WPF Window?
Thanks to a comment from ASh and the list referred to from this question, I determined that the cause was a missing <ProjectTypeGuids> block in the Logging.csproj file. I copied the one from the SanityCheck.csproj file and now all the C# WPF templates are available. So far I haven't encountered any negative side-effects to this solution. Time will tell.

UWP Community Toolkit with source code

"UWP Community Toolkit" is good tool.
https://github.com/Microsoft/UWPCommunityToolkit/tree/master
but If we installed it with NuGet,
We can not access source code ( I thought ).
Now, I only want to use "RadialGauge" only in this toolkit.
and I want to modify "RadialGauge" a bit..
then, I started to use RadialGauge with source code without NuGet install.
here is my step.
I open New UWP project with C# VS 2017.
I installed this code to my "Controls" folder. UWP RadialGauge Source
and I make this project.
but No display, Just white UWP windows is opened... Why ?
I need help... with my poor UWP knowledge, to resolve is not possible...
Here is project file.
https://www.dropbox.com/s/alvw6wcbiv5itsp/RadialGaugeTest.zip?dl=0
When you define a custom control, most of the time you want to give it a default style. This style needs to be placed inside a ResourceDictionary called Generic.xaml which then needs to be inside a folder called Themes.
In your case, you have only copied the code part over. To make the control show up on your page, all you need to do is go grab its default style from here, put it in a Generic.xaml resource dictionary, and then put the dictionary into a folder called Themes.

WPF Project with Custom XAML Controls

I am new to WPF coding. I have a project that has custom controls coded as xaml's with C# code-behinds. I try to import these files into another project and when I try to use these xaml controls in my main view file, Visual Studio cannot find the namespace that the imported xaml's and C# code-behinds are attached to.
I tried changing the namespace to be the same as my current project. I tried restarting Visual Studio 2013. Neither of these worked.
I tried adding the project with the custom controls to my solution. The imported project can read the custom xaml's, but my main project still can't reference them. I tried dragging the files over to the main project, and they still won't work.
I tried rebuilding the controls xaml's and C# code-behinds in my new project it still says:
The type 'local:ClickSelectTextBox' was not found. Please verify that that you are not missing an assembly reference and that all reference assemblies have been built.
This item is under the appropriate namespace and in the current project.
In visual studio go to TOOLS -> choose Toolbox Items. this will allow you either choose from an array of different components or browse around to find a dll that you would like to use.
EDIT:
Oh so you are trying to take customized xaml files that you already have written and modify them in a separate project? I would suggest one of two things.
1: right click your project name -> add existing, and add the xaml and xaml.cs file to your project at the same time.
if for some reason that does not work you can also try
2: creating a new xaml control (with the same name of the control you want to drop in) in your project and copy pasting the xaml code into that control, this should autogenerate the codebehind designer shell you are going to need. you can then go into the xaml.cs file and drop whatever business logic you are looking for. It is not the cleanest solution but sometimes the WPF editor gets a little funky when importing xaml files
I know it's an old post but still somebody may find it useful:) So! ... Make sure
1)You don't have public class outside of root namespace, check this
link
2)You are not mixing targets of your assemblies (unless you can't 100% avoid that)
For example, if you are referencing something like SQLite.Core NuGet (that has both x86 and x64 versions of SQLite.Interop.dll built in) in a project that is AnyCPU, sometimes it's easier to set application's target to x86 or x64 to solve the x86/x64 paths issues - but then you may get all these "type not found/control not built/assembly not found" and all that sorts of nonsense from Designer even despite your app builds and runs ok.
Hope that helps
Try downloading ReSharper's trial version, install it and then open up your code again. One of the awesome features Resharper has with XAML code is that it will automatically map objects to their appropriate namespace. I think this will make it easier for you and will show you a ton of ways to do things better. When I was first learning WPF it was honestly a godsend to use Resharper.

A project with an Output Type of Class Library cannot be started directly

Please can someone could explain why I get this error and what to do to fix it (or what I'm doing wrong!). The steps I have taken are
Download Source code from http://www.codeproject.com/Articles/16859/AForge-NET-open-source-framework
Opening in VS2010 shows the references cannot be found
Re-Add all 3 references from PlayingCardRecognition\bin\Release so no further warnings
When I try and build or Run I get the following message
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project
It should work!
If it did not work, be sure that you have set your start page. If your project is C# Windows Application or C# Console Application, try this:
Right click the Project name in Solution Explorer of Visual Studio
Select Properties
Select the Application tab
In the Output Type drop box
Select the correct application type of your project
Re-run your project and let me know if it won’t work.
The project type set as the Start-up project in that solution is of type ClassLibrary. DUe to that, the output is a dll not an executable and so, you cannot start it.
If this is an error then you can do this:
A quick and dirty fix for this, if that is the only csproj in the solution is to open the .csproj file in a text editor and change the value of the node <ProjectGuid> to the Guid corresponding to a WinForms C# project. (That you may obtain from a google search or by creating a new project and opening the .csproj file generated by Visual Studio to find out what the GUID for that type is). (Enjoy - not many people know about this sneaky trick)
BUT: the project might be a class library rightfully and then you should reference it in another project and use it that way.
.Exe's and .dll's are both assemblies. The key difference is that executeables define an entry point Main which can be invoked by the runtime. The error
"Class library cannot be started directly"
is due to the fact that said .dll's do not have a Main. To fix this issue, change the project type to a Windows application/Console application and define an entry point. Or, add a new project that is of type Windows application/Console application and reference said .dll.
The project is a class library. It cannot be run or debugged without an executable project (F5 doesn't work!!!). You can only build the project (Ctrl+Shift+B).
If you want to debug the code add a console application project (set it as the start up project) to the solution and add the reference to the library.
The project you downloaded is a class library. Which can't be started.
Add a new project which can be started (console app, win forms, what ever you want) and add a reference to the class library project to be able to "play with it".
And set this new project as "Startup project"
The project you've downloaded is a class library, not an executable assembly. This means you need to import that library into your own project instead of trying to run it directly.
Your project type is a class library one would suspect, add a ConsoleApplication or WindowsApplication and use that as your startup object. Reference this project and then access the code.
If you convert the WPF application to Class library for get the projects .dll file.After that convert the same project to the WPF application you get the following error.
Error:".exe does not contain a static main method suitable for an entry point".
Steps to troubleshoot:
1.Include the App.xaml file in the respective project.
2.Right Click on App.xaml file change the build action to Application Definition
3.Now Build your project
Goto the Solution properties -> on Build right side you see the startup project type. here you need to select the console appication/windows appication.
If you got this issue (got it in Visual Studio 2017 RC), and you don't get any of the things listed by Mak post from step 3 onward "4 In the Output Type drop box....", it is because you made a Class Library app when you want to create a cross platform app, so here is the solution :
1 Start a new project
2 select Visual C# and cross-platform app.
3 select cross-platform app (Xamarin and native app)
4 select blank form.
From then , right click, select as startup project and build as mentioned by Mak, and it should work.
If you can afford to start from scratch, it could do the trick as it did for me.
This could do the trick for the main issue as well, but must be adapted to your current version of Visual Studio ("Xamarin.forms portable" for visual studio 2015 for example).
Bye!
VS -> Debug -> Attach unity debugger -> double click project
Set your api project to a startup project:
Right click the api Project than choose Set as startup Project.
Just right click on the Project Solution A window pops up. Expand the common Properties. Select Start Up Project
In there on right hand side Select radio button with Single Startup Project Select your Project in there and apply.
That's it. Now save and build your project. Run the project to see the output.
_Sarath#F1
To fix this issue, do these steps:
Right click the Project name in Solution Explorer of Visual Studio
Select Set as StartUp Project from the menu
Re-run your project It should work!

What does MissingManifestResourceException mean and how to fix it?

The situation:
I have a class library, called RT.Servers, containing a few resources (of type byte[], but I don't think that's important)
The same class library contains a method which returns one of those resources
I have a simple program (with a reference to that library) that only calls that single method
I get a MissingManifestResourceException with the following message:
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Servers.Resources.resources" was
correctly embedded or linked into
assembly "RT.Servers" at compile time,
or that all the satellite assemblies
required are loadable and fully
signed.
I have never played around with cultures, or with assembly signing, so I don't know what's going on here. Also, this works in another project which uses the same library. Any ideas?
All I needed to do to fix this problem was to right-click the Resources.resx file in the Solution Explorer and click Run Custom Tool. This re-generates the auto-generated Resources.Designer.cs file.
If the .resx file was added to the project manually, the Custom Tool property of the file must be set to "ResXFileCodeGenerator".
The problem is due to a mismatch of namespaces, which occurs if you change the "default namespace" of the assembly in the project settings. (I changed it from (previously) "Servers" to (now) "RT.Servers".)
In the auto-generated code in Resources.Designer.cs, there is the following code:
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Servers.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
The literal string "Servers.Resources" had to be changed to "RT.Servers.Resources". I did this manually, but running the custom tool would have equally well done it.
I just came across this problem today, and I found this Microsoft Help and Support page that actually did work around the problem.
I had a couple delegates at the top of my file, in the global namespace, and all of a sudden I was getting a MissingManifestResourceException when running the program, on this line:
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
Then I moved the delegates into the namespace, got the same error. Finally I put the delegates in the only class in that file, and the error went away, but I didn't want the delegates in that class or namespace.
Then I came across that link above, which said
To resolve this problem, move all of the other class definitions so that they appear after the form's class definition.
I put the delegates (which I would not consider "class definitions") at the bottom of that file, outside of the local namespace, and the program didn't get the MissingManifestResourceException anymore. What an irritating error. But, that seems like a more robust solution than modifying the auto-generated code :)
I've run into a similar issue and, although I know it isn't the cause the OP had, I'll post it here so that if someone else runs across this problem in the future, an answer will be available.
If you add a class before the designer class you will get a MissingManifestResourceException exception at runtime (no compile time error or warning) because
Visual Studio requires that designers use the first class in the file.
For (slightly) more information see this post.
I had the same problem, but using the Run Custom Tool command as suggested by Timwi did not help in my case.
However it lead me into the right direction, because I ended up in the Properties of the .resx file. Here I noticed a difference to another .resx file that caused no problems.
In my case I had to change the property "Build Action" from "Resource" to "Embedded Resource".
My best guess for the reason is, that I had the .resx in a library that was used from another application. My application did not have its own .resx file, so it had to use the one from the library - which is only available when it's embedded in the library and not "stand alone".
When I run in a similar issue, in Vs 2012, it turned out that the "Custom Tool Namespace" property of the resx file was wrong (in my case, actually, it was unset, so the generated code yeld this exception at runtime).
My final set of properties for the resx file was something like this:
Build action: Embedded Resource
Copy to Output Directory: Do not copy
Custom Tool: ResXFileCodeGenerator
Custom Tool Namespace: My.Project.S.Proper.Namespace
I ran into a different cause of this problem, which was unrelated to resx files. I had a class library where AssemblyInfo.cs contained the following:
[assembly: ThemeInfo(
ResourceDictionaryLocation.SourceAssembly,
ResourceDictionaryLocation.SourceAssembly)]
The assembly did not contain any WPF code, theme or Resource dictionaries. I got rid of the exception by removing the ThemeInfo attribute.
I did not get an actual exception, only
A first chance exception of type 'System.Resources.MissingManifestResourceException'.
Viewing exception details, the system was requesting MyAssembly.g.resources
Hope this might be of help to someone else.
Also see: MissingManifestResourceException when running tests after building with MSBuild (.mresource has path in manifest)
I repeat the answer here just for completeness:
It appears adding LogicalName to the project file fixes it:
<LogicalName>$(RootNamespace).Properties.Resources.resources</LogicalName>
i.e. so the embedded resource entry in the project file looks like this:
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<LogicalName>$(RootNamespace).Properties.Resources.resources</LogicalName>
</EmbeddedResource>
</ItemGroup>
This is detailed in: http://blogs.msdn.com/b/msbuild/archive/2007/10/19/manifest-resource-names-changed-for-resources-files.aspx
Note that we are using a .resx file, but the bug still appears to occur.
Update: The problem with resources (incl. XAML) appears to be related to output paths and the use of forward or backward slashes as detailed in:
Why does modifying project output directories cause: IOException was unhandled "Cannot locate resource 'app.xaml'."
Not sure it will help people but this one worked for me :
So the issue I had was that I was getting the following message:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "My.Resources.Resources.resources" was correctly embedded or linked into assembly "X" at compile time, or that all the satellite assemblies required are loadable and fully signed"
I was trying to get the resources that were embedded in my project from another class library.
What I did to fix the problem was to set the Access Modifier in the tab Project->Properties->Resources from "Internal" (accessible only within the same class library) to "Public" (accessible from another class library)
Then run and voilà, no more error for me...
The solution given by BlaM worked for me too.
I am a VS 2013 User. After going through many fixes but no luck, I tried this:
Right-click the resource file, one-by-one, in case of multiple-files.
Make sure, the property "Build Action" is set to "Embedded Resource".
That's it! :)
I had the same issue, but in my case i places a class in a usercontrol which is related to the usercontrol like this
Public Class MyUserControlObject
end Class
Public Class MyUserCOntrol
end Class
The solution was to move the MyUserControlObject to the end of the Usercontrol class, like this
Public Class MyUserCOntrol
end Class
Public Class MyUserControlObject
end Class
I hope this helps
I was getting the MissingManifestResourceException error after I ported my project from VS2005 to VS2010. I didn't have any other classes defined in the file that contains my Form class. And I also had my resx Resource File Name set correctly. Didn't work.
So I deleted the resx files and regenerated them. All good now.
Recently ran into the same problem, struggled for a bit, found this topic but no answers were correct for me.
My issue was that when I removed main window from my WPF project (it does not have a main window), I forgot to remove StartupUri from App.xaml. I guess this exception can happen if you have a mistake in StartupUri, so in case if anybody is struggling with this - check your StartupUri in App.xaml.
Recently stumbled upon this issue, in my case I did a few things:
Make sure the namespaces are consistent in the Designer.cs file of the resx file
Make sure the default namespace of the Assembly(right click the project and choose Properties) is set the same to the namespace the resources file is in.
Once I did step 2, the exception went away.
I had this problem when I added another class in the file just before the class which derived from Form. Adding it after fixed the problem.
Also the same error may occur when you put a new class into the source code of a designer created form's class.
This new class may be removed, and placed in a different cs file.
(At least in my case this was the problem...)
Because I am pre-compiling my web application (using VS2012 publish feature). I was getting the error above. I tried all the suggestions, but weirdly changing 'Build Action' to 'Content' did the trick!
In my case, I have a web api with resources and I create a nuget package from that. When I use this nuget in other projects, I realise that when I request a api with resources, I am getting MissingManifestResourceException after a bit reasearch, I learn nuget packager is not packing resources automatically. If you want to use resources files, you have to do that manually. So you need to add below lines to your .nuspec file:
(Visit https://github.com/NuGet/Home/issues/1482)
<package>
<metadata>
</metadata>
<files>
<file src="bin\Debug\en\MyAssembly.resource.dll" target="lib\net40\en\MyAssembly.resource.dll" />
<file src="bin\Debug\es\MyAssembly.resource.dll" target="lib\net40\es\MyAssembly.resource.dll" />
</files>
</package>
But, before adding files, you need to be sure which version of .net you are using.
I had the with a newly created F# project.
The solution was to uncheck "Use standard resource names" in the project properties -> Application -> Resources / Specify how application resources will be managed.
If you do not see the checkbox then update your Visual Studio! I have 15.6.7 installed. In 15.3.2 this checkbox is not there.
Just to mention. If you use a constant or literal, make sure it refers to a resource of the form ProjectName.Resources, and does not cpntain Resources.resx.
It could save you an hour or two .
I've encountered this issue with managed C++ project based on WinForms after renaming global namespace (not manually, but with Rename tool of VS2017).
The solution is simple, but isn't mentioned elsewhere.
You have to change RootNamespace entry in vcxproj-file to match the C++ namespace.
In my case it was a typo in the Xaml of a window opened from Winforms Form:
Incorrect: <Image Source="/Resources/WorkGreen.gif"/>
Correct: <Image Source="../Resources/WorkGreen.gif"/>
It may help someone
In my case I have changed my project namespace and hence my solution was throwing "missingmanifestresourceexception" exception. Instead of right clicking the .resx file in the solution explorer and clicking on "Run Custom Tool" option, I have replaced the
rootnamespace to new namespace in .csproj file(RootNamespace) and rebuilded the solution again. All Resources.Designer.cs files namespaces got automatically changed with new namespace.
I hope my answer will help someone.
If you're getting this while generating a C# project using CMake, the solution I found may help you.
Your CMakeLists.txt file needs
set_property(TARGET yourTargetName PROPERTY VS_GLOBAL_RootNamespace yourRootNamespace)
Substitute your own values for yourTargetName and yourRootNamespace, obviously.
Then the resources will get embedded in your assembly!
One more reason to get this error is- '.resx' file excluded from project.
In my case, '.resx' file was excluded from project.
Select 'show all files' option in solution explorer.
Right click on '.resx' file(s) and click include in project.
Rebuild the project/solution.
I read all the answers and nothing worked for me. Most likely my situation is different, but same error. My issue was that I had two projects. Second project had a lot of forms added to it from the first one as "Add as link".
For WinForms, there are 3 required files: the code, the designer, and the resource files. If you add all 3 files at the same time as "Add as link", Visual Studio does not link them together as same form. It will compile, and run, but it will blow up with the same MissingManifestResourceException error.
Fix: You have to do them individually, in order: code file --> designer file --> resource file. Then they are grouped and no more error, at least for me.
From the Microsoft support page:
This problem occurs if you use a localized resource that exists in a satellite assembly that you created by using a .resources file that has an inappropriate file name. This problem typically occurs if you manually create a satellite assembly.
To work around this problem, specify the file name of the .resources file when you run Resgen.exe. While you specify the file name of the .resources file, make sure that the file name starts with the namespace name of your application. For example, run the following command at the Microsoft Visual Studio .NET command prompt to create a .resources file that has the namespace name of your application at the beginning of the file name:
Resgen strings.CultureIdentifier.resx
MyApp.strings.CultureIdentifier.resources

Categories

Resources