开发者

My WPF ViewModelLocator implementation is failing composition within my VSTO Excel Add-In. Can anyone critique?

开发者 https://www.devze.com 2023-04-13 09:45 出处:网络
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 tryi

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 proper开发者_如何学运维ties 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号