0

I have a resource code block with several cursors to later be used at runtime in the functionality of a WPF canvas. When I try to load it in the loaded event of Window, it will generate a NullReferenceException:

Object Reference not set to an Instance of an object

<Window.Resources>
    <ResourceDictionary>
        <Image x:Key="CursCruz" Cursor="C:/Users/Marin/source/repos/AnalisisMamposteriaWPF/Imagenes 24/CIRCURX.cur" />
        <Image x:Key="CurUno" Cursor="C:/Users/Marin/source/repos/AnalisisMamposteriaWPF/Imagenes 24/Cursor_Uno.cur" />
        <Image x:Key="CurDos" Cursor="C:/Users/Marin/source/repos/AnalisisMamposteriaWPF/Imagenes 24/Cursor_Dos.cur" />
    </ResourceDictionary>
</Window.Resources>

Code behind

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    picCanva.Background = Brushes.Black; // Para futuro cambio de background
    zoom = 1.0;
    Cursor AgregarCursor = ((Image)this.Resources["cursCruz"]).Cursor;
    Cursor ACursorCirculoCruz = ((Image)this.Resources["curUno"]).Cursor;
    Cursor ACursorCirculoEqui = ((Image)this.Resources["curDos"]).Cursor;
    MouseMove += picCanva_MouseMove_NotDown;
}
Peter Duniho
  • 62,751
  • 5
  • 84
  • 120
M. Liranzo
  • 31
  • 5
  • It's unclear why you think you need these Image elements, just to load Cursor resources. Just add the cursor files to your Visual Studio project (e.g. in a project folder named Images24), set their Build Action to Resource, and load a cursor from such a file by `var cursor = new Cursor(Application.GetResourceStream(new Uri("pack://application:,,,/Images24/CIRCURX.cur")).Stream);` – Clemens Feb 13 '21 at 08:05
  • I agree with @Clemens that you probably don't need to create the images to get the cursors. However the reason you're getting a NullReferenceException is because accessing resources is case sensitive and you've created resources with the initial letter in uppercase and trying to access them with the initial letter in lowercase. – Jason Hunt Feb 13 '21 at 08:14
  • It fixed those syntax errors and now throws the following exception: System.Windows.Markup.XamlParseException: "Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension.'threw an exception.' Line number'14' and line position '14'.' – M. Liranzo Feb 13 '21 at 14:21
  • Clemens. Could you guide me through this procedure by giving me an example. – M. Liranzo Feb 13 '21 at 14:23

0 Answers0