How do I map a type from the CLR namespace in XAML from an assembly other than the one where it was declared?

In XAML, I would like to use types from two different assemblies, each of which has its own namespace. Instead of explicitly declaring namespaces in the attribute xmlns:<xml-namespace>="<clr-namespace>", I would like to use the assembly attribute [XmlnsDefinition]to map the URIs in the namespaces for these types.

One of the assemblies is not related to WPF as such, so I would like it to not refer to assemblies related to WPF, and, in particular, to the assembly System.Xaml.dllthat would be needed if this assembly used [XmlnsDefinition].

I have a Visual Studio solution that is organized as follows:

Gu.Units.sln
    Gu.Units.csproj // no ref to System.Xaml here
    Gu.Units.Wpf.csproj // references Gu.Units and System.Xaml

In Gu.Units.Wpf.csprojI have this mapping:

[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units", AssemblyName = "Gu.Units")]
[assembly: XmlnsDefinition("http://Gu.com/Units", clrNamespace: "Gu.Units.Wpf", AssemblyName = "Gu.Units.Wpf")]
[assembly: XmlnsPrefix("http://Gu.com/Units", "units")]

I tried using it in XAML as follows:

<UserControl x:Class="Gu.Units.Wpf.Demo.Sample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:units="http://Gu.com/Units">
    <Label Content="{x:Static units:LengthUnit.Millimetres}" />
</UserControl>

But for some reason, the namespace Gu.Unitsseems to be ignored. That is, it is not included in the XML namespace identified by the URI http://Gu.com/Units. Instead, I get:

The name "LengthUnit" does not exist in the namespace " http://Gu.com/Units ".

Explicitly declaring the namespace in XAML — that is, having it xmlns:units="clr-namespace:Gu.Units;assembly=Gu.Units"— works fine, but I would also like to avoid this.

, Gu.Units.Wpf.dll [XmlnsDefinition] Gu.Units.dll, System.Xaml.dll, - XAML-?

+4
1

, :

xmlns:units="clr-namespace:Gu.Units;assembly=Gu.Units" , , .

?

: , XML.

  • [XmlnsPrefix] XAML. xmlns XAML. , XAML-authoring , , XAML, xmlns . . VS Designer , , , XAML xmlns: XAML, .

  • XAML, , . , , , , XAML . XAML .

  • [XmlnsDefinition], , , XAML. , xmlns:. URI (, http://Gu.com/Units) CLR. , xmlns: a) CLR, b) , XAML CLR (.. , URI).

    , xmlns:units="clr-namespace:Gu.Units;assembly=Gu.Units" xmlns:units="http://Gu.com/Units", units xmlns CLR, URI http://Gu.com/Units [XmlnsDefinition].

    XML. , , .


:, [XmlnsDefinition] CLR URI, , URI XAML, . , URI, , , XAML (, http://schemas.microsoft.com/winfx/2006/xaml/presentation).

URI xmlns:, XAML , "" , . . , , , , , .


EDIT:

:

, , - Gu.Units http://Gu.com/Units System.Xaml Gu.Units

:

XmlnsDefinitionAttribute , XAML. [ ]

.. [XmlnsDefinition] , , .

AssemblyName, , -, , . , , . , , , XAML .

[XmlnsDefinition] URI , , . , XAML Visual Studio AssemblyName.

WPF , , .


. , [XmlnsDefinition], , . , Gu.Units.Wpf :

public class LengthUnits : Gu.Units.LengthUnits { }

Gu.Units.Wpf.LengthUnits Gu.Units.LengthUnits. , , . , , , , Gu.Units.LengthUnits, Gu.Units.Wpf.LengthUnits. , , , , .

[TypeForwardedTo]. , Gu.Units.Wpf :

[assembly: TypeForwardedTo(typeof(Gu.Units.LengthUnits))]

, , , . XAML-. , , " : LengthUnits" ".

- , , .

+4

Source: https://habr.com/ru/post/1616982/


All Articles