2017-08-03 16 views
2

ページ内をナビゲートするナビゲーション要素を持つViewModelを格納するアプリケーションリソースをアプリケーションで定義しています。これは、すべてのデータバインディングがそこで動作しているので、ビジュアルスタジオの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); 
} 
のように見えます
+0

'ViewModelLocator'と' App.xaml'は同じアセンブリ内にありますか? –

+0

はい両方が同じアセンブリにあります。彼らは別の名前空間にありますが、私は同じ名前空間に入れようとしましたが、例外はまだポップアップしました。 –

答えて

0

私は問題を発見しました。 App.xamlには、スタートアップパラメータが削除されました。それをもう一度追加したら、問題は解決されました。 私はapp.cの起動機能で別のウィンドウを作ったので、これを行いました。しかし、これはおそらく悪いデザインなので、それをより良くする方法を見つけ出すつもりです。

0

お試しくださいResourceDictionary

App.xaml:

<Application xmlns:viewmodel="clr-namespace:QardPrint.ViewModel"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <viewmodel:ViewModelLocator x:Key="Locator" /> 

QardPrint.PageEmployeesLis.Xaml:

<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 Locator}}"> 
+0

あなたの返事をありがとう。残念ながら私は同じエラーが発生します。 –

+0

ソリューションをクリーンアップして再構築しましたか? –

+0

これから使用方法を試してみてください:https://stackoverflow.com/a/19012976/4772806 –

関連する問題