The component does not have a resource identified by the uri

后端 未结 20 1048
悲哀的现实
悲哀的现实 2020-12-01 07:33

I want to create a Generic DataGrid to use on all my Views/UserControls.

This is my structure:

Class Library called \"Core\":

20条回答
  •  余生分开走
    2020-12-01 08:07

    The reason you are getting this error is because the way InitializeComponent that is implemented (in VS 2010) will always search in the derived class's assembly.

    Here is InitializeComponent:

    /// 
    /// InitializeComponent
    /// 
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public void InitializeComponent() {
        if (_contentLoaded) {
            return;
        }
        _contentLoaded = true;
        System.Uri resourceLocater = new System.Uri("/WpfApplication1;component/mainwindow.xaml", System.UriKind.Relative);
    
        #line 1 "..\..\..\MainWindow.xaml"
        System.Windows.Application.LoadComponent(this, resourceLocater);
    
        #line default
        #line hidden
    }
    

    The line where it looks up your XAML resource is System.Windows.Application.LoadComponent(this, resourceLocator). And this most probably fails because equivalent of 'this.GetType().Assembly' is used to determine which assembly to search for the resource identified by the relative Uri. And 'this.GetType()' does get the derived type of the object, not the type of the class where the code is implemented.

    PS. Is this a bug? I do not know...

提交回复
热议问题