Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to pass in an "outer" generic collection type into a class without specifying the inner type of the items that the collection contains?
    primarykey
    data
    text
    <p>I'm writing an app with MVVM architecture. I have a View library and a ViewModels library.</p> <p>The ViewModel needs to persist a collection of things. I want the ViewModel to be abstracted from what the concrete type of the collection is, but know only that it derives from ObservableCollection and implements an interface called IIncrementalObservableCollection.</p> <pre><code>public class ViewModel&lt;TObservableCollection&gt; : ViewModelBase where TObservableCollection : ObservableCollection&lt;ThingType&gt;, IIncrementalObservableCollection&lt;ThingType&gt;, new() </code></pre> <p>I have a class inside the View layer that provides a custom ObservableCollection implementation, like this:</p> <pre><code>public class IncrementalObservableCollection&lt;T&gt; : ObservableCollection&lt;T&gt;, ISupportIncrementalLoading, IIncrementalObservableCollection </code></pre> <p>In the View layer, I can instantiate this ViewModel like this:</p> <pre><code>new ViewModel&lt;IncrementalObservableCollection&lt;ThingType&gt;&gt;(null, null); </code></pre> <p>However, the problem is that ThingType is a model object. The View shouldn't be telling the ViewModel what type of objects should be inside the collection.</p> <p>So in a nutshell; I want to be able to pass in a collection type to a ViewModel, but then have the View Model be responsible for the instantiation of the type and crucially what types will be held in the collection.</p> <p>So I'd like to be able to instantiate the ViewModel like this: <code>new ViewModel&lt;IncrementalObservableCollection&gt;(null, null);</code> - without the ThingType generic parameter.</p> <p><strong>Edit for clarification</strong> My View layer is a Windows 8 app, my ViewModel layer is a PCL. I have a GridView declared in xaml inside the Views layer which is bound to an ObservableCollection on the ViewModel. The trouble is, for the GridView to support incremental loading of data, the ObservableCollection must implement ISupportIncrementalLoading. I cannot do this in the ViewModel layer because, as a PCL, it does not have access to ISupportIncrementalLoading. So I think I need to create a concrete type inside the View layer which inherits from ObservableCollection and ISupportIncrementalLoading... then also implement a custom version of ISupportIncrementalLoading (called IIncrementalObservableCollection) which is defined in the ViewModel layer. </p> <p>So fundamentally, I want the ViewModel to utilise the concrete collection class provided by the View layer. So my problem boils down to: GridView needs ISupportIncrementalLoading on a ViewModel property, but the ViewModel layer does not have access to ISupportIncrementalLoading.</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload