36

What is the following error?

Ambiguous type reference. A type named 'VisualState' occurs in at least two namespaces, 'System.Windows' and 'System.Windows'. Consider adjusting the assembly XmlnsDefinition attributes.

UserControl:

<UserControl
    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"
    mc:Ignorable="d"
    x:Class="ButtonWPF.MyButtonAdd"
    x:Name="AddButton"
    d:DesignWidth="84" d:DesignHeight="87">
    <UserControl.Resources>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    .............
                                    <Trigger Property="IsDefaulted" Value="True"/>
                                    <Trigger Property="IsMouseOver" Value="True"/>
                                    <Trigger Property="IsPressed" Value="True"/>
                                    <Trigger Property="IsEnabled" Value="False"/>
                                    </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot"
          Height="79"
          Width="72">
        <Button Content=""
                HorizontalAlignment="Left"
                Height="61"
                Style="{DynamicResource ButtonStyle1}"
                VerticalAlignment="Top"
                Width="57"/>
    </Grid>
</UserControl>

MainWindow:

<Window x:Class="ButtonWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
        xmlns:my="clr-namespace:ButtonWPF"
        Title="winGroup"
        Height="637"
        Width="638"
        FontSize="15"
        FontWeight="Bold">
    <Grid>
        <my:MyButtonAdd HorizontalAlignment="Left"
                        Margin="540,519,0,0"
                        x:Name="btnAdd"
                        VerticalAlignment="Top"
                        IsEnabled="True"/>
    </Grid>
</Window>
George Lanetz
  • 300
  • 5
  • 18
mrJack
  • 1,001
  • 6
  • 17
  • 33
  • 3
    Where exactly does the error show up? I don't even see VisualState being used anywhere in your XAML, so it's a bit difficult to determine. My initial guess, though, is that maybe you have imported two different versions of the DLL for the `System.Windows` namespace. – Ken Wayne VanderLinde Aug 04 '11 at 23:10
  • 2
    Possible duplicate http://stackoverflow.com/questions/4913910/design-time-error-visualstate-occurs-in-at-least-two-namespaces – Tomislav Markovski Aug 04 '11 at 23:27

1 Answers1

53

This error(most of the time warning) will occur when using two or more references which contains same namespace and classes. in your case you are using VisualState which is part of PresentationFramework assembly and you might have added another assembly which contains same "VisualState" object with the same namespace "System.Windows" .

you can resolve the error using following imports in your xaml

xmlns:vsm ="clr-namespace:System.Windows;assembly=PresentationFramework"

instead of using

<VisualState x:Name="Pressed">
                                        <Storyboard>

                                        </Storyboard>
                                    </VisualState>

Use:

<vsm:VisualState x:Name="Pressed">
                                    <Storyboard>

                                    </Storyboard>
                                </vsm:VisualState>
Bathineni
  • 3,436
  • 18
  • 25
  • How is this avoided? Surely those classes should not be in two assemblys? – Gusdor Nov 03 '11 at 12:37
  • 3
    And what if it still fails? I have both referenced in my project but I explicitly use the namespace vsm as you have here. I still get a build error. "The type 'System.Windows.VisualStateGroup' exists in both 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\PresentationFramework.dll' and 'c:\Program Files (x86)\WPF Toolkit\v3.5.50211.1\WPFToolkit.dll" – alan Mar 29 '12 at 04:48
  • is it failing in any particular file.? – Bathineni Mar 29 '12 at 11:05
  • Thank you very much for your answer. This was a problem for us it it helped us. – Mike Malter Jun 29 '12 at 00:55
  • I am having same problem . The solution provided works fine in xaml . But I am accessing VisualState in my code behind . How can i solve this issue in Code Behind? – Kuntady Nithesh May 07 '13 at 09:51
  • @ Kuntady Nithesh Have look at this link this may help you.. http://stackoverflow.com/questions/453451/resolving-extension-methods-linq-ambiguity – Bathineni May 13 '13 at 11:41
  • Thanks for this, the xaml update resolved our issues. – Guy Starbuck Oct 11 '13 at 18:23