我的 WPF ViewModelLocator 实现在 VSTO Excel 加载项中组合失败。有人可以批评一下吗?

发布于 2024-12-10 02:34:28 字数 1356 浏览 0 评论 0 原文

背景

我正在使用 MEF 帮助编写一个 Excel 2007 插件 (VSTO),它可以从电子表格数据创建一个实体,而蠕变要求暴露了我的体系结构中的一个缺点,我正在尝试解决这个问题协调(我现在需要识别“模板加载”上的列,而不是“工作表提交”上的列)

我的每个本地实体属性都用包含工作表中列名称的 DescriptionAttribute 进行装饰。提交时,我会反思这些属性,以确定哪些属性映射到该工作表列,并创建一个将 PropertyName 映射到列序号的字典对。我这样做是因为 Excel COM 对象模型似乎只根据行/列序数公开工作表的单元格。执行此映射允许我的加载项在工作表上动态定位我的属性,而无需维护任何静态属性到序数的映射。

所以,这一切都很好......而且,由于这是我们重新架构底层系统时业务合作伙伴的权宜之计,我们讨论这将是一个非常“不友好”的插件,每个人一开始都船上。但现在,企业对表格中显示的一些参考数据存在验证问题。我们同意在提交时执行验证,但情况已发生变化,需要我将参考列绑定到仅包含有效值的验证下拉列表。

问题

所以现在,我需要重构我的 View 和 ViewModel,以便在加载视图时将列映射到其实体属性,这就是问题开始的地方。我从 Josh Smith 提供的代码中实现了他的 RelayCommand,我实现了 Reed Copsey Jr 的 WPF CompositionInitializer,我创建了一个 ViewModelLocator 来帮助我的合成容器找到我的 ViewModel...但无论我做什么,我都无法找到合适的 ViewModel。我现在对这个问题有点紧张……花了整个周末试图解决它,但无济于事。有人可以帮忙吗?我不是 MEF 专家。我曾涉足 MVVM 模式,但过去几年我一直在 C# 之外,错过了它的大部分演变。我认为我的问题很可能是根本性的,但我已经盯着我的代码太久了才找到它。请帮忙。

代码

我已经在 Microsoft WPF 社交论坛上发表了一篇文章,在那里我已经发布了相关代码。如果有人希望我将其转发到这里,我将很乐意效劳。但现在,我将添加该线程的链接。 (我也在该线程中添加了背景,但我对这里的背景更满意。您可以跳过我在那里添加的背景,不会错过任何内容)

http://social.msdn.microsoft.com/Forums/en/wpf/thread/f22f081d-e342-4f4e-af41-600cec68f0cd

Background:

I'm using MEF to help compose an Excel 2007 Add-In (VSTO) that can create an entity from spreadsheet data and a creep requirement exposed a shortcoming in my architecture that I'm trying to reconcile(I am now needing to identify columns on "template load" rather than on "worksheet submit")

I have each of my local entity properties decorated with a DescriptionAttribute that contains the name of the column in my worksheet. On submit, I reflect on these attributes to identify which property maps to that worksheet column and create a Dictionary of pairs that map a PropertyName to a column ordinal. I do this because the Excel COM object model seems to only expose the cells of a worksheet based upon row/column ordinals. Performing this mapping allows my add-in to dynamically locate my properties on the worksheet without having to maintain any static property-to-ordinal map.

So, this was all fine and good...and, as this is a stopgap solution for our business partner while we rearchitect the underlying system, we discussed that this was going to be a very "unfriendly" add-in and everyone was intitially onboard. But now, the business has validation concerns for some reference data that will be showing up in the sheet. We had agreed that validation would be performed on submission, but that has changed to need me to have reference columns bound to validation drop downs that only contain valid values.

The Problem

So now, I've been needing to restructure my View and ViewModel such that the mapping of the columns to their entity properties occurs on loading of the View, and that's where the problems have started. I implemented Josh Smith's RelayCommand from code that he has made available, I implemented Reed Copsey Jr's CompositionInitializer for WPF, and I created a ViewModelLocator to assist my compositioncontainer in locating my ViewModel...but whatever I do, I cannot locate the appropriate ViewModel. I'm a bit strung out on this issue at the moment..spent all weekend trying to fix it, to no avail. Can anyone please help? I'm not a MEF expert. I've dabbled with the MVVM pattern, but I've been outside of C# for the past few years and missed much of its evolution. I think it's highly likely that my problem is fundamental, but I have been staring at my code for too long to find it. Please, help.

The Code

I have begun a post in the Microsoft social forum for WPF where I have already posted the relevant code. If anyone would like me to repost it here, I would be happy to oblige. But for now, I will include a link to that thread. (I included a background in that thread as well, but I'm happier with the one here. You can skip the background I included over there and not miss anything)

http://social.msdn.microsoft.com/Forums/en/wpf/thread/f22f081d-e342-4f4e-af41-600cec68f0cd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-12-17 02:34:28

我不确定这是否会产生任何影响,但不要导出您的 StopgapViewModel 类,而是创建一个工厂类并导出该工厂类,如下所示:

public StopgapViewModelFactory 
{
    [Export(typeof(StopgapViewModel))]
    public StopgapViewModel Instance
    {
        get
        {
            return new StopgapViewModel();
        }
    }
}

StopgapViewModel 中删除 [Export] 声明 类,然后尝试一下。

I am uncertain whether this will have any impact, but instead of exporting your StopgapViewModel class, create a factory class and export the factory class, like so:

public StopgapViewModelFactory 
{
    [Export(typeof(StopgapViewModel))]
    public StopgapViewModel Instance
    {
        get
        {
            return new StopgapViewModel();
        }
    }
}

Remove the [Export] declaration from the StopgapViewModel class, and give it a go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文