Page 1 of 1

Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

Posted: 09 Jun 2024, 10:51
by Arca INC
Hi NoesisGUI Community,

I need some help with loading XAML interfaces and textures in Unreal Engine 5 without importing them directly into the project. I've tried loading the XAML file and casting my parsed result to UNoesisXaml in the constructor of my class that extends public UNoesisInstance, but the interface remains black.

Here is my test code:

FString XamlFilePath = FPaths::ProjectContentDir() + TEXT("../Assets/ConnectingGUI/MainWindow.xaml");
FString XamlContent;

if (FPaths::FileExists(XamlFilePath))
{
    FFileHelper::LoadFileToString(XamlContent, *XamlFilePath);
}
else
{
    UE_LOG(LogTemp, Error, TEXT("Xaml not Found : %s"), *XamlFilePath);
}

UNoesisXaml* MyXaml = NewObject<UNoesisXaml>();

if (!XamlContent.IsEmpty())
{
    std::string XamlText = TCHAR_TO_UTF8(*XamlContent);

    Noesis::Ptr<Noesis::BaseComponent> XamlRoot = 
    Noesis::GUI::ParseXaml(XamlText.c_str());

    BaseXaml = reinterpret_cast<UNoesisXaml*>(XamlRoot.GetPtr());
}
else
{
    UE_LOG(LogTemp, Error, TEXT("Error when loading XAML"));
}

Despite the code, my interface stays black. Can anyone provide guidance on the correct approach to load XAML interfaces and textures dynamically in UE5?

Thank you in advance!

Best regards,
P.Claudon

Re: Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

Posted: 11 Jun 2024, 11:28
by hcpizzi
Hi,

The way to do it, once you've loaded the XAML text, is to create an UNoesisXaml and set its XamlText property.
        UNoesisXaml* NoesisXaml = NewObject<UNoesisXaml>();
        NoesisXaml->XamlText.Insert((uint8*)XamlText.c_str(), XamlText.length(), 0);
However, take into account that if the XAML depends on other XAML files (say, it includes a ResourceDictionary), or images or any othe external resource, these dependencies won't be found.

Re: Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

Posted: 11 Jun 2024, 15:38
by Arca INC
I tried following the advice but I'm encountering an issue. I'm receiving the warning: LogNoesis Xaml not found ''.

Here is the code I'm using:
// TestNoesis.cpp
#include "TestNoesis.h"
#include "DataContext.h"
#include "NoesisRuntime.h"
#include "NoesisXaml.h"

UTestNoesis::UTestNoesis(const FObjectInitializer& ObjectInitializer)
{
    DataContext = ObjectInitializer.CreateDefaultSubobject<UDataContext>(this, TEXT("DataContext"));

    FString XamlFilePath = FPaths::ProjectContentDir() + TEXT("../Assets/ConnectingGUI/MainWindow.xaml");
    FString XamlContent;

    if (FPaths::FileExists(XamlFilePath))
    {
        FFileHelper::LoadFileToString(XamlContent, *XamlFilePath);
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Xaml not Found : %s"), *XamlFilePath);
        return;
    }

    if (!XamlContent.isEmpty())
    {
        std::string XamlText = TCHAR_TO_UTF8(*XamlContent);

        UNoesisXaml* NoesisXaml = NewObject<UNoesisXaml>();
        NoesisXaml->XamlText.Insert((uint8*)XamlText.c_str(), XamlText.length(), 0);

        // UE_LOG(LogTemp, Warning, TEXT("%s"), *XamlContent); // Content is correctly read 

        BaseXaml = NoesisXaml;
        // BaseXaml->XamlText.Insert((uint8*)XamlText.c_str(), XamlText.length(), 0); // I've tried this way but it results in the same error
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Error when loading XAML"));
    }
}

void UTestNoesis::NativeConstruct()
{
    Super::NativeConstruct();

    DataContext->BaseXaml = BaseXaml;
    SetDataContext(Cast<UObject>(DataContext.Get()));
}

// TestNoesis.h 
#pragma once

#include "CoreMinimal.h"
#include "NoesisInstance.h"
#include "DataContext.h"
#include "TestNoesis.generated.h"

UCLASS()
class UTestNoesis : public UNoesisInstance
{
    GENERATED_BODY()

public:
    UTestNoesis(const FObjectInitializer& ObjectInitializer);

    virtual void NativeConstruct() override;

private:
    UPROPERTY()
    TObjectPtr<class UDataContext> DataContext;
};

// DataContext.h 
#pragma once

#include "CoreMinimal.h"
#include "NoesisXaml.h"
#include "UObject/Object.h"
#include "DataContext.generated.h"

UCLASS()
class SNPROJECTCLIENT_API UDataContext : public UObject
{
    GENERATED_BODY()

public:
    UDataContext();
    
    UNoesisXaml* BaseXaml;
};
Any guidance or suggestions on what might be causing this issue would be greatly appreciated.

Thank you in advance!

Re: Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

Posted: 12 Jun 2024, 16:33
by Arca INC
Hello,

I managed to solve the problem by using the following code:
const char* xamlText = reinterpret_cast<const char*>(XamlText.GetData());
return Noesis::GUI::ParseXaml(xamlText);
This allowed me to successfully load the XAML interfaces and textures in Unreal Engine 5 without having to import them directly into the project. I hope this can help others who are facing the same issue.

Re: Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

Posted: 14 Jun 2024, 20:51
by jsantos
Thanks for letting us know! Closing this.