Archiv der Kategorie: Silverlight

Easy usage of INotifyPropertyChanged with Property-Attribute

Hello everyone,

today I want to share an implementation I’m using in my personal prototyping-Framework for Silverlight. This is important to mention, because I have tested it only for “works” but not including any productive parameters like memory-usage or processing-time.

The meaning of a prototyping-framework for me is to get thinks working as quick as possible, to show the outcome to my customer. There are some things I need very often, and so I’m using base-classes a lot.

Easy usage of INotifyPropertyChanged with Property-Attribute weiterlesen

Generic View Models in Silverlight

I like using Base-Classes for any kind of “Class-Group”. Very prominent groups are Models and ViewModels in Silverlight-Applications.

The ViewModel-Baseclass can hold any functionality, that applies to every ViewModel inheriting from it and, if we are talking about Generics, can be very convenient to use.

For example, if I want to apply the factory-pattern to my ViewModels and want every Model to have a static Create-Method I can simply implement this Method in the Base-Class.

public class ViewModelBase 
{ 
	public virtual ViewModelBase Create() 
	{ 
		return new ViewModelBase(); 
	} 
} 

Of cause, without Generics we are limited to the return-Type. We could now override this method in every specific ViewModel to at least return the right ancestor of ViewModelBase. An easier way is, to use Generics for this case. The class could look like this :

public class ViewModelBase where TViewModel : ViewModelBase 
{ 
	public virtual TViewModel Create() 
	{ 
		return Activator.CreateInstance(typeof(TViewModel)); 
	} 
} 

Now we can simply create our new specific ViewModel like this

public class MyViewModel : ViewModelBase

to get what we want generically.

 

Another possible scenario would be the following. We know, every ViewModel contains a Collection of Models, all inheriting from a class called ModelBase. we could extend out code the following way:

public class ViewModelBase where TViewModel : ViewModelBase 
{ 
	public virtual TViewModel Create() 
	{ 
		return Activator.CreateInstance(typeof(TViewModel)); 
	} 
	
	public List MyList {get;set;} 
} 

or, the better way, we could extend our Generic header to be type safe.

public class ViewModelBase 	
                                        where TViewModel : ViewModelBase 
					where TChildModel : ModelBase 
{ 
	public virtual TViewModel Create() 
	{ 
		return Activator.CreateInstance(typeof(TViewModel)); 
	} 

	public List MyList {get;set;} 
} 

Defining a Class by

public class MyViewModel : ViewModelBase

Like always, if we talk about inheritance, you should avoid to go too far with that. I implemented a Framework I use for rapid prototyping where a lot of things are done in the base-class via reflection, but in a productive environment its not always what you want. Plan your development as precise as possible.

  • What do you want to be individually implemented by each ViewModel ?
  • When is it a good thing to implement it in the Base-Class ?
  • How confusing is you inheritance for other developers ?

 

Good Luck 😮

Styles from different files as static resources

Hello everyone,

to start a silverlight project, i have a kind of default-template which I enriched with a CSS-like decentralized style today. This is done very easy now in Silverlight 3. First you habe to set up a file containing your style. In my case it was a file in a new Styles folder called TextBock.Default.xaml where I entered this dummy code:


    
        
        
        
        
    

after that, I created a MergedDictionary in my app.xaml like that:

 
         
             
                 
             
          
     

from now on, you can call this style like you’re used to with {StaticResource TextBlock.Default} within your application. Nice way to manage the styles as I think, and much easier to maintain than having all your styles plugged in your app.xaml in total.

Have fun playing with it :o)
Sascha

InitParams with Out-Of-Browser Apps

Hello everyone,

I just tried to figure out, how to pass initial parameters to a Silverlight App, that runs out of browser. We dont have a Webpage to pass InitParams and we cannot access Web.Config entries.

So i came up with the idea, to use Isolated Storage for this task. Before installing an App Out-Of-Browser we need to access the Webpage. In this case, we can read the initial paramters from Web.Config or from Silverlight-Tag, and store them to Isolated Storage.

Now we need a code-block to check, if app runs OOB (Application.Current.IsOutOfBrowser) and in this case, we read the paramters from Isolated Storage instead. So the OOB-App is configured the same way as the Web-App.

This was just one Idea, that worked. Any other suggestions are very welcome.