模棱两可的类型引用.一个名为“VisualState"的类型至少出现在两个命名空间中类型、模棱两可、出现在、两个

2023-09-08 09:31:27 作者:殊途

以下错误是什么?

不明确的类型引用.一个名为VisualState"的类型至少出现在两个命名空间中,System.Windows"和System.Windows".考虑调整程序集 XmlnsDefinition 属性.

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
    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>

主窗口:

<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" 对象.

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" .

您可以在 xaml 中使用以下导入来解决错误

you can resolve the error using following imports in your xaml

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

而不是使用

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

                                        </Storyboard>
                                    </VisualState>

用途:

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

                                    </Storyboard>
                                </vsm:VisualState>