I am following a tutorial to implement a value converter. I receive an error for this code:
public class BooleanToVisibilityConverter : IValueConverter
{
//
}
<Application
x:Class="TestApp10.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp10"
RequestedTheme="Light">
<Application.Resources>
<local:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter" />
<local:BooleanToVisibilityConverter x:Key="FalseToVisibleConverter" IsReversed="True" />
</Application.Resources>
I get an error
The name "BooleanToVisibilityConverter" does not exist in the namespace "using:TestApp10"
The function is defined in the App.xaml.cs file.
Thank you.
This seems to be an intellisense error in XAML Designer. Please make sure you have implemented your BooleanToVisibilityConverter correctly. Then you can try with following steps to fix this error.
Open your project in File Explorer, delete bin and obj folder
In Visual Studio, right-click your Solution, and select Clean.
Right-click your Solution, and select Rebuild.
After this, your error should be gone. If you still get this error, you can try to reopen your project in Visual Studio.
Related
I opened a VS 2012 Solution in VS 2015 and cannot get it to compile. It was originally targeting .Net 3.5 and used the WPF Toolkit, which caused some ambiguous references. I resolved that by removing the reference to the WPF Toolkit and replacing it with the package DotNetProjects.Wpf.Toolkit.
Now I have to tackle the next issue, which I suspect is not caused by the obvious but is a cascade from some other hidden gotcha from upgrading the versions.
My XAML forms are not recognizing the existence of classes within referenced namespaces. I can get them to come up on intellisense but the application won't compile and the error says something like: "The name "MainWindow" does not exist in the namespace "clr-namespace:FieldSheetPrinter".
I have defined my main form with the following:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FieldSheetPrinter"
mc:Ignorable="d"
xmlns:Custom="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
xmlns:validationRules="clr-namespace:SWallTech.ValidationSupport;assembly=SWallTech.ValidationSupport"
xmlns:FieldSheetPrinter_Converters="clr-namespace:FieldSheetPrinter.Converters"
xmlns:Converters="clr-namespace:SWallTech.WPF.Support.Converters;assembly=SWallTech.WPF.Support"
x:Class="FieldSheetPrinter.MainWindow"
x:Name="MainWindow"
Title="CAMRA Field Sheet Printer - Stonewall Technologies, Inc."
Width="744"
Height="583"
WindowStartupLocation="CenterScreen"
Icon="CamraPrint.ico">
I have references to the SWallTech.WPF.Support project, which is in the solution, and the compiler has no problem recognizing the classes in that project's Converters namespace. This code has no error indicator. (Red squiggles.)
<Converters:IntGreaterThanZeroToVisibilityConverter
x:Key="IntGreaterThanZeroToVisibilityConverter" />
<Converters:DatabaseConnectionImageSourceConverter
x:Key="DatabaseConnectionImageSourceConverter" />
However if I try to refer to the ViewModel I am using as the datasource, I get an error:
<local:FieldSheetPrinterViewModel
x:Key="FieldSheetPrinterViewModelDataSource"
d:IsDataSource="True" />
I also get the error that the XAML form itself is not in the namespace FieldSheetPrinter, which of course it is, as you can see from the Code-Behind.
namespace FieldSheetPrinter
{
public partial class MainWindow : Window
...
}
I have tried everything I can think of, and I am truly stumped.
Joey
As suggested above, the problem wasn't directly with the XAML, or at least not completely. The original developer had named the Window tag with the same name as the code-behind class name, and that seems to be prohibited now. That was the only issue with the XAML. Everything else had to do with overlaps in the WPF and Windows.System.Data libraries that caused IValueConverter (in the System.Windows.Data namespace) to go unrecognized. I eliminated any other references that could have conflicted, and corrected some syntax issues in the code-behind that apparently were deprecated.
Ex.
stringVariable.IsNullOrEmpty()
was used instead of
string.IsNullOrEmpty(stringVariable)
In other places some ObservableCollection objects weren't recognized as enumerable, so I had to add "ToList()" before looping through the collection.
Weird, but I didn't write this stuff!
Thanks for the suggestions.
Joey
The VisualStudio 2015 project I'm working on uses a .cs file to create a custom control derived from System.Windows.Controls.RadioButton.
using System.Windows;
using System.Windows.Controls;
using BAB_HMI.Views;
namespace BAB_HMI.Controls
{
public class HMIViewRadioButton : RadioButton
{
public HMIView ViewType
{
get { return (HMIView)GetValue(ViewTypeProperty); }
set
{
SetValue(ViewTypeProperty, value);
}
}
public static DependencyProperty ViewTypeProperty = DependencyProperty.Register("ViewType", typeof(HMIView), typeof(HMIViewRadioButton));
}
}
The resource dictionary references the control like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:BAB_HMI.Controls">
<Style x:Key="ModuleRadioButton" TargetType="{x:Type Controls:HMIViewRadioButton}">
...
</ResourceDictionary>
There are no compile errors but when opening App.xaml I get an error saying "An error occurred while finding the resource dictionary".
The error goes away if I use a .xaml file to create the custom control and put the HMIViewRadioButton code in the code-behind .xaml.cs file, but I don't want to do this because the error is only displaying on my machine. Other people on the team are not getting this error.
Is there a local setting that enables design mode to see custom controls that are implemented in .cs files instead of .xaml.cs files? ...or is it something else that's causing the error?
I figured it out myself. I had the "Platform target" build setting set to "x64". Changing it to "Any CPU" fixed the problem. I think this may be because Visual Studio is running as "x86" in Design Mode and it can't use assemblies built with just "x64" set.
When I include an assembly containing a ResourceDictionary using the following pack syntax:
"pack://application:,,,/WpfCore;component/ResourceDictionaries/ThemedControls.xaml"
It works as expected, but as soon as I add a code behind file to the XAML of the ResourceDictionary, the following error is thrown:
“An error occurred while finding the resource dictionary”
The code behind is added to the XAML in the usual way:
< ResourceDictionary x:Class="com.mycompany.WpfCore.ResourceDictionaries.ThemedControls"
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">
</ResourceDictionary>
and looks like this:
namespace com.mycompany.WpfCore.ResourceDictionaries
{
public partial class ThemedControls : ResourceDictionary
{
public ThemedControls ()
{
InitializeComponent();
}
}
}
Intuition tells me this is a namespace problem, but all the variations I've tried fail. What am I doing wrong or is this a limitation of WPF ResourceDictionaries?
Edit:
Seems the question detail was called out and found to be wanting.
The initial example had the namespace simplified. The default namespace for the WpfCore project is com.mycompany.WpfCore which I have now added into the code examples above.
The ThemedControls.xaml and ThemedControls.xaml.cs files are located in a subfolder called ResourceDictionaries within the WpfCore project folder.
The resulting assembly is used as a referenced assembly in another solution and this is where the Pack URI is being used.
Edit 2:
After playing around with the build action for the xaml files (changing from page to resource and back again) things started working. Marking Sheridan's answer as correct.
I don't think that you have declared your ResourceDictionary quite correctly... the application name really should be in the namespace. This should work... at least it works for me:
<ResourceDictionary
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"
x:Class="WpfCore.ResourceDictionaries.ThemedControls">
</ResourceDictionary>
Code behind:
namespace WpfCore.ResourceDictionaries
{
public partial class ThemedControls : ResourceDictionary
{
public ThemedControls()
{
InitializeComponent();
}
}
}
I'm starting learning XAML and I add some code to my BlankPage application. And suddenly a constructor which is initializing a component:
public BlankPage()
{
this.InitializeComponent();
}
Stop working. I've got now that error:
'BlankApplication.BlankPage' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'BlankApplication.BlankPage' could be found (are you missing a using directive or an assembly reference?)
Honestly I didn't do anything, I didn't even look at this part of code and now it doesn't work.
Screenshot:
Just to give a bit more information on how to fix this (since this explanation is a bit on the vague side)..
This issue (for me) was caused because I changed the namespace in the code after I created the project. In order to fix this issue I had to make some changes in a couple of locations:
1: In App.xaml I had to change the following:
<Application
x:Class="New.Namespace.App"
2: In MainPage.xaml I had to change the following:
<Page
x:Class="New.Namespace.MainPage"
You will also want to make sure that you change the 'namespace' line in your App.xaml.cs as well as your MainPage.xaml.cs.
Finally, you will also want to make sure you update the Project Entrypoint in the Package.appxmanifest to point to "New.Namespace.App".
If your Main class's namespace is different than .xaml file's x:Class attribute, you will get this error. For instance;
Your MainPage.xaml.cs;
namespace UWPControls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
}
Your MainPage.xaml;
<Page
x:Class="UWPControls_Different.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPHelloWorld"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</page>
You're going to see the error until changing x:class to the;
x:Class="UWPControls.MainPage"
This happens when you change a namespace for a class, you must do the same inside the XAML file.
There are two places inside the XAML file with the old namespace.
For me, the project was inside solution with other projects. When suggestions here did not work, a simple unloading and reloading the project from solution did the trick and fix all errors (with rebuilding of course).
My Solution: For my "SomePage : ContentPage", I changed the XAML properties:
Generator (Custom Tool): From MSBuild:Compile to MSBuild:UpdateDesignTimeXaml
BuildAction: From Page to Embedded resource
Using Visual Studio 2017 Enterprise 15.3.4
Problem solved.
Cause: I forgot to also change custom application name in xaml code.
Solution: I have changed application name in XAML, now it works well.
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.