Wil*_*lem 43 .net c# generics wpf mvvm
我想创建一个Generic DataGrid,用于我的所有Views/UserControls.
这是我的结构:
Class Library叫"Core":
Class叫"ViewBase":
public class ViewBase : UserControl
{
public ViewBase()
{
}
//Rest of Methods and Properties
}
Run Code Online (Sandbox Code Playgroud)
Class Library叫"Controls":
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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using Core;
public partial class GridView : ViewBase
{
public GridView ()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
然后是名为"WPFApp"的WPF应用程序:
Class叫"View":
using Controls;
public class View : GridView
{
public View()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
我的整个想法是使用GridView我需要的地方DataGrid.
当我运行应用程序时,我收到此错误:
"The component 'WpfApp.View' does not have a resource identified by the URI '/Controls;component/GridView.xaml'."
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这是正确的方法还是我离开了?
Hug*_*otl 61
令人沮丧的是,我确实遇到了这个错误并且花了很多时间试图找出原因.对我来说,它曾经工作,但后来我对派生控件的XAML做了一些非常小的改动,编译器开始给出错误信息.简短的解决方案,削减了许多小时试图找出它:关闭Visual Studio并重新打开它,重新编译,问题神奇地消失了!(这是VS2012专业版)只是添加了这个,以防任何人阅读围绕圈子试图找到他们的代码不存在的问题.可能值得首先尝试"IT Crowd解决方案".
Tim*_*ith 21
您收到此错误的原因是因为实现的InitializeComponent(在VS 2010中)将始终在派生类的程序集中进行搜索.
这是InitializeComponent:
/// <summary>
/// InitializeComponent
/// </summary>
[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
}
Run Code Online (Sandbox Code Playgroud)
它查找XAML资源的行是System.Windows.Application.LoadComponent(this,resourceLocator).这很可能失败,因为相当于'this.GetType().Assembly'用于确定搜索相对Uri标识的资源的程序集.并且'this.GetType()'确实获得了对象的派生类型,而不是实现代码的类的类型.
PS.这是一个错误吗?我不知道...
Pai*_*tal 20
这让我头疼了3天!我在类库中有一个XAML UserControl,在我的.exe项目中有一个来自UserControl的类(只有C#).在我的MainWindow.xaml的xaml设计器中,当启动应用程序时,我收到错误"组件没有uri标识的资源"."JuanCarlosGirón"的答案终于引出了我的解决方案:
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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并通过UserControl的.cs文件调用它:
namespace ClassLibrary1
{
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
//InitializeComponent();
this.LoadViewFromUri("/ClassLibrary1;component/myusercontrol.xaml");
}
}
}
Run Code Online (Sandbox Code Playgroud)
再次感谢"JuanCarlosGirón"!
Jua*_*rón 12
你可以尝试这种方法
我创造了自己的InitializeComponent(),我这样打电话
this.LoadViewFromUri("/NameOfProject;component/mainwindow.xaml");
public static void LoadViewFromUri(this Window window, 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, window, true });
}
catch (Exception)
{
//log
}
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ook 11
我做了一些非常相似的事情.我有一个C#类库,其中包含一个名为UsageControl的WPF控件(带有xaml.cs文件的xaml).在一个单独的C#项目(即单独的dll)中,我创建了一个继承自UsageControl 的C#类 CPUUsageControl ,但是在它上面放了自己的旋转.当我尝试在我的一个视图上使用CpuUsageControl时,我遇到了同样的错误.
我做了什么来解决这个问题是在我的单独程序集中,而不是创建一个从基本控件继承的类,我创建了一个包含基本控件的新WPF 控件.然后,我将CpuUsage类中包含的所有逻辑放入WpfCpuUsageControl的代码中.我能够使用这个对象是我的所有其他控件就好了.
对于你的Control"GridView",我会创建一个新的WPF用户控件,称之为GridView并使其包含一个"ViewBase"作为Grid控件的内容.在ViewBase的内容放在你的DataGrid中,如下所示:
<UserControl....>
<Grid>
<ViewBase name="vBase">
<DataGrid name="dGrid" />
</ViewBase>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我也不清楚你需要ViewBase直接从UserControl继承.如果你想要的只是你的控件具有某些属性和方法,为什么不只是创建一个BaseControl类(除了对象之外不从任何人继承)并让将来的控件继承它.也许你正在追求一个抽象的基类或接口.
对于MVVM WPF项目,我通常有一个为我实现INotifyPropertyChanged的BaseViewModel,所以我不必在任何地方都使用相同的代码.
祝你好运,我知道这个问题是一个巨大的痛苦.异常消息和谷歌是最无益的!
同样的问题在这里
复制本地必须设置为False!
我们开发了WPF解决方案(MVVM,20个项目)并实现了插件系统.我们的/ bin/Debug目录包含可执行文件,一些dll文件和包含插件的插件目录.有一个项目"DialogLib"(类库,一种对话框)定义了一个窗口(视图),ViewModel,Model和一些接口.其中一个插件使用了DialogLib的一个接口.窗口本身由主应用程序打开.
要在插件中使用"DialogLib"库的接口,我们必须将DialogLib的项目引用添加到插件项目引用中.应用程序启动时,插件已加载.如果用户然后选择菜单项,则窗口应该打开.此时,当后面的Windows代码尝试执行其InitializeComponent()时,出现错误"...组件没有由URI标识的资源...".
哪里出了问题?
问题是,当我们构建解决方案时,VS已正确创建了DialogLib.dll并将其复制到/ bin/Debug /.这是因为主应用程序文件想要打开窗口.但是DialogLib.dll也被复制到/ bin/Debug/plugins,因为其中一个插件引用它来使用DialogLib.dll中定义的接口之一.所以呢?
在运行时加载插件时,它使用/bin/Debug/plugins/DialogLib.dll中定义的接口.并且主应用程序文件尝试打开/bin/Debug/DialogLib.dll中定义的窗口.虽然文件相同,但VS遇到了麻烦.设置插件引用的DialogLib引用属性的Copy Local值可以避免将DialogLib.dll复制到/ bin/Debug/plugins,从而解决了问题.
我们在另一个项目中遇到了类似的问题(但是不同的错误),我们想要使用类型TypeA,它在dll文件中定义,在插件和主应用程序中定义.Copy Local设置为true,导致dll文件的副本位于../bin/Debug/plugins和../bin/Debug/中.原来,即使是相同的dll文件,该类型A在主应用程序文件和类型A在插件被视为不同的类型分别作为不能交换的类型.
我通过放置解决了这个问题
myusercontrol = Activator.CreateInstance<myusercontrol>();
Run Code Online (Sandbox Code Playgroud)
在包含InitializeComponent();行前的usercontrol的窗口的构造函数中
obj文件夹bin文件夹为我工作!
此外,如果使用加载程序集Assembly.LoadFile,请检查AppDomain.CurrentDomain.GetAssemblies()当前AppDomain中的重复程序集.因为在自动生成的WPF UserControl代码中,组件将使用其相对URI加载.由于当前AppDomain中存在重复的程序集,因此应用程序不知道要使用哪个程序集.
我在使用Visual Studio 2013时收到了同样的错误.
该组件没有uri标识的资源
尝试:
清理和重建解决方案 - 没有用.
关闭并打开Visual Studio - 无法正常工作.
解决方案:
进入项目bin目录并清除所有文件.
再次跑完项目,工作正常.
打开将在解决方案的根目录中打开的程序包管理器控制台,并运行以下powershell命令:
Get-ChildItem -inc bin,obj -recurse | Remove-Item -recurse -force -EA SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30343 次 |
| 最近记录: |