User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: translation resource directionary

09 Nov 2020, 18:45

Cool :)

Thanks for the update.

Tags:
 
User avatar
jsantos
Site Admin
Posts: 3015
Joined: 20 Jan 2012, 17:18
Contact:

Re: translation resource directionary

09 Nov 2020, 18:55

Was a difficult birth as usual and more than I like but in the end it works.
Could you please elaborate about this? We are always trying to improve our API.
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: translation resource directionary

09 Nov 2020, 19:19

This is more a me problem than a you problem I guess. But let me try to explain. As you may remember from my first beginner questions I started the transition to Noesis at the beginning of this year with no prior knowledge of XAML. Now the XAML part is the easy one. Creating layouts is not much different than in any other UI description language. In the end everything is a Grid or Stackpanel.

I struggled more with data bindings and interactivity. Most of the time I try something, then it doesn't work, then I need to figure out why it doesn't work. It doesn't help that XAML is C# centric and when you google something most examples are in C#. For example setting the color to a button according to some value in code. I take it you reimplemented WPF functionality here and this is how it works and you can't do much about it. But for a newcomer it's pretty hard to understand why I simply can't set a color to an UI element. Why does it have to be that complicated? I had that feeling a few times during the last months while working on this without specifically remembering what it was now.

I'm also someone learning most things from examples. So having my color converter now and knowing how and where to implement it will help with all other needed converters in the future.

While we're at it, do you have an example (in C++ would be nice) of a dragable window that could be positioned anywhere on the screen on top of a layout. Is that even possible with Noesis?
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: translation resource directionary

09 Nov 2020, 20:47

While we're at it, do you have an example (in C++ would be nice) of a dragable window that could be positioned anywhere on the screen on top of a layout. Is that even possible with Noesis?
There is no such example, but using MouseDragElementBehavior you can move any UI element inside the bounds of a container using the mouse.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
  <Border Background="Silver" Margin="100">
    <Rectangle Width="100" Height="50" Fill="Red">
      <i:Interaction.Behaviors>
        <ei:MouseDragElementBehavior ConstraintToParentBounds="True" />
      </i:Interaction.Behaviors>
    </Rectangle>
  </Border>
</Grid>
To place a "window" on top of a layout you just need to add it as the last children or use Panel.ZIndex to control the rendering z-order of children. For example, you can have a Grid added as the last children, and then you can add the windows to that Grid so they always appear on top of the rest of the UI.
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid x:Name="LayoutRoot">
    <!-- your UI here -->
    <Grid x:Name="TopWindows"></Grid>
  </Grid>
</UserControl>
Grid* topWindows = FindName<Grid>("TopWindows");
topWindow->GetChildren()->Add(messageWindow);
//...
topWindow->GetChildren()->Remove(messageWindow);
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: translation resource directionary

10 Nov 2020, 11:59

For example setting the color to a button according to some value in code. I take it you reimplemented WPF functionality here and this is how it works and you can't do much about it. But for a newcomer it's pretty hard to understand why I simply can't set a color to an UI element.
In the Localization demo I put as an example before we defined a SolidColorBrush in xaml so the Converter could return a Color directly, which will be better to avoid creating new brush objects every time the binding changes:
<ToggleButton>
  <ToggleButton.Background>
     <SolidColorBrush Color="{Binding Activity, Converter={StaticResource ActivityColorConverter}}"/>
  </ToggleButton.Background>
</ToggleButton>
bool TryConvert( BaseComponent* value, const Noesis::Type* targetType, BaseComponent*, Noesis::Ptr<BaseComponent>& result )
{
  if ( targetType == Noesis::TypeOf<Noesis::Color>() )
  {
    result = Noesis::Boxing::Box( Noesis::Color( 255, 0, 255 ) );
    return true;
  }
  return false;
}
Another option that probably fits in your situation is to use brush resources so you can return the same brushes for all the items with the same activity, being even more efficient. In this case you can make the converter expose a ResourceDictionary property you can fill in the xaml with your desired brushes:
struct ActivityBrushConverter: BaseValueConverter
{
    ActivityBrushConverter(): _resources(MakePtr<ResourceDictionary>()) { }
    ResourceDictionary* GetResources() const { return _resources; }

    bool TryConvert(BaseComponent* value, const Type*, BaseComponent*, Ptr<BaseComponent>& result) override
    {
        if (Boxing::CanUnbox<Activity>(value))
        {
            switch (Boxing::Unbox<Activity>(value))
            {
                case Activity::None: result.Reset(_resources->Get<Brush>("None")); break;
                case Activity::Eat: result.Reset(_resources->Get<Brush>("Eat")); break;
                case Activity::Sleep: result.Reset(_resources->Get<Brush>("Sleep")); break;
                ...
            }
        }

        return result != nullptr;
    }

private:
    Ptr<ResourceDictionary> _resources;
    NS_IMPLEMENT_INLINE_REFLECTION(ActivityBrushConverter, BaseValueConverter, "MyNamespace.ActivityBrushConverter")
    {
        NsProp("Resources", &ActivityBrushConverter::GetResources);
    }
};
And then in xaml:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:MyNamespace">
  <Grid.Resources>
    <local:ActivityBrushConverter x:Key="activityBrushConverter">
      <local:ActivityBrushConverter.Resources>
        <SolidColorBrush x:Key="None" Color="Transparent"/>
        <SolidColorBrush x:Key="Eat" Color="Pink"/>
        <SolidColorBrush x:Key="Sleep" Color="Blue"/>
        ...
      </local:ActivityBrushConverter.Resources>
    </local:ActivityBrushConverter>
  </Grid.Resources>
  <ToggleButton Background="{Binding Activity, Converter={StaticResource activityBrushConverter}}"/>
</Grid>

Who is online

Users browsing this forum: No registered users and 0 guests