Finish EventCommand, start on ViewLocator

This commit is contained in:
Antony Male 2014-02-03 21:52:16 +00:00
parent d7227ec3d5
commit 010c779a84
8 changed files with 86 additions and 7 deletions

View File

@ -88,7 +88,12 @@ namespace MicroMVVM
public void Execute(object parameter)
{
if (this.target == null)
throw new Exception("Target not set");
var methodInfo = this.target.GetType().GetMethod(this.methodName);
if (methodInfo == null)
throw new Exception(String.Format("Unable to find method {0} on {1}", this.methodName, this.target.GetType().Name));
if (methodInfo != null)
{

View File

@ -22,10 +22,12 @@ namespace MicroMVVM
{
var valueService = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
if (valueService.TargetProperty is ICommand)
{
return new ActionCommand((FrameworkElement)valueService.TargetObject, this.Method);
}
else
{
var ec = new EventCommand((FrameworkElement)valueService.TargetObject, valueService.TargetProperty);
var ec = new EventCommand((FrameworkElement)valueService.TargetObject, valueService.TargetProperty, this.Method);
return ec.GetDelegate();
}

View File

@ -12,11 +12,13 @@ namespace MicroMVVM
{
private FrameworkElement subject;
private object targetProperty;
private string methodName;
public EventCommand(FrameworkElement subject, object targetProperty)
public EventCommand(FrameworkElement subject, object targetProperty, string methodName)
{
this.subject = subject;
this.targetProperty = targetProperty;
this.methodName = methodName;
}
public Delegate GetDelegate()
@ -40,7 +42,16 @@ namespace MicroMVVM
private void InvokeCommand(object sender, RoutedEventArgs e)
{
var target = View.GetTarget(this.subject);
if (target == null)
throw new Exception("Target not set");
var methodInfo = target.GetType().GetMethod(this.methodName);
if (methodInfo == null)
throw new Exception(String.Format("Unable to find method {0} on {1}", this.methodName, target.GetType().Name));
var parameters = methodInfo.GetParameters().Length == 1 ? new object[] { e } : null;
methodInfo.Invoke(target, parameters);
}
}
}

View File

@ -8,11 +8,14 @@ namespace MicroMVVM
{
class MainViewModel
{
public bool CanTesty { get { return true; } }
public object ViewModel
{
get { return new SubViewModel(); }
}
}
public void Testy(object parameter)
class SubViewModel
{
}
}
}

View File

@ -4,6 +4,6 @@
xmlns:my="clr-namespace:MicroMVVM"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="{my:Action Testy}"/>
<ContentControl my:View.Model="{Binding ViewModel}"/>
</Grid>
</Window>

View File

@ -58,6 +58,7 @@
<Compile Include="EventCommand.cs" />
<Compile Include="MainViewModel.cs" />
<Compile Include="View.cs" />
<Compile Include="ViewLocator.cs" />
<Compile Include="ViewModelBinder.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>

View File

@ -22,5 +22,33 @@ namespace MicroMVVM
// Using a DependencyProperty as the backing store for Target. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TargetProperty =
DependencyProperty.RegisterAttached("Target", typeof(object), typeof(View), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
public static object GetModel(DependencyObject obj)
{
return (object)obj.GetValue(ModelProperty);
}
public static void SetModel(DependencyObject obj, object value)
{
obj.SetValue(ModelProperty, value);
}
// Using a DependencyProperty as the backing store for Model. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ModelProperty =
DependencyProperty.RegisterAttached("Model", typeof(object), typeof(View), new PropertyMetadata(null, OnModelChanged));
private static void OnModelChanged(DependencyObject targetLocation, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue == e.NewValue)
return;
if (e.NewValue != null)
{
var view = ViewLocator.LocateForModel(e.NewValue);
}
}
}
}

29
MicroMVVM/ViewLocator.cs Normal file
View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
namespace MicroMVVM
{
public static class ViewLocator
{
public static UIElement LocateForModel(object model)
{
var viewName = model.GetType().FullName;
var modelName = Regex.Replace(viewName, @"ViewModel", "View");
var modelType = Assembly.GetEntryAssembly().GetType(modelName);
if (modelType == null)
throw new Exception(String.Format("Unable to find a View with type {0}", modelName));
var instance = Activator.CreateInstance(modelType);
if (!(instance is UIElement))
throw new Exception(String.Format("Managed to create a {0}, but it wasn't a UIElement", modelName));
return (UIElement)instance;
}
}
}