I have 2 pages. One page contains another, where the Grid looks like the following:
<Grid>
<views:MapViewPage/>
</Grid>
I've included the views namespace correctly and everything runs fine but the designer seems to be having a bit of an issue with this.
It underlines the views:MapViewPage line and says object reference not set to an instance of an object, and complains about the constructor. I don't understand why, I've done this in WPF where I've included views in another page or window so I don't understand why the XAML designer complains in UWP. I've restarted visual studio and deleted the bin and obj directories but i still see the error - builds fine and runs fine tho... and includes the view as expected...
You are on the right path but sometimes designer picks up wrong metadata. Please delete your obj and bin directories and rebuild your project. It should work
It underlines the views:MapViewPage line and says object reference not set to an instance of an object, and complains about the constructor.
Yep, this is common behavior, if you just create folder and make a xaml file in it then use xaml file without build the project, the Visual Studio will not find the specific the namespace (xmlns:views="using:projectname.Views"). Please try to
restore the nuget package (right click the solution Restore Nuget Package) and build or re-build your project before running it.
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.
I keep getting XAML errors and "Invalid Markup" in design view when working with projects which compile and run just fine without any compiler or runtime errors.
Specifically the project I am trying to open is located here:
http://www.codeproject.com/Articles/474212/Zombie-Explorer-A-N-Tier-application-from-top-to-b
The issue seems to be with using classes in XAML from any external library which is not part of my source code, such as the Cinch and MahApps packages. I've found in some cases I can add the package through the NuGet Package Manager and it will replace the reference and it sometimes fixes the error.
To be honest, I'm not sure there's a single XAML file I can open without errors, even some errors about unresolved references to resources in the same solution.
I have read that changing "maximum parallel project builds" to 1 can fix these types of issues, but I've tried that and get the same results.
Is there any way to fix these errors so I can utilize the XAML designer?
I downloaded your code and am looking at it -- do you have a particular .xaml file that you have a question about?
In general, yes - it can be a bit of a trial to get the Designer (whether VS or Blend) to render your XAML composition without errors, when it does compile and run just fine. To troubleshoot this, you can debug the process of Visual Studio rendering it, itself, using another instance of Visual Studio. Or, the more laborious route, by stripping it down until no errors show, and then re-entering it until you see the errors reappear. I myself created a logger to log errors during rendering (within the VS Designer) to help me uncover what is different about it.
It is important to remember that the Designer has a very specific behavior in recreating your code as it parses and renders your xaml objects. It executes the code within the xaml itself. It will create the view-model objects, if your xaml has a binding expressed within the xaml that causes this. But, surprisingly, it does not execute the constructor of your main xaml object (such as a Window or UserControl). So if you're depending upon having an object exist so that it can be rendered, do the creating or initialization within code that does execute at design-time such as a property-getter or a bound property.
I had a similar problem, though the "missing" class is in my own project. I moved my project from a (mirrored) network drive to a local drive and that resolved it - suggesting that the problem is poor file management by VS.
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"
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.