آموزش WAF (بررسی ساختار همراه با پیاده سازی یک مثال)
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۳/۰۱/۱۳ ۱۶:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public interface IBookView : IView
{
void Show();
void Close();
} Install-Package WAF
<Window x:Class="Shell.BookShell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Book View" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Books}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" Height="200">
<DataGrid.Columns>
<DataGridTextColumn Header="Code" Binding="{Binding Code}" Width="100"></DataGridTextColumn>
<DataGridTextColumn Header="Title" Binding="{Binding Title}" Width="300"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window> [Export(typeof(IBookView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class BookShell : Window, IBookView
{
public BookShell()
{
InitializeComponent();
}
} public class Book
{
public int Code { get; set; }
public string Title { get; set; }
} [Export]
[Export(typeof(ViewModel<IBookView>))]
public class BookViewModel : ViewModel<IBookView>
{
[ImportingConstructor]
public BookViewModel(IBookView view)
: base(view)
{
}
public ObservableCollection<Book> Books { get; set; }
} [Export]
public class BookController
{
[ImportingConstructor]
public BookController(BookViewModel viewModel)
{
ViewModelCore = viewModel;
}
public BookViewModel ViewModelCore
{
get;
private set;
}
public void Run()
{
var result = new List<Book>();
result.Add(new Book { Code = 1, Title = "Book1" });
result.Add(new Book { Code = 2, Title = "Book2" });
result.Add(new Book { Code = 3, Title = "Book3" });
ViewModelCore.Books = new ObservableCollection<Models.Book>(result);
(ViewModelCore.View as IBookView).Show();
}
} public class AppBootstrapper
{
public CompositionContainer Container
{
get;
private set;
}
public AggregateCatalog Catalog
{
get;
private set;
}
public void Run()
{
Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
Catalog.Catalogs.Add(new AssemblyCatalog(String.Format("{0}\\{1}", Environment.CurrentDirectory, "Shell.dll")));
Catalog.Catalogs.Add(new AssemblyCatalog(String.Format("{0}\\{1}", Environment.CurrentDirectory, "ViewModels.dll")));
Catalog.Catalogs.Add(new AssemblyCatalog(String.Format("{0}\\{1}", Environment.CurrentDirectory, "Controllers.dll")));
Container = new CompositionContainer(Catalog);
var batch = new CompositionBatch();
batch.AddExportedValue(Container);
Container.Compose(batch);
var bookController = Container.GetExportedValue<BookController>();
bookController.Run();
}
}
نکته بخش Startup را از فایل App.Xaml خذف نمایید و در متد Startup این فایل کد زیر را کپی کنید:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new Bootstrapper.AppBootstrapper().Run();
}
}
Catalog.Catalogs.Add(new DirectoryCatalog(Environment.CurrentDirectory));