刚学prism框架,查阅了很多文档资料,照着网上外文的例子做下去了,需要了解的概念有依赖注入、MVVM等,希望给以后学习的人一点引导什么的,同时也希望牛人分享关于prism心得。

   学习资料相关链接:

http://www.codeproject.com/Articles/229931/Understand-MVVM-Using-PRISM-by-Hello-World-Silverl#

http://msdn.microsoft.com/zh-cn/magazine/dd943055.aspx

http://www.cnblogs.com/626498301/archive/2010/08/17/1801475.html

http://www.cnblogs.com/li-xiao/archive/2011/04/20/2022962.html

   先看看我的demo吧:

   我的解决方案:

 

下面我们看从上带下看下代码:

ModuleHello

SayHiEvent.cs

using Microsoft.Practices.Prism.Events;

namespace ModuleHello.Events
{    
    public class SayHiEvent 
    {
        public string SayHi { get; set; }
    }
}

HelloView.xaml

<UserControl x:Class="ModuleHello.HelloView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cal="http://www.codeplex.com/prism"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
       <TextBlock Text="{Binding HelloText}" FontSize="26" 
        HorizontalAlignment="Center" VerticalAlignment="Center" />
        <Button Content="Button" cal:Click.Command="{Binding HelloCommand}" Height="23" HorizontalAlignment="Left" Margin="220,96,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

HelloView.xaml.cs

using System.Windows.Controls;

namespace ModuleHello
{
    public partial class HelloView : UserControl, ModuleHello.View.IHelloView
    {        
        public HelloView()
        {
            InitializeComponent();            
        }        
        #region IHelloView Members

        public ViewModel.IHelloViewModel Model
        {
            get
            {
                return this.DataContext as ViewModel.IHelloViewModel;
            }
            set
            {
                this.DataContext = value;
            }
        }
        #endregion       
    }
}

IHelloView.cs

using ModuleHello.ViewModel;

namespace ModuleHello.View
{
    public interface IHelloView
    {
        IHelloViewModel Model { get; set; }
    }
}

HelloViewModel.cs

using System.Windows;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;

namespace ModuleHello.ViewModel
{
    public class HelloViewModel : IHelloViewModel
    {
        public DelegateCommand HelloCommand { get; set; }

        private IEventAggregator eventAggregator;

        public HelloViewModel(View.IHelloView view,IEventAggregator eventHi)
        {
            HelloCommand = new DelegateCommand(SayHello);

            this.View = view;
            this.HelloText = "Hello (From hello view)";
            this.View.Model = this;

            this.eventAggregator = eventHi;
            eventAggregator.GetEvent<CompositePresentationEvent<ModuleHello.Events.SayHiEvent>>().Subscribe(OnSayHi);
        }
        public string HelloText { get; set; }

        public void OnSayHi(ModuleHello.Events.SayHiEvent sayHiEvent)
        {
            MessageBox.Show(sayHiEvent.SayHi);
        }        
        #region IHelloViewModel Members

        public View.IHelloView View
        {
            get;
            set;
        }

        public void SayHello()
        {
            MessageBox.Show("Hello");
            CompositePresentationEvent<ModuleHello.Events.SayHiEvent> myEvent =
                eventAggregator.GetEvent<CompositePresentationEvent<ModuleHello.Events.SayHiEvent>>();

            ModuleHello.Events.SayHiEvent sayHi = new Events.SayHiEvent
            {
                 SayHi="Hi"
            };
            myEvent.Publish(sayHi);
        }
        #endregion

    }
}

IHelloViewModel.cs

using ModuleHello.View;


namespace ModuleHello.ViewModel
{
    public interface IHelloViewModel
    {
        IHelloView View { get; set; }
    }
}

HelloModule.cs

using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using ModuleHello.View;
using ModuleHello.ViewModel;


namespace ModuleHello
{
    public class HelloModule : IModule
    {
        #region IModule Members
        private IRegionManager regionManager;
        IUnityContainer container;
        public void Initialize()
        {
            this.RegisterViewsAndServices();
            if (this.regionManager.Regions.ContainsRegionWithName("HelloRegion"))
            {
                this.regionManager.Regions["HelloRegion"].Add(
                    this.container.Resolve<IHelloViewModel>().View);
            }
        }
        public HelloModule(IRegionManager regionManager, IUnityContainer container)
        {
            this.container = container;
            this.regionManager = regionManager;
        }
        #endregion

        protected void RegisterViewsAndServices()
        {
            this.container.RegisterType<IHelloView, HelloView>();
            this.container.RegisterType<IHelloViewModel, HelloViewModel>();
        }
    }
}

PrismSample

App.xaml.cs

  private void Application_Startup(object sender, StartupEventArgs e)
        {
            Bootstrapper bootStrapper = new Bootstrapper();
            bootStrapper.Run();
        }

Bootstrapper.cs

using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;

namespace PrismSample
{
    public class Bootstrapper:UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            Shell rootShell = new Shell();
            Application.Current.RootVisual = rootShell;
            return rootShell;
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog modules = new ModuleCatalog();
            // You can add n number of module here
            modules.AddModule(typeof(ModuleHello.HelloModule));
            return modules;      
        }
    }
}

Shell.xaml

<UserControl x:Class="PrismSample.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:Regions="clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions >
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Sample MVVM Hello World Application" 
    FontSize="16" HorizontalAlignment="Center" Grid.Row="0" Grid.ColumnSpan="2"/>
        <ContentControl Grid.Row="1" Grid.Column="0" x:Name="HelloRegion" 
    Regions:RegionManager.RegionName="HelloRegion" HorizontalAlignment="Center" 
    VerticalAlignment="Center"/>
        <ContentControl Grid.Row="1" Grid.Column="1" x:Name="WorldRegion" 
    Regions:RegionManager.RegionName="WorldRegion" HorizontalAlignment="Center" 
    VerticalAlignment="Center"/>
    </Grid>
</UserControl>