Created
May 13, 2013 08:02
-
-
Save DotNetNerd/5566822 to your computer and use it in GitHub Desktop.
WPF IOC wireup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Application x:Class="WpfApplication1.App" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Startup="App_OnStartup"> | |
<Application.Resources> | |
<Style TargetType="Button"> | |
<Setter Property="FontSize" Value="20" /> | |
</Style> | |
</Application.Resources> | |
</Application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : Application | |
{ | |
private void App_OnStartup(object sender, StartupEventArgs e) | |
{ | |
var container = TinyIoC.TinyIoCContainer.Current; | |
container.Register<IWindow, MainWindowAdapter>(); | |
container.Resolve<IWindow>().Show(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IWindow | |
{ | |
void Close(); | |
//IWindow CreateChild(object viewModel); | |
void Show(); | |
bool? ShowDialog(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainWindowAdapter : WindowAdapter | |
{ | |
//private readonly INewUserViewModelFactory vmFactory; | |
private bool initialized; | |
public MainWindowAdapter(MainWindow wpfWindow) //, INewUserViewModelFactory viewModelFactory | |
: base(wpfWindow) | |
{ | |
//if (viewModelFactory == null) | |
//{ | |
// throw new ArgumentNullException("viewModelFactory"); | |
//} | |
//this.vmFactory = viewModelFactory; | |
} | |
#region IWindow Members | |
public override void Close() | |
{ | |
this.EnsureInitialized(); | |
base.Close(); | |
} | |
//public override IWindow CreateChild(object viewModel) | |
//{ | |
// this.EnsureInitialized(); | |
// return base.CreateChild(viewModel); | |
//} | |
public override void Show() | |
{ | |
this.EnsureInitialized(); | |
base.Show(); | |
} | |
public override bool? ShowDialog() | |
{ | |
this.EnsureInitialized(); | |
return base.ShowDialog(); | |
} | |
#endregion | |
private void DeclareKeyBindings(NewUserViewModel vm) | |
{ | |
this.WpfWindow.InputBindings.Add(new KeyBinding(vm.OkCommand, new KeyGesture(Key.F5))); | |
} | |
private void EnsureInitialized() | |
{ | |
if (this.initialized) | |
{ | |
return; | |
} | |
var vm = new NewUserViewModel(this); //same view for both viewmodels? | |
this.WpfWindow.DataContext = vm; | |
this.DeclareKeyBindings(vm); | |
this.initialized = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WindowAdapter : IWindow | |
{ | |
private readonly Window wpfWindow; | |
public WindowAdapter(Window wpfWindow) | |
{ | |
if (wpfWindow == null) | |
{ | |
throw new ArgumentNullException("window"); | |
} | |
this.wpfWindow = wpfWindow; | |
} | |
#region IWindow Members | |
public virtual void Close() | |
{ | |
this.wpfWindow.Close(); | |
} | |
//public virtual IWindow CreateChild(object viewModel) | |
//{ | |
// var cw = new ContentWindow(); | |
// cw.Owner = this.wpfWindow; | |
// cw.DataContext = viewModel; | |
// ConfigureBehavior(cw); | |
// return new WindowAdapter(cw); | |
//} | |
public virtual void Show() | |
{ | |
this.wpfWindow.Show(); | |
} | |
public virtual bool? ShowDialog() | |
{ | |
return this.wpfWindow.ShowDialog(); | |
} | |
#endregion | |
protected Window WpfWindow | |
{ | |
get { return this.wpfWindow; } | |
} | |
//private static void ConfigureBehavior(ContentWindow cw) | |
//{ | |
// cw.WindowStartupLocation = WindowStartupLocation.CenterOwner; | |
// cw.CommandBindings.Add(new CommandBinding(PresentationCommands.Accept, (sender, e) => cw.DialogResult = true)); | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment