ViewModel "not set to an instance of an object"
Posted: 16 Jan 2014, 21:56
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!!";
}
}
}