5

I've made a netstandard class library. I target netstandard 1.6. My csproj is like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Runtime" Version="4.3.0" />
    <PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.3.0" />
    <PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
  </ItemGroup>
</Project>

And I have a WPF-Project, in which I reference this dll. I can use the classes of the netstandard dll in c#. I can use them in xaml, too. I get even intellicence for them. But xaml designer says, my xaml is invalid. I can compile the solution, I can run the application. At runtime is everything ok. But the designer cannot work with it.

enter image description here

The Person class looks like:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;

namespace DS.Publications.Common
{
    [DataContract(Namespace = Constants.NamespaceConstants.DataContractNamespace)]
    public class Person : INotifyPropertyChanged
    {
        private string _Title = string.Empty;

        [DataMember]
        public string Title { get { return _Title; } set { Set(ref _Title, value); } }



        private string _ForeName;

        [DataMember]
        public string ForeName { get { return _ForeName; } set { Set(ref _ForeName, value); } }

        private string _LastName;
        [DataMember]
        public string LastName { get { return _LastName; } set { Set(ref _LastName, value); } }



        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {

            if (field == null || !field.Equals(value))
            {
                field = value;
                this.OnPropertyChanged(propertyName);
                return true;
            }
            return false;
        }
    }
}

Do you know a workaround, or do you have an idea, how can I correct it?

Athari
  • 33,702
  • 16
  • 105
  • 146
lvoros
  • 312
  • 1
  • 14

2 Answers2

0

I also encountered a similar problem; I had a class in a .NET Standard Project and tried to display it in a ListBox using an Observable Collection. It turns out, that XAML does not like DataContracts. As soon as I removed the [DataContract] attribute from my .NET Standard class, it worked. I already filed a bug report to Microsoft about this behaviour. If my assumption is correct the error should go away as soon as you remove the [DataContract] attribute.

As for Solutions. You can either remove the DataContract (which won't work as you need it otherwise you wouldn't use it, i guess...) or create a proxy object for each class which Mirrors the functionality and redirects function calls to an underlying copy of the original object. I did not try though whether this is enough to prevent XAML from triggering Data Contracts (if that is whats going on).

It maybe works in compiled mode because it is only defined as a resource. As long as it is not actively displayed it could probably work. Maybe the designer tries to display resources?

This might be related to another bug (here and here), where dependencies from .NET Standard are not importet/translated to other projects. This bug report describes that when accessing functionalities from a referenced DLL things crash. Basically you have a .NET Standard library using 3rd party library X and a .NET Framework project Z using your .NET standard library Y. As soon as you call a method in Y which in turn calls X, you're screwed. You'll need to manually import all references from Y into your project Z.

As to how this might be related. Data Contracts are a .NET Standard only feature and aim to replace Serializable. But they seem to have a bit more functionality that gets triggered somehow by the XAML frontend. And as Data Contracts are not available to .NET Framework, everything comes crashing down.

If you manage to find a solution, please let us know as this bug is in need of a 'proper' workaround...

CShark
  • 1,413
  • 15
  • 25
0

I have not really found a solution for this problem... I've set the target framework of my netstandard library back to netstandard1.3. When targeting 1.3 everything works fine. I hope, a fix will be comming with the next VS-Update.

PS. As I set the target to netstandard1.3, I had the problem, that the old netstandard1.6 binaries were found, until I really cleaned the solution like here: How to really clean the solution.

lvoros
  • 312
  • 1
  • 14