代码之家  ›  专栏  ›  技术社区  ›  Aaron McIver

无法解析具有Unity的自定义WPF控件

  •  2
  • Aaron McIver  · 技术社区  · 14 年前
    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>
    

    关联的虚拟机…

    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。问题仍然在同一位置,在初始化组件()处构建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.treebuilderbalmtranslator.parseFragment()上 在System.Windows.Markup.Treebuilder.Parse()上 在System.Windows.Markup.XamlReader.LoadBaml(流、ParserContext ParserContext、对象父级、布尔关闭流) 在system.windows.application.loadcomponent(对象组件、uri资源定位器) 位于c:\documents and settings\amciver\my documents\dev\cyos\cyos\cyos.infrastructure\views.fileviewerview.initializecomponent()中的cyos.infrastructure.views.fileviewerview.xaml:line 1 在cyos.infrastructure.views.fileviewerview..ctor(fileviewerview模型viewmodel)中…

    1 回复  |  直到 14 年前
        1
  •  1
  •   vortexwolf    14 年前

    我不知道数组有什么问题,但list<byte>起作用。