UE4
Topic Author
Posts: 62
Joined: 29 Dec 2017, 06:32

confused about ObservableCollection

09 Apr 2018, 12:55

Hi,
I want to parse a string to an array, for some reason I need to use ObaservableCollection<NsString>,
but I got an error

Error C2555 'Noesis::ObservableCollection<NsString>::Get': overriding virtual function return type differs and is not covariant from 'Noesis::Collection::Get'

I find that the IList::Get(uint32_t InIndex) return a BaseComponent* , but NsStrin is not a BaseComponet, some advices?
 
UE4
Topic Author
Posts: 62
Joined: 29 Dec 2017, 06:32

Re: confused about ObaervableCollection

09 Apr 2018, 15:09

And another , about ObservableCollection
//WPF version RichTextblock

namespace GameGUI
{
    public class RichTextBlock : TextBlock
    {
        public ObservableCollection<Inline> InlineList
		{
			get { return (ObservableCollection<Inline>)GetValue(InlineListProperty); }
			set { SetValue(InlineListProperty, value); }
		}

		public static readonly DependencyProperty InlineListProperty = DependencyProperty.Register("InlineList", typeof(ObservableCollection<Inline>), typeof(RichTextBlock),
			new UIPropertyMetadata(null, OnPropertyChanged));

        private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
           

			RichTextBlock rtb = (RichTextBlock)sender;
            if(rtb != null && e.OldValue != e.NewValue)
			{
				ObservableCollection<Inline> RichTextToAdd = new ObservableCollection<Inline>();

				rtb.Inlines.Clear();
				rtb.Inlines.AddRange(rtb.InlineList);
			}
		}

    }
}

//C++

const DependencyProperty* RichTextBlock::InlineListProperty;

RichTextBlock::RichTextBlock()
{
}

NS_IMPLEMENT_REFLECTION(RichTextBlock)
{
	NsMeta<TypeId>("GameGUI.RichTextBlock");

	Ptr<UIElementData> UIData = NsMeta<UIElementData>(TypeOf<SelfClass>());

	UIData.RegisterProperty<ObservableCollection<Inline>>(InlineListProperty, "Inline",
		FrameworkPropertyMetadata::Create(NULL, FrameworkOptions_None, &OnPropertyChanged));
}

Noesis::ObservableCollection<Noesis::Inline> GameGUI::RichTextBlock::GetInlineList() const
{
	return DependencyObject::GetValue<ObservableCollection<Inline>>(InlineListProperty);
}

void GameGUI::RichTextBlock::SetInlineList(const ObservableCollection<Inline>& InlineList)
{
	DependencyObject::SetValue<ObservableCollection<Inline>>(InlineListProperty, InlineList);
}

void GameGUI::RichTextBlock::OnPropertyChanged(DependencyObject* sender, const DependencyPropertyChangedEventArgs& e)
{
	RichTextBlock* rtb = (RichTextBlock*)sender;
	if (rtb != nullptr && e.newValue != e.oldValue)
	{
		rtb->GetInlines()->Clear();
		//no AddRange?
		rtb->GetInlines().AddRange(rtb->GetInlineList());
	}
}

 
Wanderer
Posts: 168
Joined: 08 May 2017, 18:36

Re: confused about ObaervableCollection

10 Apr 2018, 13:37

Hi,
I want to parse a string to an array, for some reason I need to use ObaservableCollection<NsString>,
but I got an error

Error C2555 'Noesis::ObservableCollection<NsString>::Get': overriding virtual function return type differs and is not covariant from 'Noesis::Collection::Get'

I find that the IList::Get(uint32_t InIndex) return a BaseComponent* , but NsStrin is not a BaseComponet, some advices?
Yesterday I got similar error, maybe same. From your code, I don't see problem, I never used InLine, but in my problem was I had included header file with class declaration and after change it in to empty declaration "class something;" it throw me this error, because I used object from header in NS_IMPLEMENT_INLINE_REFLECTION, then I divided it in to *.h NS_DECLARE_REFLECTION and in *.cpp I included somethingClass.h and add NS_IMPLEMENT_REFLECTION(). After that error gone. I hope you get some idea where is your problem.
 
UE4
Topic Author
Posts: 62
Joined: 29 Dec 2017, 06:32

Re: confused about ObaervableCollection

10 Apr 2018, 15:27

Yesterday I got similar error, maybe same. From your code, I don't see problem, I never used InLine, but in my problem was I had included header file with class declaration and after change it in to empty declaration "class something;" it throw me this error, because I used object from header in NS_IMPLEMENT_INLINE_REFLECTION, then I divided it in to *.h NS_DECLARE_REFLECTION and in *.cpp I included somethingClass.h and add NS_IMPLEMENT_REFLECTION(). After that error gone. I hope you get some idea where is your problem.
Hi,
I used the NS_IMPLEMENT_REFLECTION in cpp, still got an error.
may be there some difference between NoesisGUI and WPF.
now I solved my project problem, Register InlineList as Ptr<BaseComponent>, then converter to (ObaservableCollection<Inline>*)
 
User avatar
sfernandez
Site Admin
Posts: 1880
Joined: 22 Dec 2011, 19:20

Re: confused about ObservableCollection

10 Apr 2018, 23:34

Our ObservableCollection only works with BaseComponent derived classes or boxed objects (also BaseComponent).
We should probably put an static assert to verify that and make it clear.

In your case it should be an ObservableCollection<BaseComponent> and then use it like this:
Ptr<ObservableCollection<BaseComponent>> collection = *new ObservableCollection<BaseComponent>();
collection->Add(Boxing::Box("Item 1"));
const NsString& str = Boxing::Unbox<NsString>(collection->Get(0));
And to define a DependencyProperty of this type, you have to include the Ptr in the type:
UIData->RegisterProperty<Ptr<ObservableCollection<Inline>>>(InlineListProperty, "Inline",
    FrameworkPropertyMetadata::Create(Ptr<ObservableCollection<Inline>>>(),
        FrameworkOptions_None, &OnPropertyChanged));

ObservableCollection<Inline>* GameGUI::RichTextBlock::GetInlineList() const
{
	return GetValue<Ptr<ObservableCollection<Inline>>>(InlineListProperty).GetPtr();
}

void GameGUI::RichTextBlock::SetInlineList(ObservableCollection<Inline>* InlineList)
{
	SetValue<Ptr<ObservableCollection<Inline>>>(InlineListProperty, InlineList);
}

Who is online

Users browsing this forum: No registered users and 8 guests