MoonKnight
MoonKnight

Reputation: 23833

Cannot Define Object in XAML

All, I have a user control. In the XAML markup for this control I want to define a resource (instatiate an object called cellColorConverter of the class CellColorConverter which is defined in the same namespace as the control. I have

<UserControl x:Class="ResourceStudio.Resource.Resource"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="331.2" Width="340">
<UserControl.Resources>
    <ResourceStudio.Resource:CellColorConverter x:Key ="cellColorConverter"/> // <- Error.
</UserControl.Resources>

This is giving a compile-time error saying

The namespace prefix ResourceStudio.Res is not defined.

What am I doing wrong here?

Thanks for your time.


Update: I now have

xmlns:local="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio"
             mc:Ignorable="d" Height="331.2" Width="340">
   <UserControl.Resources>
      <local:CellColorConverter x:Key ="cellColorConverter"/>
   </UserControl.Resources>

The CellColorConverter class is in the name space ResourceStudio.Resource, defined as

namespace ResourceStudio.Resource
{
   public class CellColorConverter : IMultiValueConverter
   {
       // ...     
   }
}

I still get the following error

The name "CellColorConverter" does not exist in the namespace "clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio". F:\Camus\ResourceStudio\ResourceStudio\ResourceStudio\Resource\Resource.xaml

Upvotes: 0

Views: 362

Answers (1)

Drew Noakes
Drew Noakes

Reputation: 310802

In the XAML:

<ResourceStudio.Resource:CellColorConverter />

The ResourceStudio.Resource is an XML namespace for CellColorConverter.

You need to map this XML namespace to a .NET namespace:

<UserControl xmlns:ResourceStudio.Resource="clr-namespace:ResourceStudio.Resource;assembly=ResourceStudio" ... />

The actual namespace and assembly name depend upon what you've called them in your code.

This article provides more information.

Also, you can make the namespace shorter:

xmlns:local="clr-namespace:..."

<local:CellColorConverter ... >

Upvotes: 3

Related Questions