Custom WPF control with Unity failing to resolve
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 InitializeComponen开发者_JS百科t() 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...
I don't know what the problem with array, but List<byte> works.
精彩评论