The component does not have a resource identified by the uri

后端 未结 20 972
悲哀的现实
悲哀的现实 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 07:55

    Frustratingly, I had exactly this error and spent forever trying to work out the cause. For me, it was once working but then I made some very minor changes to the XAML of the derived control, and the compiler started giving that error message. Short solution, cutting out many hours of trying to figure it out: shut down Visual Studio and re-opened it, recompiled, problem magically went away! (This is VS2012 Pro) Just added this in case anyone reading is going round in circles trying to find a non-existent problem with their code. Might be worth trying the "IT Crowd solution" first.

    0 讨论(0)
  • 2020-12-01 07:56

    This gave me headaches for 3 days! I have a XAML UserControl in a class library and a class (only C#) that derives from the UserControl in my .exe project. In xaml designer of my MainWindow.xaml and when starting the application, I got the error "component does not have a resource identified by the uri". The answer of "Juan Carlos Girón" finally lead me to the solution:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.Reflection;
    using System.IO.Packaging;
    using System.Windows.Markup;
    
    namespace ClassLibrary1
    {
        static class Extension
        {
            public static void LoadViewFromUri(this UserControl userControl, string baseUri)
            {
                try
                {
                    var resourceLocater = new Uri(baseUri, UriKind.Relative);
                    var exprCa = (PackagePart)typeof(Application).GetMethod("GetResourceOrContentPart", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { resourceLocater });
                    var stream = exprCa.GetStream();
                    var uri = new Uri((Uri)typeof(BaseUriHelper).GetProperty("PackAppBaseUri", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null, null), resourceLocater);
                    var parserContext = new ParserContext
                    {
                        BaseUri = uri
                    };
                    typeof(XamlReader).GetMethod("LoadBaml", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { stream, parserContext, userControl, true });
                }
                catch (Exception)
                {
                    //log
                }
            }
        }
    }
    

    and called that from by UserControl's .cs file:

    namespace ClassLibrary1
    {
        public partial class MyUserControl : UserControl
        {
            public MyUserControl()
            {
                //InitializeComponent();
                this.LoadViewFromUri("/ClassLibrary1;component/myusercontrol.xaml");
            }
        }
    }
    

    Thanks again to "Juan Carlos Girón"!

    0 讨论(0)
  • 2020-12-01 08:02

    I had accidently deleted a user control via a rename/copy action. When I reinstated the project file and the xaml file and .cs from version control this error started happening in the design studio for that control which had mistakenly been deleted/renamed.

    That suggested some type of cache on the file in question....so closing Visual Studio, deleting the bin directory and rebuilding worked.

    0 讨论(0)
  • 2020-12-01 08:02

    I started consistently seeing a "the component does not have a resource identified by the uri" error when I clicked a particular menu choice from an installed product that was working on other computers. I tried uninstalling the product, making sure its files really were gone, rebooting, and reinstalling the product. The problem remained. I deleted the contents of my %TEMP% directory, and the problem ceased.

    0 讨论(0)
  • 2020-12-01 08:04

    Followed PainElemental's solution (to clarify, for his code the ClassLibrary1 for me was the .dll name without the .dll extension), here's my scenario in case it helps anyone link their specific error messages to the problem:

    I use dll's to load and run usercontrols into a main program as their own popup windows. PainElemental's solution was mostly working , but 1 of the 3 classes in my "popup .dll" wouldn't load properly. I would get an exception with 2 inner exceptions, like:

    mscorlib InvokeMethod...;
    WpfXamlLoader.Load...Provide value on...StaticResourceExtension...;
    ResolveBamlType....method or operation is not implemented.

    In my case, I confirmed it would load the new URI and work in testing, but when I tried to run it over in my Live environment it would error in LoadViewFromUri().

    As I tested further, I narrowed down the issue to not being able to load a separate "library .dll" file I was using which contained a Converter I was using in the .xaml file of the class which was failing, and on further research the issue there was that the Live environment was using a different "library .dll" version than I was using in my test environment, even though the exception message from my "popup .dll" did not make any mention of that.

    For reference, I use Copy Local=True and that didn't give me issues. To best debug these kinds of issues, an understanding of the locations where .dll files are searched for by the .exe is helpful. As I understand it, when you are running projects in VS, when Copy Local=True the .dlls get copied to the same folder as the .exe when it is Built. When the .exe is run the standard location it will search for .dlls is the same folder as the .exe. Additional locations that the .exe can look for .dlls can be set in the .exe.config file, in the probing element. In the below example, it can also search in a 'MyDLLs' and the 'MyDLLs\Core' directory relative to the .exe's location. Note that it will not naturally search any subfolders, you have to specify them explicitly. I believe it also searches the GAC, but I currently have minimal knowledge concerning GAC.

    <configuration>
     ... 
    
       <runtime>  
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="MyDLLs;MyDLLs\Core;"/>
          </assemblyBinding>
       </runtime>  
    </configuration>
    
    0 讨论(0)
  • 2020-12-01 08:06

    I resolved this by placing

    myusercontrol = Activator.CreateInstance<myusercontrol>(); 
    

    in the constructor of the window containing the usercontrol before the InitializeComponent(); line

    0 讨论(0)
提交回复
热议问题