Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wpf Custom Control

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Licensing Support
View previous topic :: View next topic  
Author Message
Dom



Joined: 19 May 2013
Posts: 34

PostPosted: Wed Nov 18, 2015 2:56 pm    Post subject: Wpf Custom Control Reply with quote

Hi

I have just finished a wpf custom control to which I would like to add Infralution Licensing. I have looked at the Winforms sample for a control and the wpf application (the vb variants as I've written this in vb), however these obviously don't match the layout you see in a wpf custom control.

To that end if you were to create a new custom control for wpf in visual studio 2015 where in the resulting template would you place the licensing code. Obviously there's no application xaml or corresponding Application.xaml.vb to play with., just a generic.xaml file and a CustomControl1.vb file. I'd also like to know in the case of custom control wheteher it's better to add a reference to the relevant infralution dll (and if so what does one do with regards to distribution (given that ones own end users will need to distribute the control (always assuming of course that it sells!), or to embed the relevant infralution code files in the control itself, and again some idea of where).

Many thanks

Dom
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Wed Nov 18, 2015 11:45 pm    Post subject: Reply with quote

Custom WPF controls are licensed in exactly the same way as Windows Forms controls (except that instead of referencing Infralution.Licensing.Forms you should reference Infralution.Licensing.WPF).

The first thing is to add a derived LicenseProvider class to your custom control project. In the sample Licensed Control project this is the MyControlLicenseProvider class. It derives from EncryptedLicenseProvider. It is also possible to use authenticated licensing for controls by deriving from AuthenticatedLicenseProvider - but for simplicity the sample project shows simple Encrypted Licensing.

Next you tell .NET that this is a licensed control by adding the LicenseProvider attribute to your custom control class definition eg:

Code:
Imports System.ComponentModel
Imports Infralution.Licensing.WPF
<LicenseProvider>
Public Class CustomControl1
    Inherits Control


Finally you add a constructor (Sub New method) for your control and put the code that checks the license in the constructor (as the windows forms sample does.

Code:

    '' <summary>
    '' The license context last time we checked the license
    '' </summary>
    '' <remarks>
    '' This is used to reduce the number of times the license check needs to be done.
    '' If your control is likely to be used in multiple places (eg like a text box) then checking the license each
    '' time a control is created can be very annoying when in evaluation mode.
    '' </remarks>
    Private Shared _lastDesignContext As LicenseContext
    Private Shared _licenseChecked As Boolean = False

    Sub New()
        ' Is the control being displayed in the designer?
        '
        If LicenseManager.CurrentContext.UsageMode = LicenseUsageMode.Designtime Then

            ' at design time we must check the license every time or else our license may not
            ' get compiled into the application resources
            '
            If Not LicenseManager.IsLicensed(GetType(CustomControl1)) Then

                ' only show the install dialog if we haven't already done so for this form
                '
                If Not LicenseManager.CurrentContext Is _lastDesignContext Then

                    ' Display the evaluation dialog until the user installs a license or
                    ' selects Exit or Continue
                    '
                    Dim license As EncryptedLicense = Nothing
                    While license Is Nothing
                        Dim evaluationMonitor As New RegistryEvaluationMonitor("MyControlEvaluationPassword")
                        Dim dialog As New EvaluationDialog(evaluationMonitor, PRODUCT_NAME)
                        Dim dialogResult As EvaluationDialogResult = dialog.ShowDialog()
                        If dialogResult = EvaluationDialogResult.Exit Then
                            Throw New LicenseException(GetType(CustomControl1), Me, "Control not Licensed")
                        End If
                        If dialogResult = EvaluationDialogResult.Continue Then Exit While
                        If dialogResult = EvaluationDialogResult.InstallLicense Then
                            Dim licenseForm As New EncryptedLicenseInstallForm()
                            license = licenseForm.ShowDialog(New MyControlLicenseProvider(), license, PRODUCT_NAME)
                        End If
                    End While
                    _lastDesignContext = LicenseManager.CurrentContext
                End If

            End If

        Else

            ' at runtime only check the license if we haven't already done so
            '
            If Not _licenseChecked Then

                ' show a nag screen if the control is not licensed
                '
                If Not LicenseManager.IsLicensed(GetType(CustomControl1)) Then
                    MessageBox.Show("This application was created using an unlicensed version of MyControl", "Unlicensed Application")
                End If
            End If
        End If
        _licenseChecked = True
    End Sub


See the section in the help on "Component and Control Licensing" for more detail.

To get started you can just add a reference to Infralution.Licensing.WPF - but this requires your customers to also deploy the assembly. When you purchase ILS we provide the source code for the application classes so that you can include these directly in your project avoiding the need to distribute the separate assembly.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Dom



Joined: 19 May 2013
Posts: 34

PostPosted: Thu Nov 19, 2015 7:06 am    Post subject: Reply with quote

Many thanks for the clear and concise reply. Now even as I type this (given that we are talking about controls here rather than applications) I realise that the answer is probably no, however is there a way when the unlicensed control is being used in design time to either just have the nag screen appear the once and then no more. ie the first time that a form or view is loaded with a copy of my unlicensed control on the nag screen appears thereafter it doesn't.

The next question involves the issue of an end user simply deciding to use the control unlicensed and then wrap it in their own application, figuring that the end user wont care too much about a nag screen (unlikely I know but there's none so queer as folk). Is there a way to incorporate the 30 day trial feature that ils provides such that if an end user uses an unlicensed control then there end user will see a nag screen for thirty day and then on day 31 their (the end user who chose to use the unlicensed control)application will automatically shut down just after the start up.

I've seen this happen with a couple of third party vendor's controls now because stupidly I have forgotten to add the necessary entry to the licences.licx file.

And finally, but related to the above point. The reason I have been getting caught out on occasions is because I find it much easier to simply create controls straight in xaml (or back when I used Winforms directly in code). So Although I've not tested it yet I assume that if I drag my custom wpf control from the toolbox to the design surface an entry will be automatically added to the licenses.licx file. But what happens if I simply add a namespace to the xaml and then create an instance of my control in xaml, does the relevant entry get made in the licences.licx file then.

I was talking to the CTO of Component One the other day about this and he was of the opinion that this behaviour was probably caused by an anomaly in visual studio, but I wondered if you'd seen it or had any suggestions to combat this with my own control using ils if indeed this could be an issue.
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu Nov 19, 2015 8:54 am    Post subject: Reply with quote

Quote:
is there a way when the unlicensed control is being used in design time to either just have the nag screen appear the once and then no more. ie the first time that a form or view is loaded with a copy of my unlicensed control on the nag screen appears thereafter it doesn't


That is the way the code I posted works - it will only display the evaluation dialog if it hasn't already done so for the design context (ie Visual Studio)

Code:

Is there a way to incorporate the 30 day trial feature that ils provides such that if an end user uses an unlicensed control then there end user will see a nag screen for thirty day and then on day 31 their (the end user who chose to use the unlicensed control)application will automatically shut down just after the start up.


You could definitely do something like that by modifying the logic in the constructor - if you remove the check on the design time context and always do that logic then I think it will probably do what you want ie

Code:
    Private Shared _licenseChecked As Boolean = False

    Sub New()

        ' we must check the license every time or else our license may not
        ' get compiled into the application resources
        '
        If Not LicenseManager.IsLicensed(GetType(CustomControl1)) Then

            ' only show the install dialog if we haven't already done so for this form
            '
            If Not _licenseChecked Then

                ' Display the evaluation dialog until the user installs a license or
                ' selects Exit or Continue
                '
                Dim license As EncryptedLicense = Nothing
                While license Is Nothing
                    Dim evaluationMonitor As New RegistryEvaluationMonitor("MyControlEvaluationPassword")
                    Dim dialog As New EvaluationDialog(evaluationMonitor, PRODUCT_NAME)
                    Dim dialogResult As EvaluationDialogResult = dialog.ShowDialog()
                    If dialogResult = EvaluationDialogResult.Exit Then
                        Throw New LicenseException(GetType(CustomControl1), Me, "Control not Licensed")
                    End If
                    If dialogResult = EvaluationDialogResult.Continue Then Exit While
                    If dialogResult = EvaluationDialogResult.InstallLicense Then
                        Dim licenseForm As New EncryptedLicenseInstallForm()
                        license = licenseForm.ShowDialog(New MyControlLicenseProvider(), license, PRODUCT_NAME)
                    End If
                End While
                _licenseChecked = True
            End If
        End If

    End Sub


I haven't checked this code - but I think it should do what you are asking for.

Quote:
But what happens if I simply add a namespace to the xaml and then create an instance of my control in xaml, does the relevant entry get made in the licences.licx file then


No in that case the user has to add the entry to the .licx file themselves. We have an FAQ on typical control licensing issues for our own Virtual Tree control that covers this:

http://www.infralution.com/phpBB2/viewtopic.php?t=21
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Licensing Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group