Unity 的自定义 WPF 控件无法解决
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 实现自定义控件...
<Grid>
<controls:RichTextBoxExtended x:Name="Document" Text="{Binding Path=File}">
</controls:RichTextBoxExtended>
</Grid>
关联的 VM...
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)
如果我从 XAML 中删除绑定...
<Grid>
<controls:RichTextBoxExtended x:Name="Document">
</controls:RichTextBoxExtended>
</Grid>
一切都会顺利进行...完全没有问题...想法?
编辑:
切换为创建一个新实例,绕过 Unity。问题仍然在同一个地方,在 InitializeComponent() 构造 FileViewerView 时出现异常,并显示“对象引用未设置到对象的实例”。
在 System.Windows.Markup.BamlRecordReader.ProvideValueFromMarkupExtension(MarkupExtension markupExtension,对象 obj,对象成员) 在 System.Windows.Markup.BamlRecordReader.ReadPropertyArrayEndRecord() 在 System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord) 在 System.Windows.Markup.BamlRecordReader.Read(布尔 singleRecord) 在 System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment() 在 System.Windows.Markup.TreeBuilder.Parse() 在 System.Windows.Markup.XamlReader.LoadBaml(流流,ParserContext parserContext,对象父级,布尔 closeStream) 在System.Windows.Application.LoadComponent(对象组件,Uri资源定位器) 在 c:\Documents and Settings\amciver\My Documents\dev\cyos\cyos\cyos.infrastruct\Views\FileViewerView.xaml 中的 cyos.infrastruct.Views.FileViewerView.InitializeComponent():第 1 行 现在在 cyos.infrastruct.Views.FileViewerView..ctor(FileViewerViewModel viewModel)...
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);
}
}
Above is the custom control...
XAML implementing custom control...
<Grid>
<controls:RichTextBoxExtended x:Name="Document" Text="{Binding Path=File}">
</controls:RichTextBoxExtended>
</Grid>
Associated VM...
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);
}
}
}
Finally..if I call...
FileViewerView view = _container.Resolve<FileViewerView>();
It fails.
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)
If I remove the binding from within the XAML...
<Grid>
<controls:RichTextBoxExtended x:Name="Document">
</controls:RichTextBoxExtended>
</Grid>
Everything works without a hitch...no problems at all...ideas?
EDIT:
Switched to creating a new instance, bypassing Unity. Issue is still in the same place, exception at construction of FileViewerView at InitializeComponent() with "Object reference not set to an instance of an object."
at System.Windows.Markup.BamlRecordReader.ProvideValueFromMarkupExtension(MarkupExtension markupExtension, Object obj, Object member)
at System.Windows.Markup.BamlRecordReader.ReadPropertyArrayEndRecord()
at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
at System.Windows.Markup.TreeBuilder.Parse()
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at cyos.infrastructure.Views.FileViewerView.InitializeComponent() in c:\Documents and Settings\amciver\My Documents\dev\cyos\cyos\cyos.infrastructure\Views\FileViewerView.xaml:line 1
at cyos.infrastructure.Views.FileViewerView..ctor(FileViewerViewModel viewModel) in now...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道数组有什么问题,但 List作品。
I don't know what the problem with array, but List<byte> works.