The component does not have a resource identified

2019-01-11 02:53发布

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

This is my structure:

Class Library called "Core":

Class called "ViewBase":

public class ViewBase : UserControl
{
    public ViewBase()
    {
    }   

    //Rest of Methods and Properties
}

Class Library called "Controls":

UserControl Called "GridView":

XAML:

    <vb:ViewBase x:Class="Controls.GridView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:vb="clr-namespace:Core;assembly=Core">

    <Grid>
        <DataGrid></DataGrid>
    </Grid>

    </vb:ViewBase>

Code Behind:

using Core;

public partial class GridView : ViewBase
{
    public GridView ()
    {
        InitializeComponent();
    }
}

Then is the WPF Aplication called "WPFApp":

Class called "View":

using Controls;

public class View : GridView
{
    public View()
    {
        InitializeComponent();
    }
}

My whole idea is to use GridView where i need a DataGrid.

When i run the application i get this error:

"The component 'WpfApp.View' does not have a resource identified by the URI '/Controls;component/GridView.xaml'."

What am i doing wrong?

Is this the correct approach or am i way off?

17条回答
Explosion°爆炸
2楼-- · 2019-01-11 03:20

I resolved this by placing

myusercontrol = Activator.CreateInstance<myusercontrol>(); 

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

查看更多
唯我独甜
3楼-- · 2019-01-11 03:22

I got this error after renaming a xaml file. Reversing the renaming solved the problem.

Furthermore, I found that a reference to the xaml file name in App.xaml was not updated (the StartupUri), but renaming that to the current name didn't resolve the problem (but maybe it does for you). Basically, I can't rename the xaml file.

Fyi, for me, the component 'complaining' in the error was SplitComboBox.

查看更多
再贱就再见
4楼-- · 2019-01-11 03:22

Quicker than closing all of Visual Studio is just to kill XDescProc.exe in your task manager.

XDescProc is the designer. The moment the process is closed you'll see a Reload the designer link in visual studio. Click that and XDes will be started again and your 'no resource' error should be gone.

Here's the link visual studio shows after you kill the designer process:

enter image description here

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-11 03:22

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.

查看更多
你好瞎i
6楼-- · 2019-01-11 03:24

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"!

查看更多
登录 后发表回答