组件没有由 uri 标识的资源 [英] The component does not have a resource identified by the uri

查看:19
本文介绍了组件没有由 uri 标识的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通用数据网格以用于我所有的视图/用户控件.

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

这是我的结构:

类库称为核心":

Class 称为 "ViewBase":

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

    //Rest of Methods and Properties
}

类库称为控件":

UserControl 调用了 "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>

背后的代码:

using Core;

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

然后是名为WPFApp"的 WPF 应用程序:

Then is the WPF Aplication called "WPFApp":

Class 称为"View":

using Controls;

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

我的整个想法是在需要 DataGrid 的地方使用 GridView.

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'."

我做错了什么?

这是正确的方法还是我离题了?

Is this the correct approach or am i way off?

推荐答案

我做了一些非常相似的事情,但结果相同.我有一个 C# 类库,其中包含一个名为 UsageControl 的 WPF 控件(带有 xaml.cs 文件的 xaml).在一个单独的 C# 项目(即单独的 dll)中,我创建了一个 C# class CPUUsageControl,它从 UsageControl 继承,但对其进行了自己的调整.当我尝试在我的一个视图上使用 CpuUsageControl 时,我遇到了与您相同的错误.

I was doing something very similar with the same result. I had one C# class library that contained a WPF control called UsageControl (xaml with accompanying xaml.cs file). In a separate C# project(i.e. separate dll) I created a C# class CPUUsageControl which inherited from UsageControl, but put its own spin on it. When I tried to use the CpuUsageControl on one of my views I got the same error you did.

我所做的修复是在我的单独程序集中,而不是创建一个继承自基本控件的类,我创建了一个新的 WPF 控件,该控件包含基本控件.然后我将包含在 CpuUsage 类中的所有逻辑放入后面的 WpfCpuUsageControl 代码中.我能够使用这个对象是我所有的其他控件都很好.

What I did to fix that was in my seperate assembly, instead of creating a class that inherited from the base control, i created a new WPF Control that contained the base control. I then put all of the logic that was contained in the CpuUsage class into the WpfCpuUsageControl's code behind. I was able to use this object is all of my other controls just fine.

对于您的控件GridView",我将创建一个新的 WPF 用户控件,将其命名为 GridView 并使其包含一个ViewBase"作为网格控件的内容. 在你的 DataGrid 中放入 ViewBase 的内容,像这样:

For your Control "GridView" i would create a new WPF user control, call it GridView and make it contain a "ViewBase" as the content of the Grid control.Inside of the ViewBase's content put in your DataGrid, like this:

<UserControl....>
    <Grid>
        <ViewBase name="vBase">
            <DataGrid name="dGrid" />
        </ViewBase>
    </Grid>
</UserControl>

我也不清楚您需要 ViewBase 直接从 UserControl 继承.如果您想要的只是让您的控件具有某些属性和方法,为什么不创建一个 BaseControl 类(该类不继承自任何对象,但不继承自对象)并让未来的控件从中继承.也许抽象基类或接口正是您所追求的.

It is also not apparent to me that you need ViewBase to inherit from UserControl directly. If all you want are for your controls to have certain properties and method why not just make a BaseControl class (that does not inherit from anyone but object) and have future controls inherit from it. Perhaps an abstract base class or interface is what you're after.

对于 MVVM WPF 项目,我通常有一个 BaseViewModel,它为我实现了 INotifyPropertyChanged,因此我不必在任何地方都执行相同的代码.

For MVVM WPF projects, I typically have a BaseViewModel which implements INotifyPropertyChanged for me so I don't have to do that same code everywhere.

祝你好运,我知道这个问题很难弄清楚.异常消息和谷歌最无用!

Best of luck, I know this problem was a huge pain to figure out. The exception message and google are most unhelpful!

这篇关于组件没有由 uri 标识的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆