mirror of https://github.com/AMT-Cheif/Stylet.git
Finish EventCommand, start on ViewLocator
This commit is contained in:
parent
d7227ec3d5
commit
010c779a84
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,14 @@ namespace MicroMVVM
|
|||
{
|
||||
class MainViewModel
|
||||
{
|
||||
public bool CanTesty { get { return true; } }
|
||||
|
||||
public void Testy(object parameter)
|
||||
public object ViewModel
|
||||
{
|
||||
|
||||
get { return new SubViewModel(); }
|
||||
}
|
||||
}
|
||||
|
||||
class SubViewModel
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue