darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Trouble porting a custom Button from WPF to Noesis

03 Aug 2019, 20:31

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.
 
darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Trouble porting a custom Button from WPF to Noesis

03 Aug 2019, 20:39

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.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Trouble porting a custom Button from WPF to Noesis

07 Aug 2019, 12:19

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>
 
darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Trouble porting a custom Button from WPF to Noesis

08 Aug 2019, 01:04

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.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Trouble porting a custom Button from WPF to Noesis

08 Aug 2019, 19:27

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.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 79 guests