Ambiguous type reference. A type named `VisualState` occurs in at least two namespaces
以下错误是什么?
Ambiguous type reference. A type named 'VisualState' occurs in at least two namespaces, 'System.Windows' and 'System.Windows'. Consider adjusting the assembly XmlnsDefinition attributes.
用户控制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <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> |
主窗口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <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> |
当使用两个或多个包含相同命名空间和类的引用时,将发生此错误(大多数情况下是警告)。在您的情况下,您正在使用 VisualState ,它是 PresentationFramework 程序集的一部分,并且您可能添加了另一个程序集,该程序集包含具有相同命名空间 "System.Windows" 的相同 "VisualState" 对象。
您可以在 xaml
中使用以下导入来解决错误
1 | xmlns:vsm ="clr-namespace:System.Windows;assembly=PresentationFramework" |
而不是使用
1 2 3 4 5 | <VisualState x:Name="Pressed"> <Storyboard> </Storyboard> </VisualState> |
用途:
1 2 3 4 5 | <vsm:VisualState x:Name="Pressed"> <Storyboard> </Storyboard> </vsm:VisualState> |