no Constructor found for Commands using MVVM
Posted: 29 Mar 2016, 05:12
Hi
I have five files under the same directory in the Assets named mvvmtest (Assets/mvvmtest/ these five files),which are BaseViewModel,FirstModel(role Model),FirstViewModel(role ViewModel),FirstWindow(role View),RelayCommand,to implement MVVM pattern,but there always an error occurring all the time,but if i put FirstViewModel,FirstModel,RelayCommand,BaseViewModel in one file ,like it did http://www.noesisengine.com/docs/Gui.Co ... orial.html, it will work,but when i separate it into modules, although the namespace is correctly referenced,but this error always shows:
These are my implementation:
FirstModel:
BaseViewModel:
FirstViewModel:
RelayCommand:
FirstWindow.xaml:
Any idears ? Thanks in advance.
I have five files under the same directory in the Assets named mvvmtest (Assets/mvvmtest/ these five files),which are BaseViewModel,FirstModel(role Model),FirstViewModel(role ViewModel),FirstWindow(role View),RelayCommand,to implement MVVM pattern,but there always an error occurring all the time,but if i put FirstViewModel,FirstModel,RelayCommand,BaseViewModel in one file ,like it did http://www.noesisengine.com/docs/Gui.Co ... orial.html, it will work,but when i separate it into modules, although the namespace is correctly referenced,but this error always shows:
MissingMethodException: No constructor found for Presentation.Conmmands.RelayCommand::.ctor()
System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:319)
System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:268)
System.Activator.CreateInstance (System.Type type, System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:263)
Noesis.Extend.CreateInstance (IntPtr unityType, IntPtr cPtr) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisExtend.cs:3855)
UnityEngine.Debug:LogException(Exception)
Noesis.Debug:LogException(Exception) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisDebug.cs:24)
Noesis.Error:SetNativePendingError(Exception) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:33)
Noesis.Extend:CreateInstance(IntPtr, IntPtr) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisExtend.cs:3875)
System.Object:wrapper_native_000007FED33F9690(String)
NoesisGUISystem:Noesis_LoadXAML(String) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:405)
NoesisGUISystem:Load(String) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:279)
NoesisGUISystem:LoadXaml(String) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel:LoadXaml() (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:496)
NoesisGUIPanel:OnEnable() (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:383)
NoesisException: Loading Assets/mvvmtest/FirstWindow.xaml
No constructor found for Presentation.Conmmands.RelayCommand::.ctor()
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:26)
NoesisGUISystem.Noesis_LoadXAML (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:406)
NoesisGUISystem.Load (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:279)
NoesisGUISystem.LoadXaml (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel.LoadXaml () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:496)
NoesisGUIPanel.OnEnable () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:383)
These are my implementation:
FirstModel:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Presentation.Models
{
class FirstModel
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
}
}
public string ToUpper()
{
return this._name.ToUpper();
}
public bool isValid()
{
return true;
}
}
}
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Presentation.ViewModels1
{
class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisedPropertyChanged(string prop)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(prop));
}
}
}
}
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Models;
using Presentation.Conmmands;
using Presentation.ViewModels1;
namespace Presentation.ViewModels
{
class FirstViewModel : BaseViewModel
{
FirstModel fm;
public Presentation.Conmmands.RelayCommand Button1ToUpperCommand
{
get; set;
}
public FirstViewModel()
{
Console.WriteLine("firstviewmodel initial aaaaaaaaaaaaaaaa");
fm = new FirstModel();
fm.Name = "bakerl";
Button1ToUpperCommand = new Presentation.Conmmands.RelayCommand(ToUpper);
Console.WriteLine(Button1ToUpperCommand);
}
public string textbox1
{
get
{
return fm.Name;
}
set
{
if (fm.Name != value)
{
Console.WriteLine("-------");
fm.Name = value;
RaisedPropertyChanged("textbox1");
}
}
}
public void ToUpper(object parameter)
{
string a = fm.ToUpper();
textbox1 += a;
//fm.Name += textbox1;
Console.WriteLine(a);
}
}
}
RelayCommand:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace Presentation.Conmmands
{
class RelayCommand : ICommand
{
private readonly Action<object> _action;
public RelayCommand(Action<object> action)
{
_action = action;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_action(parameter);
}
}
}
FirstWindow.xaml:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/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"
xmlns:vm="clr-namespace:Presentation.ViewModels"
mc:Ignorable="d"
Height="300" Width="300"
DataContext="{DynamicResource FirstViewModel}">
<Grid.Resources>
<vm:FirstViewModel x:Key="FirstViewModel"></vm:FirstViewModel>
</Grid.Resources>
<Grid>
<StackPanel>
<TextBox Name="textbox1" Text="{Binding textbox1, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="{Binding Text, ElementName=textbox1}"></TextBlock>
<Button Name="button1" Content="convert" Command="{Binding Path=Button1ToUpperCommand}"></Button>
<TextBlock Text="{Binding textbox1}"></TextBlock>
</StackPanel>
</Grid>
</Grid>