Use of different using statements using #if conditionals with two UWP projects - c#

I am currently working on a system with multiple target platforms.
In my solution are two UWP platforms that share a Shared-Project. Now I have to tell both of these UWP projects which using statements they are supposed to load.
I am thinking about using #if conditionals in the Shared-Project like this for example:
#if UPW_Project_Client
using some.namespace.client.a;
using some.namespace.client.b;
#endif
#if UPW_Project_Server
using some.namespace.server.a;
using some.namespace.server.b;
#endif
Yet I did not find a suitable solution and would appreciate any help to get this problem fixed.

I can't say if this is the best way to solve what you are trying to do, but you can do this the way you want to with conditional compilation symbols.
If you have this in your shared project
#if UWP_Project_Server
using some.namespace.client.a;
using some.namespace.client.b;
#endif
#if UWP_Project_Server
using some.namespace.server.a;
using some.namespace.server.b;
#endif
Then in your server project, right click -> properties, then go to the build tab. Make sure the configuration is set to All Configurations. In the "Conditional Compliation Symbols" fields, just append UWP_PROJECT_SERVER. Needs a semicolon per symbol too.
Do the same for the client project but with UWP_PROJECT_CLIENT.
You can toggle the context of the shared project when viewing the files. Use the combo box near the top of the editor.
Client context
Server context
This is just my test project, ProjectSpecificImplementation is defined in both my Client and Server UWP projects.

Related

Custom conditional compilation symbols

I'm currently struggling using custom configurations.
My solution has one .NET Standard Library and two other Projects (one for Windows, one for Android) which uses the library.
What I try to do is giving the library the compiler constant WINDOWS and MOBILE.
Thats how I tried it several times:
Create two new configurations WindowsDE and MobileDE, copy settings from Debug configuration and create new project configuration. At some trys I also deleted the default Debug configuration but that didn't helped
Properties of the library -> Build, choose WindowsDE and put WINDOWS into Conditional compilation symbols field then choose MobileDE and put ANDROID in it.
I'm testing it with calling a method in the library :
#if WINDOWS
System.Diagnostics.Debug.WriteLine("Windows");
#endif
#if ANDROID
System.Diagnostics.Debug.WriteLine("Android");
#endif
But that doesn't work at all. Also just using
System.Diagnostics.Debug.WriteLine("anything");
without having any #if does not print and at some trys I could not even debug the library anymore.
Would appreciate any help on this
You can define conditional compiling constants in the project properties
#if WINDOWS
System.Diagnostics.Debug.WriteLine("Windows"); // NOT printed!
#endif
#if ANDROID
System.Diagnostics.Debug.WriteLine("Android"); // Printed!
#endif
You can enter several symbols separated by semicolons. Don't set them true or false. The ones that are listed are true. The missing ones are automatically false.
If neither the one nor the other prints, then possibly the solution does not compile and you are running an old code. Try to rebuild the solution.

How to create multiple App using one project

How to create multiple executable file/uwp app by using one project?
I have a program which have two version, one is Full Version, one is Lite Version.
But I have only one Project which contain all the code, the only different of full and lite can be adjust in my config, when the config changed, it will turn into lite version with less feature. I need to produce full.exe and lite.exe using only one code based (one project)
full.exe
config file will have "full=1".
lite.exe
config file will not contain this info.
Same for UWP, How to make two app using only one UWP project?
You can use Conditional Compilation and Preprocessor Directives.
First open Build/Configuration Manager and add new configuration. Call it "Full"
In project properties in Build tab choose this configuration and add to conditional configuration symbols some word. Wor example "Full".
Now you can wrap code that is used in light or in full versions:
#If FULL
// Insert code to be compiled for full version
#End If
update:
If you need trial versions you can use LicenseInformation class and ProductLicenses. Nice article about it:
Test In-App Purchases in Windows Universal Apps (UWP)

Removing the code with preprocessor in .net

I have .net C# application. In the application have two set of code for different client.
We were thinking of removing the part of code through preprocessor. Diabling the part with config file parameter is not an option for us.
We want simple setup like:
#define DEBUG
//....
#if DEBUG
Console.WriteLine("Debug version");
#endif
The only issue is, our part of code is distributed into multiple files and multiple projects in the solution.
So we want to define globally the preprocessor “DEBUG” at one place. Preferably in project property or something.
What is the best option for us?
Look for "Conditional Compilation Symbols" on the "Build" page of the project property dialog. You can set it per-build configuration.

Check using preprocessor if app is a winforms or asp.net

Is this possible to check in assembly what client (winforms app or asp.net page) is running it?
I want to add some methods but only for specific client.
I know, there is predefined DEBUG (#if DEBUG). Where can I find full list, what can I check using preprocessor?
To expand on m0sa's answer, preprocessor directives are basically just a string passed to the compiler.
If you are so inclined, you can add new build configurations (example: instead of Debug/AnyCPU and Release/AnyCPU, you could make WebDebug/AnyCPU, WinformsDebug/AnyCPU, WebRelease/AnyCPU, etc).
Then in the properties page of your project, for each configuration you could provide a value in the 'Conditional compilation symbols' field. For example, for WebDebug and WebRelease, you could provide the conditional symbol WEB. Then, you would be able to use:
#if WEB
using MyNamespace.WebStuff;
#endif
You will need multiple build configurations for that and define different a preprocessor directive for each one. You can set the conditional preprocessor directives in the Build tab of the project Properties page.There are no other directives defined, just the DEBUG directive which you can turn on and off (together with the TRACE directive) in the same tab. Note that DEBUG it is not defined for the "release" build configuration. This is kind of what you will need to do to enable different versions of the assembly to be built.
References:
MSDN
www.davidarno.org <-- see this one for a more visual description

VS2008: Multiple build configs with defines don't work as expected

I have a base library to maintain in multiple versions. I do a SVN switch whenever I need to work on another version.
I don't have multiple versions of my test application solution, so I thought that for different versions I could do multiple solution / project configurations that define symbols for the version to be able to have version-specific code in my test.
Currently I have the following build configurations in the test application solution: Debug, Release, DebugV10, ReleaseV10, DebugV15, ReleaseV15. In the *V10 and *V15 configs, I created and selected corresponding *V10 and *V15 PROJECT configurations for the two projects that have version-specific test code (not for all projects, most run normal Debug / Release configuration in the solution -Vx configuration).
In those project configurations I entered the corresponding conditional compilation symbols (VERSION10 and VERSION15).
Now in my code in the project I go like
#if VERSION10
// do v1.0 stuff
#elif VERSION15
// do v1.5 stuff
#else
// do trunk stuff
#endif
But apparently VS doesn't recognize the symbols. Even a simple #if DEBUG does not work anymore, allthoug define DERBUG constant is checked in all Debug* project configurations.
Is this a known thing? What can I do about it?
The concept that you describe sounds fine.
That is, the code uses:
#if VERSION10 ... #endif
and the Project configuration defines VERSION10
and a Solution configuration is set up that is set to use the above Project configuration.
As long as these are all set up correctly, I'd expect it to work.
The fact that you say even #if DEBUG isn't working as expected suggests that something is very broken.
I'd suggest you try the simplest possible case, to ensure you undersatand how to set it up: Create a new minimal "hello world" application that just has simple code that allows you to tell apart the versions:
#if VERSION1
Console.WriteLine("Hello from version 1");
#else
Console.WriteLine("Hello from version 2");
#endif
Then create project configurations ("Debug Version 1", "Debug version 2") and see if you can set them up (one with VERSION1 defined, one without) to get the two outputs when built.
Then add Solution configurations that use the above Project configurations, and build them to check that they print the right things when executed.
Once you've done this you should have a working system (an understanding of how these elements relate to each other) that you can apply back to your more complex project. To apply them back, I suggest deleting most of the project/solution configurations and then rebuilding them from scratch now that you are sure you know what to do - often rebuilding these things from first principles works out better than trying to tweak an existing 'broken' setup back into life.
Conditional compilation in C# is different from C and C++. See here for info on the ConditionalAttribute class.
For example:
doDebugOutput(); // unconditionally call the optional code
Conditional["DEBUG"]
void DoDebugOutput()
{
// do expensive debug-only output here
}

Categories