Prism研究之一: Silverlight HelloWorld Demo

1.首先建一个Silverlight项目:HelloWorld.Silverlight.PrismSample

2.在解决方案中add新项目Silverlight类库HelloWorld.Silverlight.HelloWorldModule

 在引用中添加

  Microsoft.Practices.Composite.dll
 Microsoft.Practices.Composite.Presentation.dll

HelloWorldModule.cs代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using HelloWorld.Silverlight.HelloWorldModule.Views;

namespace HelloWorld.Silverlight.HelloWorldModule
{
public class HelloWorldModule : IModule
{
private readonly IRegionManager regionManager;



public void Initialize()
{
regionManager.RegisterViewWithRegion(
"MainRegion", typeof(HelloWorldView));
}



public HelloWorldModule(IRegionManager regionManager)
{
this.regionManager = regionManager;

}
}
}

3.在HelloWorld.Silverlight.HelloWorldModule 项目中add一个文件夹Views

views 中的HelloWorldView.xaml代码
<UserControl x:Class="HelloWorld.Silverlight.HelloWorldModule.Views.HelloWorldView"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center"
VerticalAlignment
="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
</Grid>
</UserControl>

以下在HelloWorld.Silverlight.PrismSample项目中

在引用中添加下面5个的引用

    Microsoft.Practices.Composite.dll
 Microsoft.Practices.Composite.Presentation.dll
 Microsoft.Practices.Composite.UnityExtensions.dll
 Microsoft.Practices.ServiceLocation.dll
 Microsoft.Practices.Unity.dll

再添加项目引用HelloWorld.Silverlight.HelloWorldModule

4.MainPage 改成Shell

Shell.xaml
<UserControl x:Class="HelloWorld.Silverlight.PrismSample.Shell"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Regions
="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.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" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<ItemsControl Name="MainRegion" Regions:RegionManager.RegionName="MainRegion"/>
</Grid>
</UserControl>
App.xaml.cs代码
private void Application_Startup(object sender, StartupEventArgs e)
{
//this.RootVisual = new Shell();

var bootStrapper
= new Bootstrapper();

bootStrapper.Run();

}

5.添加类Bootstrapper.cs

Bootstrapper.cs代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Composite.Modularity;

namespace HelloWorld.Silverlight.PrismSample
{
public class Bootstrapper : UnityBootstrapper
{

protected override DependencyObject CreateShell()
{
Shell shell
= Container.Resolve<Shell>();
Application.Current.RootVisual
= shell;
return shell;
}


protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog
= new ModuleCatalog()
.AddModule(
typeof(HelloWorld.Silverlight.HelloWorldModule.HelloWorldModule));
return catalog;
}
}
}
原文地址:https://www.cnblogs.com/honeymoon/p/1732103.html