开发者

In M-V-VM where does my code go?

开发者 https://www.devze.com 2023-01-03 00:09 出处:网络
So, this is a pretty basic question I hope. I have a web service that I\'ve added through Add Service Reference. It has some methods to get list and开发者_开发技巧 get detail of a perticular table in

So, this is a pretty basic question I hope.

I have a web service that I've added through Add Service Reference. It has some methods to get list and开发者_开发技巧 get detail of a perticular table in my database.

What I'm trying to do is setup a UI as follows:

  1. App Load
    1. Load service proxy
    2. Call the GetList(); method display the results in a ListBox control
  2. User Double Clicks item in ListBox, display a modal dialog with a "detail" view

I'm extremely new to using MVVM, so any help would be greatly appreciated.

Additional information:

// Service Interface (simplification):

interface IService 
{
    IEnumerable<MyObject> GetList();
    MyObject GetDetail(int id);
}

// Data object (simplification)
class MyObject
{
    public int ID { get; set; }
    public string Name { get; set; }
}

I'm thinking I should have something like this:

MainWindow
    MyObjectViewUserControl 
        Displays list
        Opens modal window on double click

Specific Questions:

  1. What would my ViewModel class look like?
  2. Where does the code to handle the double click go?
    1. Inside the UserControl?
    2. What about Commands, would this be a good application of them?

Sorry for the long details, but I'm very new to the whole thing and I'm not educated enough to ask the right questions.

I checked out the MVVM Sample from wpf.codeplex.com and something isn't quite clicking for me yet, because it seems very confusing.


  1. Your VM would have:
    • an ObservableCollection that contains items that you bind to your ListBox.
    • CurrentItem that is bound to a currently selected item in the list
    • DelegateCommand that is bound to a double click and that invokes a logic to show a detail view (see MVVM ways of doing this, either a service or event aggregator like approach)
    • A logic to invoke invoke GetList() and then populate this colleciton
  2. The code to handle double click will go to VM (see above)


You will have 2 ViewModels because you have 2 views

1 ViewModel will have

  1. Your service proxy
  2. List
  3. ShowDetailFor(string selectedItemFromList) This should create a new instance of VM2 passing it the service proxy in ctor and invoke ShowDetail on VM2.

2 ViewModel will have

  1. MyObject (if it is just messagebox, no need to have ViewModel)
  2. ShowDetail(int id)

Once you have the ViewModel use WPF binding and command to connect data from VM to XAML

MVVM for user control becomes a bit more complicated, so if possible stay away from UserControls for now.


See the RegularExpressionViewModel example in my yodelay project.

The majority of your code will go in your ViewModel. The only code that should be in your view is code that wires the events of the View to the ViewModel. Usually no code will be required in the View since most of the wiring can be handled by DataBinding.

I use a presentation abstraction layer with Dependency Injection to communicate with dialogs.

0

精彩评论

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

关注公众号