我在我的应用程序中定义了一个应用程序资源,它将我的ViewModel与导航元素一起存储以导航页面。这在Visual Studio的xaml编辑器中工作良好,因为所有数据绑定都在那里工作。但是,当我尝试在调试器中运行应用程序时,它会显示一条消息Cannot find source with the name ViewModelLocator.
的异常有人知道发生了什么问题吗?WPF在执行时找不到本地资源
我在我的App.xaml像这样定义的本地资源:
<Application.Resources>
<viewmodel:ViewModelLocator x:Key="ViewModelLocator"/>
</Application.Resources>
,我尝试使用这样的:
<Page x:Class="QardPrint.PageEmployeesList"
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:QardPrint"
xmlns:viewmodel="clr-namespace:QardPrint.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="PageEmployeesList"
DataContext="{Binding EmployeesListViewModel, Source={StaticResource ViewModelLocator}}">
我ViewModelLocator类看起来像这样
public class ViewModelLocator
{
public EmployeesListViewModel EmployeesListViewModel => new EmployeesListViewModel(App.Navigation);
}
“ViewModelLocator”和“App.xaml”是否在同一个程序集中? –
是的,他们都在同一个大会。它们位于不同的命名空间中,但我也尝试将它们放在相同的命名空间中,并且Exception仍然弹出。 –