Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am working on a small project where I want to connect a application with my WiFi lightbulb.
I already have the source files for the original Windows application but its made with WPF.
I would like to make it into a WinForm application because it makes things alot easier to build and develop since im not too good with WPF.


The issue is that even though I have the source code infront of me,
its very confusing. I thought it would be just copy/pasting it(Sounds lazy but I needed the same functionallity in a WinForm)
but it turns out that it wasnt that simple.

As you can see in the GitHub link below, everything is there.
https://github.com/dotMorten/LifxNet/tree/master/src/SampleApps/SampleApp.Windows

1. It find the connected lightbulb(s)
2. Its able to turn it on and off.

For some reason it was really hard to find which did which and how to convert it into a WinForm state and not a WPF.

What I need help with exactly is.

1. What makes the application able to find the lightbulb and how do I achieve the same thing in a WinForm application. (Since its apparently not the same code as a WPF)
2. Which function is it that controls the light switch (On/Off )

Any help is highly appriciated!

What I have tried:

I've tried reading through the code (the WPF application) and tried to recreate it in WinForm, I've also googled for about 2 days now and no success.
Posted
Updated 12-Aug-16 6:30am

Basically, don't: the two systems really aren't close enough to try and use the same code, or even to try and find "similar" controls.
Instead, use the debugger to run the WPF app, and find the code that does the work, by stepping through and using breakpoints to work out what is doing what.

Then consider copying the non-UI methods that deal with the light - or even the whole class if it's well written - and write your own UI to interface with it.

But the WPF specific code won't be much use to you here.
 
Share this answer
 
Comments
BladeLogan 12-Aug-16 12:14pm    
Thank you for the quick answer! I really appriciate it like really!

Here is the thing..
I found this in the code and it is locating the bulbs

ObservableCollection<LifxNet.LightBulb> bulbs = new ObservableCollection<LifxNet.LightBulb>();
LifxNet.LifxClient client = null;

Now I added that and the needed references but what do I do now? Do I add the "bulbs" to a listbox?
OriginalGriff 12-Aug-16 12:26pm    
That code doesn't locate anything: it just sets up a List-with-knobs-on; something else uses that collection to find the bulbs and fill it - but it isn't that code.
BladeLogan 12-Aug-16 12:29pm    
Im assuming you are talking about

private void Client_DeviceDiscovered(object sender, LifxNet.LifxClient.DeviceDiscoveryEventArgs e)
{
var _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
var bulb = e.Device as LifxNet.LightBulb;
if (!bulbs.Contains(bulb))
bulbs.Add(bulb);
});
}
OriginalGriff 12-Aug-16 13:58pm    
Looks like it to me...
Obviously, you need to understand code to convert it. Thus it does not make any sense to convert a WPF application to WinForms if you don't understand it.

Thus given that you don't understand WPF code, then the only remaining option would be to start from a blank WinForms application and add all required functionality to the WinForms application.

But then since WPF is much more powerful that WinForms, you have to ensure that you could implement everything you want in WinForms.

But if the WPF application, then why you would convert it? If something in not broken, then don't fix it.

Given than WPF is far superior in its abilities and should also support more easily Hi-DPI devices and some other modern features, it would almost never make sense to convert WPF code to WinForms.

Even if you want to convert the application to WinForms, you need to learn WPF to be able to know hot existing code works. Assuming that this code can be ported to WinForms, it would make no sense to do so once you understand WPF as you might at that point make any necessary adjustment directly to the WPF application.

WPF is far superior to WinForms because it help separate the UI from the logic so that it is possible to have one UI designer that don't do much programmation to design (in Blend) and to have a programmer that do the UI model without doing the UI. This is really nice as the UI is much more less tie to the code so it is easier to change the UI without modifying code.

WPF is also superior because it allows animation, transformation and more to be applied to the UI...

About your code
Here we don't do other work... As such, if you have a specific question, then show specific code that you would like to convert. Nobody here would convert a whole application for nothing.

And as already explained, the whole idea to convert WPF to WinForms make little sense. Usually, it would be much easier to convert from WinForms to WPF as one could in theory write an event-based WPF application. However, this make little sense as you loose all the benefit of UI and model separation...
 
Share this answer
 
Comments
BladeLogan 12-Aug-16 12:34pm    
I gave you 5 stars because yes, not understanding the code makes it really hard to understand what to covnert etc, I can see the logic behind the code but yeah you had some good points.. And why I would like to convert it is because I have my voice recognition application made in a WinForm and I would like to add the light switch functions to it. But I guess thats not going to happend, in most likley going to drop this project now and maybe look at some other lightbulbs that has a better documentaion when it comes to C#, or well.. something that is more broad.

Thanks for the answer!
Philippe Mori 12-Aug-16 13:23pm    
You can always combine WinForms and WPF but it assume a minimal understanding of each...

In my WinForms application, I do use WPF for things that cannot be easily done in WinForms.

So if you like the light bulb from your WPF application but need them in a WinForms application, the best compromise is to host a WPF windows inside a Form or UserControl. Adding an host control can be done directly from the designer.
BladeLogan 12-Aug-16 13:25pm    
I see what you mean! And that sounds great to be honest.
Quick question.. I just got told that I should drop WinForms because its dead and outdated and move on to WPF.. The thing is that I've been learning C# for about 6 months now and it just feels like I've learned it for no other reason than to be told that I need to drop it and learn something new. I would need an honest opinion about this, do i drop WinForms and start with WPF?
Philippe Mori 12-Aug-16 15:54pm    
If you want to write a large new desktop application that support Windows 7, WPF would make much more sense than WinForms.

For small utility applications that have only a few simple screens, using either won't matters as much. Simple WinForms design is a bit faster and simpler.

On the other hand, as soon as one try to write professional level code any advantage would vanish.

In WPF, the code is still in C#. The main difference is that in WPF one try harder to avoid code-behind. That way, it decouple the UI from the logic. Thus from a point of view of learning most of what you have learn might still be useful with WPF and most what you still need to learn is additional stuff (XAML) and other design related stuff part if you want customized styling and such...

There are a lot of tutorials on the internet about WPF. By the way, I am not yet an expert of WPF as I have done only a few windows with it and my code was sometime in WinForms-style...

In any professional application, the logic should be in the ViewModel and not in the UI (that is code behind files).

In fact, most modern UI framework use some kind of MVVM/MVC/MVP/...
BladeLogan 12-Aug-16 16:02pm    
Lets say i was going to look for a job as a developer in the .NET framework, what would be a higher chance of seeing in the workplace, Winform or WPF? Also thanks for the great pointers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900