WPF native controls in Xamarin - wpf

I was playing around with Xamarin and WPF after following the BoxViewClock tutorial from the Xamarin website but apart from that one article, which has been duplicated on a number of sites I cannot find any other advanced examples.
Is it currently possible to add/use a WPF custom control to an xamarin forms page? or for that matter a view/user control?

You typically start creating a custom control by deriving from one of Xamarin.Forms controls and you do it in shared .NET Standard library with Xamarin.Forms UI project. As soon as there's behavior which is not feasible within Xamarin.Forms you create a native renderer in a project targeting specific platforms, in our case in WPF project. Thus there're 2 steps for creating custom controls in Xamarin.Forms:
Derive from existing Xamarin.Forms controls in Shared project
Add extensions to it via platform renderer (or behaviour which is a custom case of renderer) in platform project.
You can find example of creating "ExtendedLabel" control which adds to the standard Xamarin.Forms' Label with capability to change cursor to Hand when mouse hovers over the label in WPF application:
Xamarin.Forms control: https://github.com/maxim-saplin/CrossPlatformDiskTest/blob/master/Saplin.CPDT.UICore/Controls/ExtendedLabel.cs
WPF platfrom renderer: https://github.com/maxim-saplin/CrossPlatformDiskTest/blob/master/Saplin.CPDT.WPF/ExtendedLabelRenderer.cs - you can ignore the code after "if (label.FormattedText != null &&..." which is a workaround for a bug in Xamarin.WPF and not related to the questions
P.S.: in the repo there's also a renderer for macOS which implements the same behaviour

Related

How can I make a stack panel with a transparent background and blur effect in WPF without an image?

I have only found solutions to this question that work with images, but I want to be able to overlook a stack panel with this "effect", for example. I actually want it to look something like the one in Windows, for example, like here:
so that you can see a slight transparency in the background.
This kind of translucent texture is called Acrylic and is a Fluent Design System component on Windows 10.
If you want to bring this kind of brush to your app, I would recommend you to look into using the WinUI library. This is the modern and native user experience (UX) framework for Windows desktop applications going forward.
WinUI 2.x controls be used WPF applications using XAML Islands.
WinUI 3 is the next version of WinUI that includes a full native UI platform that can be used on top of the latest version of .NET and be seen as a replacement for WPF and WinForms GUIs.
There is no native support for Acrylic in WPF.

Using AdControl to display ads in a WPF application packaged for UWP

I (appear to) have successfully created a UWP package for my WPF application using the following guide:
https://learn.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-packaging-dot-net
Now I would like to take advantage of the AdControl class to display banner ads in my project, as detailed here:
https://learn.microsoft.com/en-us/windows/uwp/monetize/adcontrol-in-xaml-and--net
Is this possible? I don't see the "Universal Windows" section under References for my WPF project, which I can guess is because it’s not an original Universal Windows project. I do see it under References for the UWP package wrapper project I created using the guide above, but that doesn't help me show ads in the WPF project.
Any help appreciated.
You can add Universal Windows reference and use the API in your project as described here but you cannot use AdControl since XAML/UI in WPF is not the same as XAML in UWP and the AdControl is a UWP XAML control:
As mentioned above, there are exceptions to the rule that Windows 10
APIs are accessible from PC software. The first big exception concerns
XAML UI APIs. The XAML framework in UWP is different from the one in
WPF and you really don’t want to be mixing them up, anyways.

Can we integrate a WinForms application with a WPF application?

I want to integrate two existing applications into one. One of those apps is built on Windows Forms and the other on WPF.
Is it possible to achieve this?
WPF supplies the WindowsFormsHost class that allows you to host WinForms controls inside a WPF window; conversely, WinForms supplies ElementHost that allows you to host WPF controls inside a form.
Unfortunately how well things work out is highly dependent on exactly what you are doing, last time I checked there were more than a few rough edges. For more information, definitely start from this MSDN page.
If you want to have "independent" WPF windows and WinForms forms inside the same application, you will have to make both frameworks "share" some code in your UI thread's message loop. For a primer on how to do that, see here.
There are various classes to help you with this.
For hosting Windows Forms controls in a WPF Window, you can use the WindowsFormsHost class. For hosting WPF Controls in a Windows Forms Window, you can use the ElementHost class.
You can look here for mor information on the subject (windows forms section):
http://msdn.microsoft.com/en-us/library/ms753178.aspx

Use WinForms custom textBox in WPF?

I have a WPF application that is in need of syntax highlighting. THIS is exactly what I'm looking for... however its for WinForms. Can I, and if so, how do I add it to my WPF program?
http://wpfsyntax.codeplex.com/
http://devhawk.net/2009/07/09/syntax-highlighting-textboxes-in-wpf-a-sad-story/
There's a walkthrough on MSDN
This walkthrough steps you through an application that hosts a Windows Forms composite control to perform data entry in a WPF application. The composite control is packaged in a DLL. This general procedure can be extended to more complex applications and controls.
In this scenario the WinForms controls are in a separate dll which needs to be signed.
The WPF application needs to use a WinFormsHost control as the container for the control.

.Net 2.0 custom controls and WPF

out of curiosity i just want to know those custom control which was developed in .Net 2.0 version that can be reused in WPF application. if yes then what would be the process. usually we add custom control to the toolbox and just drag & drop those onto form and easily manipulate and work with them. so can we follow the same step to include those custom control developed with .net v2.0 onto WPF form or not. please discuss. thanks
You can reuse Windows Forms Controls in a WPF application (and the reverse is also true, you can host WPF controls in a Windows Forms application) .
Here is an official documentation link on this subject: Walkthrough: Hosting a Windows Forms Composite Control in WPF

Resources