Page 1 of 1

Trouble porting a custom Button from WPF to Noesis

Posted: 03 Aug 2019, 20:31
by darthmaule2
I have a custom Button defined in "StandardToggleButton.xaml" (abbreviated):
<Button x:Class="ButtonTest.StandardToggleButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ButtonTest"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d">
    <Button.Resources>
        <Color x:Key="GridForegroundOnColor">#EDEEEF</Color>
        <Color x:Key="GridBackgroundOnColor">#3B73B9</Color>
        <Color x:Key="GridForegroundOffColor">#5C5F63</Color>
        <Color x:Key="GridBackgroundOffColor">#313337</Color>

        <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly=
							local:StandardToggleButton, ResourceId=StandardToggleButtonControlTemplate}" 
							TargetType="{x:Type local:StandardToggleButton}">

	...
        </ControlTemplate>

        <Style TargetType="{x:Type local:StandardToggleButton}">
            <Setter Property="Template"
				   Value="{StaticResource {ComponentResourceKey
						  TypeInTargetAssembly=local:StandardToggleButton,
						  ResourceId=StandardToggleButtonControlTemplate}}" />
        </Style>
    </Button.Resources>
</Button>
The code-behind StandardToggleButton.xaml.cs
#if USE_WPF
using System.Windows;
using System.Windows.Controls;
#else
using Noesis;
#endif

namespace ButtonTest
{
    public partial class StandardToggleButton : Button
    {
        /// <summary>
        /// The constructor.
        /// </summary>
        public StandardToggleButton()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty IsOnProperty = DependencyProperty.Register("IsOn", typeof(bool), typeof(StandardToggleButton), new FrameworkPropertyMetadata());
        public bool IsOn
        {
            get
            {
                return (bool)GetValue(IsOnProperty);
            }
            set
            {
                SetValue(IsOnProperty, value);
            }
        }

#if !USE_WPF
        private void InitializeComponent()
        {
            GUI.LoadComponent(this, "StandardButtonControl.xaml");
        }
#endif
    }
}
Used like this in MainWindow.xaml:
<Window x:Class="ButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ButtonTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:StandardToggleButton></local:StandardToggleButton>
    </Grid>
</Window>
Minimal example attached.

Re: Trouble porting a custom Button from WPF to Noesis

Posted: 03 Aug 2019, 20:39
by darthmaule2
Looks like I'm not able to attach files - not sure what the issue is. I tried all three browsers and via drag and drop and the "Add files" button.

Re: Trouble porting a custom Button from WPF to Noesis

Posted: 07 Aug 2019, 12:19
by sfernandez
Hello,

Noesis doesn't implement ComponentResourceKey, the xaml parser should be logging that as an error like:
[noesis] StandardToggleButton.xaml(11): Can't set an extension of type 'ComponentResourceKey' as Key
But you can use string keys in this case and it works as expected:
<ControlTemplate x:Key="StandardToggleButtonTemplate" TargetType="{x:Type local:StandardToggleButton}">
    ...
</ControlTemplate>
<Style TargetType="{x:Type local:StandardToggleButton}">
    <Setter Property="Template" Value="{StaticResource StandardToggleButtonTemplate}" />
</Style>

Re: Trouble porting a custom Button from WPF to Noesis

Posted: 08 Aug 2019, 01:04
by darthmaule2
That fixed it, thanks.

You mention the XamlPlayer... is there a standard way to integrate Noesis Xaml parsing output into a build? My current setup provides no debug output on Xaml parsing which makes it very difficult to debug Xaml issues. I can drag & drop a xaml into the XamlPlayer, but it would be nice if my build output just displayed whatever parsing errors occurred... right now it just seems to fail silently.

Re: Trouble porting a custom Button from WPF to Noesis

Posted: 08 Aug 2019, 19:27
by sfernandez
By default we were logging all that information to the debugger Output window, unfortunately I just found that it was only working for the debug version of the library (we were using System.Diagnostics.Debug.WriteLine), so it was in fact useless if user didn't build their own debug version of Noesis.App package.

We decided to add support in Application class to override how logs and unhandled exceptions are managed in next release:
public class App : Application
{
  ...
  
  protected override void OnLog(LogLevel level, string channel, string message) { ... }
  
  protected override void OnUnhandledException(Exception e) { ... }
}
By default these functions will use Console.WriteLine() to output the log message and the exception.