Tag Archives: WPF

Generic Types with Complex Constraints

I am working on a WPF app, leveraging Prism and Unity.

I have a scenario where I have lots of similar views/viewmodel. Not exactly alike, but 90%. I have 22 dimension-type datatypes. Instead of building out 88 individual MVVM scenarios, I am doing a cut-n-paste for the 22 instance of each specific activity (CRUD/Navigation). I needed to figure out how to define a generic base viewmodel that enforced inheritance for the generic type.

BTW: This is a throw-away app with an expected <3yr lifespan. It’s not worth the time/effort in building up something truly dynamic. I need to slam it out and pass it off to off-shore support. If it’s too complicated, I won’t be able to hand it off. This falls under, ‘Just get it done,’

All of the samples I could find were ‘ClassName<T,U> where T : class where U class’ examples. That doesn’t work for what I need to do. Below is how I solved the class definition issue.

public class DimensionBase : BindableBase, IChangeTracking 
{
...
}
public abstract class CollectionViewModelBase<T> : BindableBase, INavigationAware, IActiveAware where T : DimensionBase 
{
...
}
public abstract class DetailViewModelBase<T> : BindableBase, INavigationAware where T : DimensionBase 
{
...
}
public abstract class EditViewModelBase<T> : BindableBase, INavigationAware, IConfirmNavigationRequest where T : DimensionBase 
{
...
}

What I learned is that you can tack on the where clause to the end of the class definition to enforce constraints on the ‘T’ after all of the constraints are applied to the base class itself. I had been trying to manage the ‘T’ constraints first which is following my understanding of the ‘standard’ examples of where T … where U …

The delete action is managed by the collection view model, by way of the confirmation dialog hosted in the shell. I use events to manage that process.

There one more thing I want to clarify. I am using the region manager to handle all view-based navigation. I am using the event aggregator for passing messages around the system. The biggest one is telling all of the views to refresh their data when the target environment has changed. Getting navigation/confirmation dialog to fire, leverages events because the views hosted by separate modules (24 so far) that cannot directly access the notification/clarification dialogs hosted in the shell. Nor does Prism enable hosting these dialogs in a region (as far as I can tell). So there is only one way to do it. Events.

Enjoy.

Rotating a XAML object without using the Rotate Transformation

Problem:

I needed to take a stock XAML ‘image’ for a search icon from SyncFusion’s Metro Studio and rotate it about 75 degrees but I couldn’t use the rotate transformation code to do it because all of the required data had to be contained in the path description/geometry.

If you need a reference for how to do a normal transformation, try the Transforms Overview on MSDN.

Context:

Last year, I needed to set a logo for a new app I was developing using the ModernUI shell (CodePlex/GitHub).  The logo is set in as a property of the ModernWindow as LogoData.  LogoData is a raw path.  So I couldn’t use anything except the XAML defined geometry to describe the logo.      Continue reading Rotating a XAML object without using the Rotate Transformation

ToolBarTray Adapter

There isn’t a native ToolBarTray adaptor built into the Prism library. Creating an adapter is only a matter of learning the secret handshake.

It takes three parts to get an adaptor to work. The first part is the adaptor itself. The second is wiring it into the bootstrapper. The third part is the XAML to define the region. Below are samples of all three.

Continue reading ToolBarTray Adapter

Setting Theme/Accent with MahApps.Metro

I’m building up a sample app that leverages the MahApps.Metro libraries.  One of the challenges is setting the AppTheme and Accent colors dynamically.   I looked through more than a few online solutions and none of them worked for me.  Usually, the syntax was from an earlier version and didn’t match the ThemeManager object that I downloaded from NuGet (v1.0.0.0).

After comparing the sample app (v4.5) against my code in VS, I noticed the results from ThemeManager.DetectAppStyle() was always null in my app and valid in the sample app.   Continue reading Setting Theme/Accent with MahApps.Metro