问题描述
以下错误是什么?
不明确的类型引用.名为VisualState"的类型至少出现在System.Windows"和System.Windows"两个命名空间中.考虑调整程序集 XmlnsDefinition 属性.
用户控件:
<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>
这篇关于不明确的类型引用.名为“VisualState"的类型至少出现在两个命名空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!