Arca INC
Topic Author
Posts: 4
Joined: 01 May 2024, 18:49

Loading XAML Interfaces and Textures in Unreal Engine 5 without Importing

09 Jun 2024, 10:51

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
 
User avatar
hcpizzi
Site Admin
Posts: 335
Joined: 09 Feb 2012, 12:40

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

11 Jun 2024, 11:28

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.
 
Arca INC
Topic Author
Posts: 4
Joined: 01 May 2024, 18:49

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

11 Jun 2024, 15:38

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!
 
Arca INC
Topic Author
Posts: 4
Joined: 01 May 2024, 18:49

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

12 Jun 2024, 16:33

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.
 
User avatar
jsantos
Site Admin
Posts: 3990
Joined: 20 Jan 2012, 17:18
Contact:

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

14 Jun 2024, 20:51

Thanks for letting us know! Closing this.

Who is online

Users browsing this forum: AliciaCastillo, Bing [Bot], Google [Bot] and 2 guests