Reputation: 29
I am working in Visual Studio on a plugin for an application. I've created a WPF window, and I need to set its DataContext to a class I've built. This class implements an interface from the original application (a .dll I reference in the project).
I'm building to a few different versions of the main application, and each version has its own API .dll. Because there are different .dll versions, I dynamically load the correct version by the Build Configuration I'm working on (defined in the .csproj).
This is the window definition in my Xaml:
<Window x:Class="MyNamespace.MyClass.MonitorWindow"
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"
xmlns:local="clr-namespace:MyNamespace.MyClass"
mc:Ignorable="d" Title="MyClass Monitor" >
<Window.DataContext>
<local:MonitorCommand></local:MonitorCommand>
</Window.DataContext>
Here is where I load the .dll by configuration in the .csproj:
<Reference Include="RevitAPI" Condition="'$(Configuration)' == 'Revit2018'">
<HintPath>C:\Program Files\Autodesk\Revit 2018\RevitAPI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="RevitAPI" Condition="'$(Configuration)' == 'Revit2020'">
<HintPath>C:\Program Files\Autodesk\Revit 2020\RevitAPI.dll</HintPath>
<Private>False</Private>
</Reference>
The Designer does not work, and this is one of the error messages I'm getting:
Error XDG0008 The name "MonitorCommand" does not exist in the namespace "clr-namespace:MyNamespace.MyClass". MyNamespace MonitorWindow.xaml 10
Intellisense auto-completes the "MonitorCommand" class within the DataContext, however, it marks this as an error.
I think the error may be coming from the Xaml not recognizing the external .dll, because when I removed the interface implementation from the MonitorCommand, the error disappeared.
My goal is to properly link the .Xaml to my class, and be able to access its properties in the window.
Please let me know if you've run into anything like this! Thanks in advance
Upvotes: 0
Views: 331
Reputation:
xmlns should point to a namespace, not a class.
Your definition should look like: xmlns:local="clr-namespace:MyNamespace".
Then in your class, add a property that point to the command(of the type of your command).
Then, your window's DataContext should be an instance of your class.
In the window's content(your view) add a Button and have it's Command property
bind to the property that holds the command.
Upvotes: 1