I'm working on a project that uses the Silicon Labs SDK. After making a wrapper for their SDK I made a WPF project to visualize some of the results from the SDK. My first test was with the GetNumDevices function. My WPF knowledge is not very strong so sorry about anything that makes your eyes bleed, but for a demonstration let me show you what I have. First for the API method I am talking about.
[DllImport("CP210xManufacturing.dll", CallingConvention = CallingConvention.StdCall)]
public static extern CP210x_STATUS CP210x_GetNumDevices(ref int lpdwNumDevices);
instead of making 2 labels and 1 button to visualize this method I decided to make a user control.
CP210UserControl.xaml
<UserControl x:Class="SiliconLabsRosettaStone.CP210UserControl"
x:Name="panel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="300"
DataContext="{Binding ElementName=panel}">
<Border BorderBrush="DarkGreen" BorderThickness="3" Margin="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
<ColumnDefinition Width="75"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Caption}" Margin="3,0" />
<Border Grid.Column="1" BorderBrush="Black" BorderThickness="2" Margin="3,0">
<TextBlock x:Name="responseTextBlock" Text="{Binding Response}" />
</Border>
<Button Grid.Column="2" Content="Execute" Click="Button_Click" Margin="3,0" />
</Grid>
</Border>
</UserControl>
CP210UserControl.xaml.cs
public partial class CP210UserControl : UserControl
{
public CP210UserControl()
{
InitializeComponent();
}
public void SetupUserControl(Func<CP210UserControl, CP210x_STATUS> function)
{
this.cp210Function = function;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CP210x_STATUS response = cp210Function(this);
this.responseTextBlock.Background = response == CP210x_STATUS.CP210x_SUCCESS ? null : new SolidColorBrush(Colors.Pink);
}
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public string Response
{
get { return (string)GetValue(ResponseProperty); }
set { SetValue(ResponseProperty, value); }
}
public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(CP210UserControl), new PropertyMetadata("CP210x"));
public static readonly DependencyProperty ResponseProperty = DependencyProperty.Register("Response", typeof(string), typeof(CP210UserControl), new PropertyMetadata(""));
private Func<CP210UserControl, CP210x_STATUS> cp210Function;
}
That is the file that I have a question about because of its usage.
this is how I use it in my main window
MainWindow.xaml
<StackPanel>
<local:CP210UserControl x:Name="cpGetNumDevices" Grid.Row="1" Grid.ColumnSpan="3" Caption="CP210x_GetNumDevices" />
</StackPanel>
Now for the part I hate. How i hook it up :/
MainWindow.xaml.cs
private void Window_Loaded(object sender, RoutedEventArgs e)
{
cpGetNumDevices.SetupUserControl(getNumberDevices);
}
private CP210x_STATUS getNumberDevices(CP210UserControl control)
{
int devices = 0;
var result = CP210xWrapper.CP210x_GetNumDevices(ref devices);
control.Response = string.Format("Number Of Devices:{0}", devices);
return result;
}
I would think that I could make the xaml look like this
<local:CP210UserControl x:Name="cpGetNumDevices" Grid.Row="1" Grid.ColumnSpan="3" Caption="CP210x_GetNumDevices" cp210Function="getNumberDevices" />
if i change my user control to this.
public Func<CP210UserControl, CP210x_STATUS> cp210Function
{
get { return (Func<CP210UserControl, CP210x_STATUS>)GetValue(cp210FunctionProperty); }
set { SetValue(cp210FunctionProperty, value); }
}
public static readonly DependencyProperty cp210FunctionProperty = DependencyProperty.Register("cp210Function", typeof(Func<CP210UserControl, CP210x_STATUS>), typeof(CP210UserControl), new PropertyMetadata(null));
but when I run the program I get this error
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'Func`2' type does not have a public TypeConverter class. Error at Line 7 Position 122.
So how can I make said TypeConverter? is it even possible?
EDIT
here is what basically my end game.