Page 1 of 1

confused about ObservableCollection

Posted: 09 Apr 2018, 12:55
by UE4
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?

Re: confused about ObaervableCollection

Posted: 09 Apr 2018, 15:09
by UE4
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());
	}
}


Re: confused about ObaervableCollection

Posted: 10 Apr 2018, 13:37
by Wanderer
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.

Re: confused about ObaervableCollection

Posted: 10 Apr 2018, 15:27
by UE4
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>*)

Re: confused about ObservableCollection

Posted: 10 Apr 2018, 23:34
by sfernandez
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);
}