ViewModel "not set to an instance of an object"
I'm currently working on creating a test case using databinding and a viewmodel that can be viewed in Unity, but at the same time allow the .xaml to be edited in Blend/Visual Studio/etc. and show up in the design view.
The issue I am having is that Visual Studio/Blend outputs an error, "Object reference not set to an instance of an object.", for the creation of the TestUIViewModel resource:
I think the issue is that the files are in separate directories and cannot find the references, but I'm not sure. I have a .csproj, TestUI.csproj, created in a separate directory (Documents/NoesisTest/TestUI/) with Blend, and I have the .xaml file and .cs files linked to that project.
TestUI.xaml (located in Documents/NoesisTest/Assets/Test UI Assets/):
TestUIViewModel.cs (located in Documents/NoesisTest/Assets/Plugins/TestUI/:
Any ideas?
The issue I am having is that Visual Studio/Blend outputs an error, "Object reference not set to an instance of an object.", for the creation of the TestUIViewModel resource:
Code: Select all
<local:TestUIViewModel x:Key="viewModel"/>
TestUI.xaml (located in Documents/NoesisTest/Assets/Test UI Assets/):
Code: Select all
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestUI"
Height="300" Width="300">
<Grid>
<Grid.Resources>
<local:TestUIViewModel x:Key="viewModel"/>
</Grid.Resources>
<Grid.DataContext>
<Binding Source="{StaticResource viewModel}"/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Click Me!" FontSize="32" Command="{Binding ClickMeCommand}"/>
<TextBox Grid.Row="1" Text="{Binding SampleText, Source={StaticResource viewModel}}"/>
</Grid>
</UserControl>
Code: Select all
using UnityEngine;
using System;
namespace TestUI
{
[Noesis.Extended]
public class TestUIViewModel : Noesis.SerializableComponent
{
string _sampleText = string.Empty;
public string SampleText
{
get
{
return _sampleText;
}
set
{
if (_sampleText != value)
{
_sampleText = value;
NotifyPropertyChanged("SampleText");
}
}
}
public Noesis.DelegateCommand ClickMeCommand
{
get;
private set;
}
public TestUIViewModel()
{
ClickMeCommand = new Noesis.DelegateCommand(OnClickMe);
}
private void OnClickMe()
{
SampleText = "Hello!!";
}
}
}
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: ViewModel "not set to an instance of an object"
Hi,
First of all, you have to modify the xaml file a bit because of a bug about nested markup extensions that will be solved for the next version. Try this one:
Then I created a Blend project, and added a new Class item (TestUIViewModel.cs) with the following code:
Finally I linked (in the 'Projects' tab in Blend editor right click the project name, and select 'Link to existing item...' in the context menu, see Blend tutorial for more info) the TestUI.xaml from the Unity Assets/ directory.
This setup is working for me to view and edit the xaml file in Blend editor.
Let me know if it works also for you.
First of all, you have to modify the xaml file a bit because of a bug about nested markup extensions that will be solved for the next version. Try this one:
Code: Select all
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestUI"
Height="300" Width="300">
<Grid>
<Grid.Resources>
<local:TestUIViewModel x:Key="viewModel"/>
</Grid.Resources>
<Grid.DataContext>
<StaticResource ResourceKey="viewModel"/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Click Me!" FontSize="32" Command="{Binding ClickMeCommand}"/>
<TextBox Grid.Row="1" Text="{Binding SampleText}"/>
</Grid>
</UserControl>
Code: Select all
using System;
namespace TestUI
{
public class TestUIViewModel
{
public TestUIViewModel()
{
}
}
}
This setup is working for me to view and edit the xaml file in Blend editor.
Let me know if it works also for you.
Re: ViewModel "not set to an instance of an object"
I still get the "not set to an instancde of an object" message, but it seems to work in Blend. Now I just have an error message in Unity:
The type or namespace name `DelegateCommand' does not exist in the namespace `Noesis'. Are you missing an assembly reference?
The type or namespace name `DelegateCommand' does not exist in the namespace `Noesis'. Are you missing an assembly reference?
Last edited by mtanfield on 17 Jan 2014, 15:53, edited 1 time in total.
Re: ViewModel "not set to an instance of an object"
DelegateCommand is not part of noesisGUI (it happens the same with WPF). You can implement it easily, as shown in the Commands tutorial.
Code: Select all
[Noesis.Extended]
public class DelegateCommand : Noesis.BaseCommand
{
private readonly System.Action _action;
public DelegateCommand(System.Action action)
{
_action = action;
}
public bool CanExecuteCommand(Noesis.BaseComponent parameter)
{
return true;
}
public void ExecuteCommand(Noesis.BaseComponent parameter)
{
_action();
}
}
Re: ViewModel "not set to an instance of an object"
Ok, it works now!
One other thing I noticed was that if I made the DelegateCommand class part of the TestUI namespace, a warning conflict would show up between the actual file and the class data in the ScriptAssembly .dll. By removing the namespace designation, everything works as it should!
Thanks again!

One other thing I noticed was that if I made the DelegateCommand class part of the TestUI namespace, a warning conflict would show up between the actual file and the class data in the ScriptAssembly .dll. By removing the namespace designation, everything works as it should!
Thanks again!
-
-
sfernandez
Site Admin
- Posts: 2908
- Joined:
Re: ViewModel "not set to an instance of an object"
DelegateCommand is defined in one of our samples (Commands sample). If you didn't delete the NoesisGUI/Samples/ directory, that class will conflict with the one you defined in TestUI namespace when using DelegateCommand without fully qualifying its name.
Who is online
Users browsing this forum: dengfan, Semrush [Bot] and 5 guests