anakic / Jot

Jot is a library for persisting and applying .NET application state.
http://www.codeproject.com/Articles/475498/Easier-NET-settings
MIT License
633 stars 56 forks source link

Using Jot in a VB Application #36

Closed SezMe closed 3 years ago

SezMe commented 5 years ago

I am trying to use Jot in my VB WPF application but there is not a one-to-one correspondence with the C# Demo application. Is such an example available?

anakic commented 5 years ago

I haven't made one as I haven't done much VB.NET programming, but if you paste your code, we can probably figure out if anything is wrong and how to fix it.

SezMe commented 5 years ago

Here is my Services Module:

`Imports Jot

Module TrackingService `Public Trackr As New Tracker()

`Sub New()

    Trackr.Configure(Of Window)().Id(Function(w) w.Name).Properties(Function(w) New With
            {
            Key w.Height,
            Key w.Width,
            Key w.Left,
            Key w.Top
            }
            ).PersistOn(NameOf(Window.Closed))
End Sub

`End Module

There is not a New method for Windows so I have:

Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized Trackr.Track(Me) End Sub

That line throws this error: The type initializer for 'PIM.TrackingService' threw an exception (PIM is the name of my application).

SezMe commented 5 years ago

Yuck! How do I reduce the font size?

anakic commented 5 years ago

What does the exception message say?

SezMe commented 5 years ago

Exception thrown: 'System.ArgumentException' in Jot.dll Exception thrown: 'System.TypeInitializationException' in PIM.exe

SezMe commented 4 years ago

anakic, do you need any more information from me to try to diagnose the problem?

anakic commented 4 years ago

Hi. It's not going to be easy without giving it a try. In my Visual Studio, I don't have VB.NET windows forms projects available, which probably means I'm missing an installer workload. I'm a bit swamped at the moment, so I don't know when I'll be able to check but I'll leave the issue open so I revisit it when I get the chance.

SezMe commented 4 years ago

I understand - get at this when you can.

To help, I've created a very simple project with only about 10 lines of code. It demonstrates the issue I have run into. See the attached.

I hope that helps.

JotDemo.zip

anakic commented 4 years ago

It looks like with VB.NET the projection of the anonymous type gets wrapped inside a Convert expression by the VB.NET compiler:

image

Any ideas why VB.NET is casting the result to object instead of keeping the anonymous type? I don't use VB.NET so I'm not sure if it's a syntax thing or just the way the VB.NET compiler is implemented.

Anyway, Jot analyzes the expression and sees the Convert expression rather than the NewExpression that it expects. The NewExpression gets nested inside the Convert expression but Jot does not check there, so it throws an error.

You can get around this by using multiple Property methods calls instead of the Properties method:

Trackr.Configure(Of Window)().Id(Function(w) w.Name, SystemInformation.VirtualScreen.Size) _
            .Property(Function(w) w.Top) _
            .Property(Function(w) w.Left) _
            .Property(Function(w) w.Height) _
            .Property(Function(w) w.Width) _
            .PersistOn(NameOf(Window.Closing)).StopTrackingOn(NameOf(Window.Closing))

The above should work fine.

One more suggestion - for WPF use the SourceInitialized event rather than Loaded to start tracking the window. Load happens after the first render, so if you use it, you'll see the window jump from it's default location to the stored one.

anakic commented 4 years ago

@SezMe did you give the above a try?

SezMe commented 4 years ago

I did, immediately, and wrote it up here but the post is not here! Very Sorry! I'll use your suggestion and give you the results and answer your question right now.

SezMe commented 4 years ago

First, here is why VB.NET is casting the result to an object (see link below):

"The class has no usable name, inherits directly from Object, ...."

Thus, an anonymous type in VB MUST be an object.

When I tried your modifications previously, they did not work thus the longer (missing) write up. Now, however, the approach you suggested works just fine.

Thank you very much.

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types

SezMe commented 4 years ago

For those who may be reading this who want to use Jot in a VB application, the SystemInformation Class only exists for Windows Forms, but NOT for wpf. In order to use Jot in VB one must use:

Imports System.Windows.Forms

anakic, is there an equivalent Class in wpf Windows that would provide the same functionality as SystemInformation does for Forms

ElektroStudios commented 4 years ago

Jot analyzes the expression and sees the Convert expression rather than the NewExpression that it expects. The NewExpression gets nested inside the Convert expression but Jot does not check there, so it throws an error.

You can get around this by using multiple Property methods calls instead of the Properties method

@anakic thanks for the solution. But from December 2019, I wonder whether the usage of Properties method (the internal logic to analyze the expression) is a thing that could be fixed or improve soon for VB.NET users of JOT?.

anakic commented 3 years ago

This should be solved with the latest commit and in the current NuGet package (2.1.8). You should now be able to use this style of configuring tracking:

.Properties(Function(w) New With
            {
            Key w.Height,
            Key w.Width,
            Key w.Left,
            Key w.Top
            }