Unity无法解析的自定义WPF控件
上面是自定义控件 XAML正在实现自定义控件Unity无法解析的自定义WPF控件,wpf,unity-container,prism,Wpf,Unity Container,Prism,上面是自定义控件 XAML正在实现自定义控件 public class RichTextBoxExtended : RichTextBox { static RichTextBoxExtended() { //DefaultStyleKeyProperty.OverrideMetadata(typeof(RichTextBoxExtended), new FrameworkPropertyMetadata(typeof(RichTextBoxExtended)))
public class RichTextBoxExtended : RichTextBox
{
static RichTextBoxExtended()
{
//DefaultStyleKeyProperty.OverrideMetadata(typeof(RichTextBoxExtended), new FrameworkPropertyMetadata(typeof(RichTextBoxExtended)));
}
public byte[] Text
{
get { return (byte[])GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(byte[]), typeof(RichTextBoxExtended), new UIPropertyMetadata(null, new PropertyChangedCallback(TextChangedCallback)));
private static void TextChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
RichTextBoxExtended richTextBoxExtended = obj as RichTextBoxExtended;
richTextBoxExtended.ChangeText(e);
}
private void ChangeText(DependencyPropertyChangedEventArgs e)
{
//clear out any formatting properties
TextRange range = new TextRange(base.Document.ContentStart, base.Document.ContentEnd);
range.ClearAllProperties();
//load bytes into stream, load stream into range
MemoryStream stream = new MemoryStream(e.NewValue as byte[]);
range.Load(stream, DataFormats.Rtf);
}
}
如果我从XAML中删除绑定
Resolution of the dependency failed, type = "cyos.infrastructure.Views.FileViewerView", name = "". Exception message is: The current build operation (build key Build Key[cyos.infrastructure.Views.FileViewerView, null]) failed: Object reference not set to an instance of an object. (Strategy type BuildPlanStrategy, index 3)
一切顺利…一点问题都没有…想法
编辑:
切换到创建新实例,绕过Unity。问题仍然存在,InitializeComponent()处构建FileViewerView时出现异常,“对象引用未设置为对象的实例”
位于System.Windows.Markup.BamlRecordReader.ProvideValueFromMarkupExtension(MarkupExtension MarkupExtension,对象对象,对象成员)
位于System.Windows.Markup.BamlRecordReader.ReadPropertyArrayEndRecord()处
位于System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord-BamlRecord)
位于System.Windows.Markup.BamlRecordReader.Read(布尔单记录)
位于System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()处
在System.Windows.Markup.TreeBuilder.Parse()中
位于System.Windows.Markup.XamlReader.LoadBaml(Stream-Stream,ParserContext-ParserContext,Object-parent,Boolean-closeStream)
位于System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)
在c:\Documents and Settings\amciver\My Documents\dev\cyos\cyos\cyos.infrastructure\Views\FileViewerView.xaml中的cyos.infrastructure.Views.FileViewerView.InitializeComponent()处:第1行
在cyos.infrastructure.Views.FileViewerView..ctor(FileViewerViewModel viewModel)的now…我不知道数组的问题是什么,但列表确实起作用了。它确实起作用了…对此有点沮丧…我唯一能想到的是列表=类字节=结构。。。
public class FileViewerViewModel : AViewModel
{
private byte[] _file = null;
public FileViewerViewModel(ILoggerFacade logger)
{
}
/// <summary>
/// Gets or sets the <seealso cref="DataFormats.Rtf"/> file representation as a <seealso cref="byte[]"/>
/// </summary>
public byte[] File
{
get
{
return _file;
}
set
{
_file = value;
RaiseChanged(() => this.File);
}
}
}
FileViewerView view = _container.Resolve<FileViewerView>();
Resolution of the dependency failed, type = "cyos.infrastructure.Views.FileViewerView", name = "". Exception message is: The current build operation (build key Build Key[cyos.infrastructure.Views.FileViewerView, null]) failed: Object reference not set to an instance of an object. (Strategy type BuildPlanStrategy, index 3)
<Grid>
<controls:RichTextBoxExtended x:Name="Document">
</controls:RichTextBoxExtended>
</Grid>