Blend -> C++ failures - formatting, namespaces
Hi,
I'm using Blend to create xaml-based UI with a C# mock, after which I copy the xaml files over and write up a C++ equivalent of the mock-ups.
Two areas consistently fail for me and need manual fix-up, meaning any time I copy and forget, I break my C++ version of the UI.
* String formatting - in Blend, if I want for example a time to be always two digits and a trailing 's' for Seconds, it looks like this
(Not doing this _sometimes_ works but sometimes breaks the C# build).
But this breaks in the C++ build, which instead wants what I expected:
* Namespaces - I put my view models in a "ViewModels" folder in the Blend project, which put them inside a namespace, the final result being (with project name as well); "rtg.ViewModels.MyViewModelName", and in xaml that is accessed like so
But this doesn't work in the C++ build - it can't recognize MyViewModelName, or it can't recognize SomeViewHere, and so on. Seems to happen to anything in a namespace like this.
So I try to register on the C++ side my types in the reflection macros - they're inside 'rtg' namespace already, then I try
and outside of namespace
and with ViewModels or viewModels in front
I can't get any combination to work unless I modify the xaml to not use namespaces at all
But this of course breaks in Blend, which seems to encourage namespaces, unless I modify it there and get rid of namespaces that naturally come with the directory structure. Or I maintain two xaml copies, which is not desirable.
What is the canonical and correct way to handle the namespace differences between Blend and Noesis C++? Same Q with the string formatting and why I need to sometimes escape it.
Thanks!
I'm using Blend to create xaml-based UI with a C# mock, after which I copy the xaml files over and write up a C++ equivalent of the mock-ups.
Two areas consistently fail for me and need manual fix-up, meaning any time I copy and forget, I break my C++ version of the UI.
* String formatting - in Blend, if I want for example a time to be always two digits and a trailing 's' for Seconds, it looks like this
Code: Select all
StringFormat=\{0:#\,0.0\}s
But this breaks in the C++ build, which instead wants what I expected:
Code: Select all
StringFormat={}{0:#,0.0}
Code: Select all
xmlns:viewModels="clr-namespace:rtg.ViewModels"
...
<UserControl.Resources>
<DataTemplate DataType="{x:Type viewModels:MyViewModelName}">
<views:SomeViewHere/>
</DataTemplate>
</UserControl.Resources>
So I try to register on the C++ side my types in the reflection macros - they're inside 'rtg' namespace already, then I try
Code: Select all
NS_IMPLEMENT_REFLECTION(MyViewModelName, "MyViewModelName")
Code: Select all
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.MyViewModelName")
Code: Select all
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.viewModels.MyViewModelName")
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.viewModels:MyViewModelName")
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.ViewModels.MyViewModelName")
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.ViewModels:MyViewModelName")
Code: Select all
<!--xmlns:viewModels="clr-namespace:rtg.ViewModels"-->
...
<UserControl.Resources>
<DataTemplate DataType="{x:Type MyViewModelName}">
<SomeViewHere/>
</DataTemplate>
</UserControl.Resources>
What is the canonical and correct way to handle the namespace differences between Blend and Noesis C++? Same Q with the string formatting and why I need to sometimes escape it.
Thanks!
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: Blend -> C++ failures - formatting, namespaces
Hi,
For the string formatting problem, could you please report it in our bugtracker? This is clearly an unsupported syntax we need to add.
Regarding the namespaces you need to match the reflection name in C++ with the full name of the C# type. In your examples the C# namespace is "rtg.ViewModels", so the full name of a view model defined there will be "rtg.ViewModels.MyViewModelName". This means your C++ reflection name should be exactly that:
And in the xaml it will be referenced like this:
There is another thing to take into account, the reflection type must be registered in Noesis before loading the xaml. This can be done by just calling TypeOf for that type when you are initializing your application and registering your components:
Hope this helps.
For the string formatting problem, could you please report it in our bugtracker? This is clearly an unsupported syntax we need to add.
Regarding the namespaces you need to match the reflection name in C++ with the full name of the C# type. In your examples the C# namespace is "rtg.ViewModels", so the full name of a view model defined there will be "rtg.ViewModels.MyViewModelName". This means your C++ reflection name should be exactly that:
Code: Select all
NS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.ViewModels.MyViewModelName")
Code: Select all
xmlns:viewModels="clr-namespace:rtg.ViewModels"
...
<UserControl.Resources>
<DataTemplate DataType="{x:Type viewModels:MyViewModelName}">
<views:SomeViewHere/>
</DataTemplate>
</UserControl.Resources>
Code: Select all
void RegisterComponents()
{
Noesis::TypeOf<rtg::MyViewModelName>();
...
Noesis::RegisterComponent<rtg::SomeViewHere>();
...
}
Re: Blend -> C++ failures - formatting, namespaces
Awesome, thanks!Hi,
For the string formatting problem, could you please report it in our bugtracker? This is clearly an unsupported syntax we need to add.
Regarding the namespaces you need to match the reflection name in C++ with the full name of the C# type. In your examples the C# namespace is "rtg.ViewModels", so the full name of a view model defined there will be "rtg.ViewModels.MyViewModelName". This means your C++ reflection name should be exactly that:And in the xaml it will be referenced like this:Code: Select allNS_IMPLEMENT_REFLECTION(rtg::MyViewModelName, "rtg.ViewModels.MyViewModelName")
There is another thing to take into account, the reflection type must be registered in Noesis before loading the xaml. This can be done by just calling TypeOf for that type when you are initializing your application and registering your components:Code: Select allxmlns:viewModels="clr-namespace:rtg.ViewModels" ... <UserControl.Resources> <DataTemplate DataType="{x:Type viewModels:MyViewModelName}"> <views:SomeViewHere/> </DataTemplate> </UserControl.Resources>
Hope this helps.Code: Select allvoid RegisterComponents() { Noesis::TypeOf<rtg::MyViewModelName>(); ... Noesis::RegisterComponent<rtg::SomeViewHere>(); ... }
One question - I've been using Noesis::RegisterComponent<T>() for all types - view models, views, etc. When should I use one over the other (TypeOf<T>()), if there's a difference?
-
sfernandez
Site Admin
- Posts: 3008
- Joined:
Re: Blend -> C++ failures - formatting, namespaces
This is the safest thing to do, as RegisterComponent registers the class in the factory (so an instance can be created by name when found in the XAML), and it also registers the type in the reflection.I've been using Noesis::RegisterComponent<T>() for all types - view models, views, etc.
If a class is only referenced in XAML as a type (with the x:Type extension) and there is no need to create objects of that class in XAML, registering the type with TypeOf<T>() should be enough (for example, view models that only appear in the DataType property of a DataTemplate).When should I use one over the other (TypeOf<T>()), if there's a difference?
Anyway I recommend you continue using RegisterComponent for everything.
Who is online
Users browsing this forum: Ahrefs [Bot] and 12 guests