I'm adding a new WPF project to an existing Visual Studio solution and would like to reuse a bunch of code (C# and xaml) from an existing project within the solution.
I've created the new project and added existing files as follows:
Right click project
Add -> Add Existing Item
Find the file to reuse, use the arrow next to "Add" and "Add as Link"
I now have a nice project set up with all the proper links. However, XAML chokes on these links. For example:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Resources\Elements\Buttons\Buttons.xaml" />
<ResourceDictionary
Source="Resources\Elements\TextBox\TextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
The files "Buttons.xaml" and "TextBox.xaml" exist as links in my new project. The project builds, but when I run, I get the following XamlParseException:
'Resources\Elements\Buttons\Buttons.xaml'
value cannot be assigned to property
'Source' of object
'System.Windows.ResourceDictionary'.
Cannot locate resource
'resources/elements/buttons/buttons.xaml'.
It seems like the XAML parser is requiring an actual copy of these XAML files to exist in my new project, instead of links.
This is exactly what I'm trying to avoid. I want my project to share these files so that any changes get transferred to the other project without hunting and copying.
Any insight is appreciated!
Linking to an external XAML file does not create a file where the link exists in a project structure, as you have noticed.
My advice is to use relative links in the MergedDictionaries references. If the XAML to reuse in another project is called Common, the Source property of the first nested ResourceDictionary could be:
..\Common\Resources\Elements\TextBox\TextBox.xaml
which is actually the path that you used to add the existing item.
Related
What should the build action for a resource dictionary? In WPF it's Resource build action for ResourceDictionary file, but in UWP there no such a build action, and googling didn't help so far.
I tried several options like Embedded Resource and so on but all of them just cause different exceptions.
The project works without using the ResourceDictionary file and there is just one simple style in it.
For ResourceDictionary the setup should look as in this screenshot:
So the Build Action is Page and the Custom Tool should be XamlIntelliSenseFileGenerator.
If you are still getting an exception, ensure that the XAML code is valid or alternatively right-click a folder in your project, select Add > Add new item..., select XAML category on the left and then Resource Dictionary template and let Visual Studio create the correct file for you.
This may well be a stupid question, but I cannot find an answer...
I am just getting started with WPF, and am trying to add a ResourceDictionary to my project.
this one here:
https://monotone.codeplex.com/
So I downloaded the zip file, and unzipped it to:
MyProjectDir/MonoTone
I have added the following to my App.xaml
<Application x:Class="TestWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWPF"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Monotone/Monotone.Colors.xaml" />
<ResourceDictionary Source="Monotone/Monotone.Brushes.xaml" />
<ResourceDictionary Source="Monotone/Monotone.MahApps.xaml" />
<ResourceDictionary Source="Monotone/Monotone.xaml" />
<ResourceDictionary Source="Monotone/Monotone.ExtendedWPFToolkit.xaml" />
<ResourceDictionary Source="Monotone/Monotone.ColorBox.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
as here:
https://monotone.codeplex.com/wikipage?title=Installing%20Monotone&referringTitle=Documentation
Now Intelsense is underlining the xaml paths, and cannot find the files.
I have added a reference to the dll. What am i missing?
Thank you.
First, try including the Monotone catalog in the solution by clicking on show all files icon in solution explorer.
Second, as shown in below screen, pinpoint the exact path as in the first line of example.
To explicitly answer your question: Visual Studio was complaining because you needed to add the Monotone files (from the release zip) to your project under a folder called "Monotone".
Janis S's answer already stated this.
Unfortunately, the Monotone project contains a few dependencies on other projects... Specifically, you'll notice that it references the ColorBox control, which can be found on CodePlex, and it also depends on some Xceed assemblies. Your project will not build without those.
A few examples of the references to external dependencies are:
xmlns:xtk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:nc="http://schemas.ncore.com/wpf/xaml/colorbox"
xmlns:conv="clr-namespace:Xceed.Wpf.Toolkit.Converters;assembly=Xceed.Wpf.Toolkit"
xmlns:Behaviours="clr-namespace:MahApps.Metro.Behaviours"
xmlns:mm="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mmm="http://metro.mahapps.com/winfx/xaml/shared"
At this point you have three options
remove all references to the external assemblies.
OR figure out which assemblies are needed and add references to those as well.
OR find a different dark theme to use.
Option 3 is probably the best one at this point as CodePlex is shutting down soon and Monotone does not appear to be maintained. A quick Google search results in a few free WPF Dark Themes that are still functioning that could be used instead.
Edit
If you really want to use Monotone, go to their downloads page and grab the sample application. All of the missing DLLs are included in that download (you will need to include those in your project and add them as references).
I might be very late to the party, but to clarify things:
I am the creator of Monotone (which was a theme I used for an IDE I developed which is now obsolete). I don't mind if you pick another/better dark theme for your application, it's pretty old at this point.
Monotone is now 'maintained' on GitHub. Well, it's more or less stale at this point since I'm not into C# anymore. But if you (or anyone else) have a specific problem with it, feel free to submit an issue on GitHub or a pull request that solves it.
You only need to add the XAML-files for Monotone.Colors.xaml, Monotone.Brushes.xaml and Monotone.xaml to the ResourceDictionary.MergedDictionaries for the default WPF controls. Include in this order. If I remember correctly the files have to be set to EmbeddedResource. That's it.
Monotone.ExtendedWPFToolkit.xaml is only needed if you use (and want to theme) the ExtendedWPFToolkit controls. Sames goes for Monotone.MahApps.xaml and Monotone.ColorBox.xaml. So, these are optional. It's kept in separate files to avoid dependencies to libaries you might not use
You don't need the DLLs for styling the standard WPF controls
Because of the shutdown of CodePlex the Wiki-Pages are gone. I might dig into the code and recreate it on GitHub (or even better add it to the repo itself), that you have a helpful documentation.
As pointed out, the SampleApplication contains a working example which you can use as a template and/or to understand how it works
I created a template I wish to use for future projects. The template's project name is "Template". I went through Visual Studio file >> Export Template and exported the project as a project template.
I then created a new project, in a new solution using that template and called it something else. (XIVAchievements). It then replaced every occurence of "Template" with "XIVAchievements", as expected.
I can compile and run the Template project, but with the new project it will not compile. It gives an error every time I make any reference from the theme.xaml file. Theme.xaml starts off:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:XIVAchievements.Converters">
<!--Converters-->
<conv:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<conv:BooleanToWindowBGSelectionConverter x:Key="BooleanToWindowBGSelectionConverter" />
The last 2 lines there provide errors, stating that:
"The name "BooleanToVisibilityConverter" does not exist in the namespace "clr-namespace:XIVAchievements.Converters"."
It does, of course. I have tried closing and reopening VS, as well as clean and rebuild, but nothing seems to work. When manually typing out the lines which error or the xmlns, intellisense correctly gives the options IO am referencing.
Silly me. After looking at it long and hard, the problem was that I called it Template, and the word Template was used through my theme XAML, in places such as:
<Setter Property="Control.Template">
And the .Template was changing to .XIVAchievements. I put these back to .Template and it now works OK. Should give a different project name next time before exporting the template!
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.
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.