sericaer
Topic Author
Posts: 13
Joined: 09 Oct 2021, 09:29

How to parse a xaml which reference types from other dll ?

29 Nov 2021, 09:46

I want to develop a game support mod, in the mod there are xamls for GUI, and dlls for logic.

for example, the xaml like this:
<UserControl
             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:jui="clr-namespace:JiangH;assembly=JiangH.UI"
             xmlns:native="clr-namespace:Mods.Native;assembly=Assembly"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Canvas>
        <Canvas.DataContext>
            <native:SceneMainView/>
        </Canvas.DataContext>

        <Label Name ="name" Content="{Binding player.name,FallbackValue = '_PLAYER_NAME_'}"/>

    </Canvas>
</UserControl>
xmlns:native="clr-namespace:Mods.Native;assembly=Assembly set the dll for mod.

This xaml is parsed in code
    public partial class JiangHMainView : UserControl
    {
        public JiangHMainView()
        {
            InitializeComponent();

#if NOESIS
            var filePath = UnityEngine.Application.streamingAssetsPath;
#else
            var filePath = "../../../Assets/StreamingAssets";
#endif
            var str = File.ReadAllText(filePath + "/mods/native/SceneMain.xaml");

            var ass = Assembly.Load(File.ReadAllBytes(filePath + "/mods/native/Assembly.dll"));

            var userControl = XamlReader.Parse(str) as UserControl;

            var uiElement = userControl.Content as UIElement;
            userControl.Content = null;

            this.rootCanvas.Children.Add(uiElement);
        }
Assembly.Load(File.ReadAllBytes(filePath + "/mods/native/Assembly.dll")) load the dll, which have the class "SceneMainView".

The project is run succesful in Blend, but run faild in Unity. it report an exception:
[NOESIS/E] <memory>(12): Unknown type 'mods.native.SceneMainView'.
UnityEngine.Debug:LogError (object,UnityEngine.Object)
NoesisUnity:UnityLog (int,string) (at Packages/NoesisGUI/Runtime/NoesisUnity.cs:317)
Noesis.GUI:ParseXaml (string) (at Packages/NoesisGUI/Runtime/API/Core/NoesisGUI.cs:291)
Noesis.XamlReader:Parse (string) (at Packages/NoesisGUI/Runtime/API/Proxies/XamlReader.cs:20)
JiangH.JiangHMainView:.ctor () (at Assets/Scenes/MainScene/JiangHMainView.xaml.cs:36)
(wrapper dynamic-method) object:lambda_method (System.Runtime.CompilerServices.Closure)
Noesis.Extend:CreateInstance (intptr,intptr) (at Packages/NoesisGUI/Runtime/API/Core/Extend.cs:4681)
Noesis.GUI:LoadXaml (System.IO.Stream,string) (at Packages/NoesisGUI/Runtime/API/Core/NoesisGUI.cs:270)
NoesisXaml:Load () (at Packages/NoesisGUI/Runtime/NoesisXaml.cs:17)
NoesisView:LoadXaml (bool) (at Packages/NoesisGUI/Runtime/NoesisView.cs:465)
NoesisView:OnEnable () (at Packages/NoesisGUI/Runtime/NoesisView.cs:553)
I think it because the "Assembly.Load" not add assembly to Application. But how to resolve this issue?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: How to parse a xaml which reference types from other dll ?

29 Nov 2021, 10:51

Hi, this is related to issues #1016, #1283, and #1320, that we solved for next release.
As a workaround you can manually load the referenced assembly into the domain before loading the xaml:
Assembly.Load("YourAssembly");

Who is online

Users browsing this forum: Google [Bot], vinick and 61 guests