0

I've made my first couple of C# .NET MVVM projects and i'd like to go on with this. So i thought it would be good to make my own simple - lets say - framework where i can put mvvm-stuff in which i need in most of my projects. I have created some controls and other helpful methods. So far so good.

Now i want to include a "generic" MainViewModel and a MainWindowView, which should give me the same surface in all of my projects into my Framework and i don't know whats the best way to do this.

This MainViewModel should be the basis for the other projects. I have declared a ribbon-menue with buttons i always need (like show, edit, save settings) and i have a method in it where i can add buttons into the ribbon-menue of my view with just one method call (AddButton(Command, Image, Label)), a command for opening a tabcontrol, a ReloadCommand, Fields to Repositories, a DialogService and methods to register workspaces in this ViewModel.

I also have a MainWindowView where i have my basic UI with a RibbonMenue, a StatusBar, ComboBoxes on the left and i just want to add application-specific TabItems!?

I don't really know whats the best way to "specify" this in the special application which uses the framework. I just thougt of Inheritance, or Reflection with Attributes or something like this.

What do would be the best way to use a MainViewModel and a MainWindowView in a framework?

Tobias
  • 232
  • 1
  • 16
  • 2
    It's not clear what your problem here is. Where do the reflection and attributes come from? – Daniel Hilgarth Aug 22 '13 at 08:01
  • sorry, i edited the question. i have e.g. some commands and i'd like to specify them in the projects – Tobias Aug 22 '13 at 08:16
  • 1
    Well if you want to have a VM implementation in the framework you have to consider, why you want to put it there. What's so special about your `MainViewModel`? Are you going to need it with the same implementation in other projects? – DHN Aug 22 '13 at 08:16
  • It should be the basis for the other projects. i have a declared a ribbon-menue with buttons i always need (like show, edit, save settings) and i have a method in it where i can add buttons into the ribbon-menue of my view with just one method call (AddButton(Command, Image, Label)), a command for opening a tabcontrol,... and i need this things in all of my other projects, so i thought it would be good to put it in a simple framework. Do you think that's not a good idea? – Tobias Aug 22 '13 at 08:34
  • 1
    So what's the problem? Put the class in the assembly and use where ever you like. – DHN Aug 22 '13 at 08:43

1 Answers1

1

I'm not exactly sure if we're on the same page, but I'm assuming that your MainViewModel class is like a 'base view model' class. I have a generic BaseViewModel class in my MVVM projects, but I also have generic BaseDataType classes as well that (both) expose the INotifyPropertyChanged interface to the extending classes.

In my applications, I have a series of Manager classes that implement the Singleton pattern (eg. there can be only one of each). The BaseViewModel class exposes these valuable manager classes to the extending view model classes. Each manager class provides some further functionality. For example, see the following list:

StateManager: maintains global data/object state across the whole application

DependencyManager: maintains dependency with a collection of interfaces and their concrete implementations

FeedbackManager: maintains access to the application feedback control (for user feedback)

WindowManager: provides access to file dialogs and child window management

ClipboardManager: provides access to the computer clipboard

UiThreadManager: provides access to multi-threading

EmailManager: provide access to be able to send e-mails

HardDriveManager: provides access to the user's computer hard drives

ExportManager: provides access to XML generation and FTP transfers

DataOperationManager: provides access to all data operations (explained further below)

UpdateManager: provides access to application updates

SecurityManger: provides access to all security matters

ExcelManager: provides access to functionality that generates Excel documents based on view model data.

All of these manager classes are accessible from any view model that extends the BaseViewModel class.

In addition to this, my (abstract) BaseViewModel also exposes certain Command objects that are required in every view, like 'Save', Delete', 'Refresh' etc.

Finally, it also provides access to often used functionality like 'InsertNewDataTypeToCollection' and 'RemoveDataTypeFromCollection' methods.

As well as this part of the 'framework', I also have a number of base data type classes. These provide common properties and often used functionality like data synchronisation, animation, data error reporting (an extension of the 'IDataErrorInfo' interface) which automatically links in with the feedback control.

The final part of my system revolves around database access. I have a suite of classes that I wrap around every data object that comes or goes to the database. These automatically provide user feedback in the UI, asynchronous operations, and error logging and handling.

I hope that this helps you and that I didn't misunderstand your question.

UPDATE >>>

Oooops, I forgot to mention the most important part... I have a MainViewModel class that extends BaseViewModel and is displayed in the MainWindow.xaml. In this view model, there is a public property of type BaseViewModel:

public BaseViewModel ViewModel
{
    get { return viewModel; }
    set 
    {
        if (viewModel != value) 
        { 
            viewModel = value;
            NotifyPropertyChanged("ViewModel");
        } 
    }
}

I can set this property to any view model class that has extended the BaseViewModel class. In MainWindow.xaml, I have the following setup:

...
<Grid Grid.Row="1" Background="{StaticResource Windows7LightBackground}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ContentControl Grid.RowSpan="2" Content="{Binding ViewModel}" Margin="5" />
</Grid>
<Controls:FeedbackControl Grid.Row="0" Feedback="{Binding FeedbackManager.Feedback}" 
VerticalAlignment="Top" HorizontalAlignment="Stretch" MaxWidth="750" 
Margin="100,22,100,0" />
...

In the first Grid row, I have a Ribbon control, in the second I have a ContentControl that displays the view that matches the view model that is set in the ViewModel property and then I have a custom FeedbackControl that slides in and out of view when feedback arrives.

The final piece of this puzzle is linking the views with the view models. This is done in the App.xaml file using DataTemplate objects:

...
<DataTemplate DataType="{x:Type ViewModels:HomeViewModel}">
    <Views:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
    <Views:MainView />
</DataTemplate>
...
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • yeah we're on the right way. :) so you have an abstract ViewModel called BaseViewModel where you put that managers in. do you also know how i can handle it when i also have a MainWindowView where i have my basic UI with a RibbonMenue, a StatusBar, and ComboBoxes on the left and just want to add application-specific TabItems? Do you understand what i mean? :) – Tobias Aug 22 '13 at 14:02
  • I've updated my answer for you. – Sheridan Aug 22 '13 at 15:15
  • ok, you handle it with a viewmodel-property. looks good :) thanks a lot for this detailed answer! – Tobias Aug 23 '13 at 05:21
  • sorry for another question, but do you have an idea how to inherit from a view in the xaml code? is that possible? for explanation: i'd like to have a `BaseViewWindow` in my framework. and i'd like to have `UserControls` in my application which are shown in `TabCommands` of my `BaseViewWindow`. So i'd like to use the `View` in my `Framework` and not creating the same View in each application. – Tobias Aug 26 '13 at 08:15
  • In addition to the last comment: I've read your question: http://stackoverflow.com/questions/5916930/using-mvvm-in-wpf-should-i-lauch-child-windows-from-view-code-behind-or-viewmo So is it possible for me to make the `BaseViewWindow` in my Framework, a `MainViewWindow` in my application which is something like a `ViewContainer` as daub815 has mentioned in his answer, and the rest are some `UserControl`s which are opened in `TabCommand`s? If this is possible, I don't know how i can call the `BaseViewWindow` in my Application. Perhaps in the App.xaml as StartupUri? – Tobias Aug 26 '13 at 09:03
  • @TobiasGötzendorfer, this post is long enough already and at StackOverflow, we ask one question per post. I can't give you a proper answer in the comments, so please ask another question, providing as much information about what you want as possible. I'll take a look for it on Monday, or you can add my @ name (like I added yours at the start of this comment) to your post and then I'll get sent a notification. – Sheridan Aug 26 '13 at 15:17
  • Ok you're right. I've posted a new question here: http://stackoverflow.com/questions/18466902/how-can-i-use-a-view-from-a-framework-in-an-application-wpf-mvvm – Tobias Aug 28 '13 at 10:13