WPF App (.NET Core) clr-namespace reference to Class Library (.NET Core) Could not load file or assembly

1

I have a solution with two projects created in Visual Studio Community 2019 version 16.4.2. The first project is View.csproj, a .NET Core 3.1 WPF app. The second project is ViewModel.csproj, a .NET Core 3.1 Class Library.

When I run the code below, I get the following runtime error. What am I doing wrong?

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message='Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '4' and line position '21'.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at View.App.InitializeComponent() in D:\github\StackOverflow\View\App.xaml:line 1
   at View.App.Main()

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'ViewModel, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
// View.csproj;App.xaml.cs
using System.Windows;
using ViewModel;

namespace View
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            new MainWindow { DataContext = new Main() }.Show();
        }
    }
}
<!-- View.csproj;App.xaml -->
<Application x:Class="View.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Main.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>
<!-- View.csproj;MainWindow.xaml -->
<Window x:Class="View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{Binding Title}" Height="450" Width="800">

    <ContentPresenter Content="{Binding}" />

</Window>
<!-- View.csproj;Main.xaml -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel">
               <!-- ^=== The error message points to the clr-namespace reference above. -->
               <!--      It compiles just fine, so why can't it find the ViewModel assembly at runtime? -->
               <!--      If I put the Main.cs file in the View project, it runs just fine. -->

    <DataTemplate DataType="{x:Type vm:Main}">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>

</ResourceDictionary>
// ViewModel.csproj;Main.cs
namespace ViewModel
{
    public class Main
    {
        public string Title => "Hello world.";
        public string Name => "Hello again from Main.cs";
    }
}
<!-- View.csproj -->
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\ViewModel\ViewModel.csproj" />
  </ItemGroup>

</Project>
<!-- ViewModel.csproj -->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>
c#
wpf
xaml
.net-core
asked on Stack Overflow Dec 20, 2019 by Robin • edited Dec 21, 2019 by Guillaume Sasdy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0