问题
I am referencing to a namespace of another project(class library, where all the models resides).
as mentioned in the related questions, they have sloved the probem by adding the assembly as well to the .xaml file. But this did not work for me. That is why I am posting.
I have 2 projects.
- WPF project itself.
- Class Library(this has all the viewmodels)
WPF project has a reference to the Class Library.(Which should create a dll file of this Class Library in the bin of WPF project)
I want to refer to a model in the class library. So, I did as follows.
<UserControl x:Class="WPFApplication.Views.SalesScreen"
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:productListViewModel="clr-namespace: WPFApplication.CommonInterface.Shared.ViewModels; assembly=WPFApplication.CommonInterface"
mc:Ignorable="d"
d:DesignHeight="820" d:DesignWidth="1280" Loaded="PlaneLoaded">....
....
<productListViewModel:ProductListVM x:Key="ProductsListVMC"/>...
But still the problem exits, this ProductListVM
class(model) exists in the WPFApplication.CommonInterface.Shared.ViewModels
name space and it is public
as well. :)
the error says,
The name "ProductListVM" does not exist in the namespace "clr-namespace:WPFApplication.CommonInterface.Shared.ViewModels; assembly=WPFApplication.CommonInterface".
I also tried to seperately build the Class Library and add the dll file manually to the WPF project bin/debug
and built it. but the problem exits as the same.
there is another error in the error list as well.
Unknown build error, ''clr-namespace:WPFApplication.CommonInterface.Shared.ViewModels; assembly=POS.WPFApplication.CommonInterface' mapping URI is not valid. Line 7 Position 14.' D:\Dev\WPFApplication\Views\POSViews\SalesScreen.xaml
Error 1 The URI "clr-namespace:WPFApplication.CommonInterface.Shared.ViewModels; assembly=WPFApplication.CommonInterface" is not a valid namespace identifier. D:\Dev\WPFApplication\Views\POSViews\SalesScreen.xaml
Since I have copied the namespaces from the class model itself, there can not be spelling errors(99%),
Can you give me a help on this to solve the problem fast.
Thanks in advance.
回答1:
When you want to reference from another project in one of your XAML file of WPF project, Watch out for the following.
In my issue, when I removed the space between these as shown below [This is the space]
.
xmlns:productListViewModel="clr-namespace: WPFApplication.CommonInterface.Shared.ViewModels;[This is the space]ssembly=WPFApplication.CommonInterface"
And it worked :)
PS: I am working in VS2012
I wasted a big amount of time on this.:(
来源:https://stackoverflow.com/questions/14284450/reference-name-space-in-another-project-in-wpf-xaml-file