Category: "Example Codes"
MVC 3 Example
I have completed the users section for AdvGenCMS. I used ASP.Net MVC 3 and Razor to do. It has CRUD functions and used the LINQ Repository Pattern. I think that is a good example for MVC3 in C#.
Please download the source and check it out http://localhost:44271/Account/List page in your own development box.
Custom Membership Provider
Custom Membership Provider is easy to do. Firstly, please extends MembershipProvider
Code
public class AdvCMSMembershipProvider : MembershipProvider |
Secondly, Right Click MembershipProvider, and then implements all abstract methods.
The only method you need to change:
Code
public override bool ValidateUser(string username, string password) | |
{ | |
IAdvUser user = userRepository.GetByUserName(username); | |
| |
return user.Password == password; | |
} |
Lastly, chaging web.config
Code
<membership defaultProvider="AdvCMSMembershipProvider"> | |
<providers> | |
<clear/> | |
<add name="AdvCMSMembershipProvider" | |
type="AdvCMS.Security.AdvCMSMembershipProvider, AdvCMS.Security" /> | |
</providers> | |
</membership> |
Please download our opensource project, AdvGenCMS, as the example.
Timer in WPF
In Windows Form time, there is a component for Timer. But in WPF, there is not such things. So, what thing we can do, when we need to execute a piece of code periodically. We can use the timers under System.Timers. But please do not use System.Timers.Timer, because it triggered, it will execute the code in a new thread. If you try to access any controls in WPF Windows, the system will throw an exception. I like to use System.Windows.Threading.DispatcherTimer in WPF, because that is very similar with the Timer in Windows Timer.
There are the example code:
Code
DispatcherTimer sysTimer = new DispatcherTimer(); | |
sysTimer.IsEnabled = true; | |
sysTimer.Interval = new TimeSpan(0, 0, 1); | |
sysTimer.Tick += new EventHandler(sysTimer_Tick); |
To find more about this, please download the example project, AdvGenStopWatch.
WPF Example Code - AdvGenStopWatch
I created an opensource project, AdvGenStopWatch. This is a simple stop watch program and aimed to show how to use WPF. Including the following features:
- How to use Grid Layout and ViewPoint, this make all elements will be autosize.
- How to use Timer in WPF
- How to use ListView
I believe this is a good starting point to learn WPF.
How to put your PC in sleep or hibernate via C#
I thought that is impossible to put your PC in sleep or hibernate via C#. I found that is incorrect! Moreover, that is very simple too, just one line of command.
Putting the PC in sleep mode
Code
Application.SetSuspendState(PowerState.Suspend, false, false); |
Putting the PC in Hibernate mode
Code
Application.SetSuspendState(PowerState.Hibernate, false, false); |
That is so simple!!!