XAML C# + Unmanaged C++ - c#

I have a large amount of code in C++ (unmanaged), and I'd like to take advantage of the new fluent design features in the latest Windows 10 WPF/XAML. I found a very old sample for Windows 8.1 which shows a C# XAML page being driven by a C++ library, however, both of these are Universal Windows Platform (UWP) projects.
As far as I can tell it is not possible to reference a non-UWP library from a UWP project. The alternative, then, is to use the slightly older WPF components to host XAML controls using functions that allow you to load and render XAML pages (created in Blend, for example), but I've not been able to find a working example of this particular configuration.
Any thoughts or ideas about how to attack this issue will be appreciated, if any of you have figured out how to bridge C# WPF + XAML and unmanaged C++ projects, please let me know!

Related

Xamarin cross platform iOS app

I am a .NET developer. I have a complete Android app developed in Android studio. I want to develop that app in Xamarin cross platform.
Which project template should I choose to develop my app both for Android and iOS? Which one of these will be easier to develop a cross platform app?
Blank App (Native Portable)
Blank App (Native Shared)
Blank App (Xamarin Forms Portable)
Blank App (Xamarin Forms Shared)
These links will help you to decide which project type you'll going to use:
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/
https://xamarinhelp.com/portable-class-library-pcl-vs-shared-projects/
http://hotkrossbits.com/2015/05/03/xamarin-forms-pcl-vs-shared-project/
Xamarin Shared Library and PCL
http://xfcomplete.net/general/2016/01/19/pcl-or-shared-project/
Here are some aspects:
Xamarin Forms vs Native
Native you have to specify your Templates for each OS
Xamarin Forms, one Layout for all OS - But you can't use platform specific views/controls
Portable:
No platform specific code (it's possible to write your own lib for it, but for the most important features you will find such libraries on github or over nuget - e.g. https://github.com/XLabs/Xamarin-Forms-Labs for Controls, Camera-/Location-/..-access)
Easier to code Apps for multiple platforms - no need for compiler directives for code switching depending on platform
no need to think about what code is working on which os
easier to maintain and test (in my opinion, with a good design of your architecture it's not a big deal to make a shared project good maintainable and testable like an portable app)
Which platforms do you use? Are these supported in the PCL-libraries you need?
Shared:
Easier acces on platform-specific features
more versatile / flexible
Compiler directives for OS
Even though you have to decide which type will good fit with your values and needs.
I hope, I could help a little bit.

A very complicated workaround for using a C# dll with an Android app? (xamarin)

I have an proprietary Android app (created with Android Studio) and I need to add some functionality which requires to use a C# DLL.
The app is connecting wirelessly to a development board that does some sensor measurements. Normally on windows apps the dll is used to do some complex calculations of calibration coefficients for the measuring.
My problem is:
I don't have the the source code of the DLL and most likely I won't be approved to access it.
I've read that you can use xamarin to make android apps and use C# dlls but I can't rewrite the whole app for xamarin just to use the dll (also I've never used xamarin)
So far I had the idea to build a simple http server with C# that gets parameters with a GET request, runs them through the dll and returns the results. However it's not an ideal solution because it requires a separate pc to run the server and the adndroid device to have a network connection to it.
My second idea is to build a separate app with xamarin that uses the dll and make my main app start it just to calculate the coefficients and get the results. Perhaps make the second app not visible in the launcher and somehow distribute the two apps together.
Since I am not experienced with android development and especially xamarin and I don't have a lot of time to waste on this project I want to know if my idea is feasible or if there is a better alternative.
Can you suggest some useful tutorials for xamarin and using dlls with it?
===============================================
Edit
I have managed to get the source code of the dll project which is in C++ (and full of windows only stuff) and now I'm trying to use NDK to compile it and JNI with Android studio to use the native functions. Unfortunately almost every step has it's own quirks and problems and it's far from smooth :/ I wouldn't recommend it to beginners like me!
I know this answer is late, but I'm in a similar situation right now.
This project here helped me setup a native Android Studio build with an embedded C# lib:
https://github.com/royd/KotlinAppWithXamarinDependency
The only issue I'm facing right now, that I can't get the R8 obfuscation/optimization to work without breaking the app.

Problems referencing Facebook SDK C# WP8 from C++ Windows Phone 8 app

I try to reference facebook.dll - wp8 branch - within an app of type "Windows Phone Direct3D App (Native only)" project under Visual C++ VS2012, and I get the error:
a reference to [dllname] cannot be added because the two projects
target different runtimes.
The restriction is that I cannot change the architecture of the application, so the main entry-point must be in the C++ project, and from here to call somehow the code written in C# for facebook.
Based on http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714080(v=vs.105).aspx , I tried also to reference the C# dll indirectly through a Visual C++ "Windows Phone Runtime Component" but again I get the same error. I tried to change some project settings and nothing worked. Trying to consume C# dll in C++, in all samples and demos which I found on the web, for wp8, the main entry-point is always in C#. Only some hints regarding windows store apps seem to indicate that could be possible to consume C# from C++, but I don't find any reference saying explicitly that my scenario on wp8 is possible or impossible.
How can I use facebook SDK from my WP8 app? Or what options I have?
Thanks in advance to anybody which shares the solution or any suggestion
C++ projects can't just invoke any C# DLL in WP8. You'll have to change how the class is built. Start a new C# XAML + C++ D3D project and have a look at IDrawingSurfaceManipulationHandler and Direct3DInterop. Basically you'll have to recreate and recompile the Facebook C# SDK to use it in WP8 C++.
However, installing and using the Facebook SDK on WP8 C# projects works OK on my machine. Have you considered usign a mixed C# and C++ project? It does have a small perf hit, but as long as you use DrawingSurfaceBackgroundGrid you should still have relatively good perf.

Could not add reference to WinRT C++ DLL project

THE SCENARIO:
I am making a [edit: native WinRT] C++ Direct X DLL project for Windows Store apps based on the "Direct3D App" Visual Studio code sample.
This must be a separate DLL project from my Windows Store App project because I will be using my XAML UserControl with Direct X renderer inside it both in the Windows 8 Metro app and a Windows Phone version of the app.
THE PROBLEM:
I cannot reference the C++ project in the C# Windows Store app project. Visual Studio refuses with the following words:
A reference to <project-name> could not be added.
I've seen suggestions to compile the C++ project using the /clr flag, but this is not compatible with:
the /ZW flag required by some API within the project
the "wrl.h" header
a multitude of other command-line flags in the project settings
So the /clr flag does not appear to be an option in this situation.
THE QUESTION:
How can I reference the C++ project so that I can use my XAML Direct X renderer within the C# Windows Store App project? Or is there a better alternative?
Thanks in advance.
I solved the problem by re-creating the C++ project as a Windows Runtime Component project. I had originally created the project from the Class Library (Windows Store apps) template. It must be that the Win RT template has some configuration settings which enable it to work.
After making this change, the project could be added as a reference in the C# project.
It is because of #user1610015's answer that I realised I was actually developing native Win RT code, not managed code, which lead me to try this solution - thanks!
The /ZW flag is for C++/CX projects. C++/CX is almost identical in syntax to managed C++ (C++/CLI), except that C++/CX is completely unmanaged. This may be what made you think you were developing managed C++, when in fact you weren't.
I haven't used C++/CX, but from what I've heard, Windows Runtime classes are COM classes. So I think you can use the TlbImp.exe tool from the Windows SDK to create a managed DLL from your unmanaged DLL, which you can then reference from your C# project. Try it and let me know if it works.
Another option is to talk to the Windows Runtime without C++/CX. While C++/CX makes Windows Runtime programming a lot easier, it's not strictly necessary. You can also use the WRL library. Since the WRL library is pure C++, it should also be usable from C++/CLI.

How to organize Windows Phone code base to target both 7.x and 8 platforms

I took over a Windows Phone project which was previously targeting WP 7.1 platform, and with the recent announcement of the new platform it should target WP 8 as well.
My VS 2010 solution consists on several projects (Data access, Model, Tests and WP7 client app) and i am wandering on how to include support for WP8.
I have to note that the code-base is not compatible with WP8, due to usage of Toolkit controls and other 3rd party libraries targeted for WP7.1 specifically.
Also there is another problem with the Visual Studio versions - WP7.1 can work with VS 2010, but WP8 requires VS 2012. Should i move the whole code-base to VS 2012?
Any good advice on how to organize code-base in a most meaningful way in order to avoid duplication and possible painful maintenance?
I am thinking between one solution - multiple projects vs. multiple solutions - reusable projects approach. Code duplication (like two separate folders/solutions) should be the least possible approach (fallback).
The best thing to do is for you to upgrade all the projects to VS2012 in order to be able to support both WP versions (you'll still be able to open the WP7 project in VS2010 despite it being a VS2012 solution, 'cause it's backward compatible).
Regarding the best way to organize the code base, in my opinion you should create a Portable Library that supports both WP7 and WP8 and get all the common code base there (probably mainly the business logic, specially good if you're using a pattern such as MVVM). Not forgetting about the controls issue, you should probably have some different controls for WP8 'cause the screen sizes are not the same and you could get some more info available and use different controls. If you still want to use the same XAML code, have just one file in one of the projects and add that same file "As Link" in the other project you want to re-use it.
Finally, you can, and also probably should, use some conditional code compilation by making use of the #if directive.
Hope that this information helps, and if any code sample is needed, just say so and I'll try to gather something up.

Categories