在使用UserControl示例时,我无法使代码生效。不可否认,我一路上有一些拼写错误,但即使在他们全部清理完之后,我仍然遇到xaml解析器的错误,说该类无法构建。
错误发生在测试应用程序的主窗口的xaml中......
<Window x:Class="CH10.TestUserControls.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrls="clr-namespace:CH10.UserControls;assembly=CH10.UserControls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--The error occurred in the following line-->
<ctrls:ColorPicker SelectedColor="AntiqueWhite"
SelectedColorChanged="OnColorChanged"/>
<TextBlock x:Name="_tbcolor" FontSize="20" Grid.Row="1" Margin="8" />
</Grid>
</Window>
我设法通过在DP的注册中导致运行时错误来欺骗它引发完全相同的错误...
System.Windows.Markup.XamlParseException occurred
_HResult=-2146233087
_message='The invocation of the constructor on type 'CH10.UserControls.ColorPicker' that matches the specified binding constraints threw an exception.' Line number '13' and line position '10'.
HResult=-2146233087
IsTransient=false
Message='The invocation of the constructor on type 'CH10.UserControls.ColorPicker' that matches the specified binding constraints threw an exception.' Line number '13' and line position '10'.
Source=PresentationFramework
LineNumber=13
LinePosition=10
StackTrace:
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
InnerException: System.TypeInitializationException
_HResult=-2146233036
_message=The type initializer for 'CH10.UserControls.ColorPicker' threw an exception.
HResult=-2146233036
IsTransient=false
Message=The type initializer for 'CH10.UserControls.ColorPicker' threw an exception.
Source=CH10.UserControls
TypeName=CH10.UserControls.ColorPicker
StackTrace:
at CH10.UserControls.ColorPicker..ctor()
InnerException: System.ArgumentException
_HResult=-2147024809
_message=Default value type does not match type of property 'SelectedColor'.
HResult=-2147024809
IsTransient=false
Message=Default value type does not match type of property 'SelectedColor'.
Source=WindowsBase
StackTrace:
at System.Windows.DependencyProperty.ValidateDefaultValueCommon(Object defaultValue, Type propertyType, String propertyName, ValidateValueCallback validateValueCallback, Boolean checkThreadAffinity)
at System.Windows.DependencyProperty.RegisterCommon(String name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
at System.Windows.DependencyProperty.Register(String name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
at System.Windows.DependencyProperty.Register(String name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
at CH10.UserControls.ColorPicker..cctor() in c:\Users\Admin\Documents\Visual Studio 2013\Projects\CH10.UserControls\CH10.UserControls\ColorPicker.xaml.cs:line 26
InnerException:
上述错误与投掷但我人为创造的错误相同。唯一的区别是原始错误的堆栈跟踪将我带到了这个方法的第一行......
public event RoutedPropertyChangedEventHandler<Color> SelectedColorChanged
{
add { AddHandler(SelectedColorChangedEvent, value); }
remove { RemoveHandler(SelectedColorChangedEvent, value); }
}
我在工作版中粘贴了相同的代码,但没有任何区别。
我开始将我输入的代码与我正在阅读的书籍下载的版本进行比较,我唯一的区别就是在UserControl文件夹的Settings.Designer.cs文件中。这条线尤其......
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
背景是......
namespace CH10.UserControls.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}
在下载的代码版本中,它有&#34; 10.0.0.0&#34;而不是&#34; 11.0.0.0&#34; ...
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
所以,即使它是自动生成的(我猜项目设置的时候),我改变了我的版本以匹配,突然一切正常。
然后我把它改回去了,它仍然有用。
该指令的作用是什么,何时生成代码以及为什么会导致此类问题?
代码在此github repo
中有两个组件,UserControl和一个测试它的应用程序。 测试应用程序中的Settings.Designer.cs文件具有&#34; 11.0.0.0&#34;我没有改变它。
在伪装之后,我开始得到这样的错误......
异常:组件&#39; CH10.UserControls.ColorPicker&#39;不具有 由URI标识的资源 &#39; /CH10.UserControls;组分/ colorpicker.xaml&#39;
但是关机/重启可以摆脱它。