by jasonrshaver
31. January 2012 12:42
So, you enjoy the MVVM pattern for your Silverlight or Windows Phone 7 application, but you can't figure out how to perform your navigation from your ViewModel? Here is a guide that allows you to do this from your ViewModel:
- // Option 1
- GotoProductCatalogCommand = new RelayCommand(
- () => Navigate("/Views/ProductCatalogPage.xaml"));
- // Option 2, now without strings!
- GotoProductCatalogCommand = new RelayCommand(
- () => Navigate<ProductCatalogPageViewModel>());
The first option allows you to give a URI to navigate to, just like you would expect, but option two allows your application to navigate based on convention. Never miss-type a URI again!
All you have to do is add the following to your ViewModel base class or MVVM Light’s ViewModelBase:
- public void Navigate<T>()
- {
- var UriFragment = String.Format(@"/Views/{0}.xaml"
- , typeof(T).Name.Replace("ViewModel", ""));
- Navigate(UriFragment);
- }
- public void Navigate(String uriFragment)
- {
- var MyFrame = Application.Current.RootVisual as Frame;
- MyFrame.Navigate(new Uri(uriFragment, UriKind.Relative));
- }
- public void GoBack()
- {
- (Application.Current.RootVisual as Frame).GoBack();
- }
Notice that my ‘convention’ for Views is to basically take the type name of your ViewModel, remove the string “ViewModel” and look up the file in the “/Views” directory. Obviously you can move to a more comprehensive convention as your needs require.
by jasonrshaver
30. January 2012 22:39
I am a fan of keeping strings out of my .NET Applications. In the past I used CSLA and enjoyed the way it loaded properties and change notification without using strings. Because why work now-a-days is all about creating samples, I use MVVM Light instead.
For those who don’t understand the what I am talking about, take a look at this example:
// Normal RaiseProperyChanged
RaisePropertyChanged("Products");
// Statically-Typed RaisePropertyChanged
RaisePropertyChanged(() => Products);
To get the same functionality, add the following method to your class (or ViewModel base class):
public void RaisePropertyChanged<T>(Expression<Func<T>> property)
{
RaisePropertyChanged(property.GetMemberInfo().Name);
}
And if you don’t already have the GetMemberInfo() extension method, Add the following class to your application:
using System.Linq.Expressions;
using System.Reflection;
namespace System.Linq.Expressions
{
public static class ReflectionExtensionMethods
{
public static MemberInfo GetMemberInfo(this Expression expression)
{
MemberExpression operand;
LambdaExpression lambdaExpression = (LambdaExpression)expression;
if (lambdaExpression.Body as UnaryExpression != null)
{
UnaryExpression body = (UnaryExpression)lambdaExpression.Body;
operand = (MemberExpression)body.Operand;
}
else
{
operand = (MemberExpression)lambdaExpression.Body;
}
return operand.Member;
}
}
}
by jasonrshaver
23. January 2012 13:54
Let’s just say you want to make a PivotViewer for the Periodic Table of Elements. And lets just say you want it to look something like you remember it in your middle school science book. You get the data, add a ‘Group’ value to control the columns, but the vertical order is all wrong.
Here is a quick way to fix this. I don’t think anyone documented the method for doing this before, so if you have any questions, let me know!
And add the following to your PivotViewer XAML
<pv:PivotViewer x:Name="myPivotViewer" ItemsSource="{Binding}" BorderThickness="0" ViewChanged="myPivotViewer_ViewChanged">
And in code behind, add:
- private System.Windows.Controls.Pivot.PivotViewerView _OldView = null;
-
- private void myPivotViewer_ViewChanged(object sender, EventArgs e)
- {
- if (_OldView != null)
- {
- _OldView.PivotViewerViewUpdating -=
- new EventHandler<PivotViewerViewUpdatingEventArgs>
- (View_PivotViewerViewUpdating);
- }
- _OldView = myPivotViewer.View;
- _OldView.PivotViewerViewUpdating +=
- new EventHandler<PivotViewerViewUpdatingEventArgs>
- (View_PivotViewerViewUpdating);
- }
-
- private void View_PivotViewerViewUpdating(object sender
- , PivotViewerViewUpdatingEventArgs e)
- {
- e.CollectionView.SortDescriptions.Add(
- new SortDescription("AtomicNumber"
- , ListSortDirection.Ascending));
- }
It is very important to make sure you are cleaning up your PivotViewerViewUpdating event reference as shown!
And Ta-Da, you have the following work of art:

Sorry for making such a quick example without any downloadable source, but I figured it was good to get this information out there!