问题
Creating a WPF application, using VS2017. I have an enum like:
namespace Sample.Common
{
public enum SmPageTypes
{
Type1 = 0,
Type2 = 1
}
}
I'm trying to use it in xaml form, but get an error:
The name "SmPageTypes" does not exist in the namespace "clr-Sample.Common;assembly=Sample".
Here is xaml code adding namespace:
xmlns:CommonAlias="clr-namespace:Sample.Common;assembly=Sample"
and add enum as resource:
<Window.Resources>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues"
ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="CommonAlias:SmPageTypes" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
I've added the project, containing this enum, tried to clean, restart Visual Studio, rebuild solution with administrator rights, changed platform target and configuration, it did not help. What else can I do?
回答1:
If the enum and the XAML are in different projects, the project containing the XAML must have a reference to the one containing the enum.
Right-click the XAML project, select Add reference, and add a reference to your enum project.
回答2:
I've solved the problem. The reason was that assembly name should be Sample.Common, not Sample.
回答3:
In VS, right click on your Solution -> Properties -> Configuration Properties
A new dialog is opened, try to change the project configurations from Debug to Release or vice versa.
After that, re-build your solution. It can solve your problem
来源:https://stackoverflow.com/questions/51408156/the-name-does-not-exist-in-the-namespace-in-wpf-application