In Flex, is it posible to identify if the code is runing on Web or AIR? - apache-flex

I'm coding an app that runs both in the web and on AIR, to avoid copying code arround, I figured I should do 3 kinds of projects on flex builder: Library, Web and AIR projects.
So all my code is on the Library project.
I have accessData.as that extends EventDispatcher to fetch web services and return them as an event. I plan on using this class to also fetch SQLite data for the desktop version, but to do so I need it to decide from wich source to get the data depending on if its Web or AIR.
Anyone know how to do this?

Please refer to this link Detect AIR versus Flash Player from an actionscript library Its more detailed.

You really should have two build targets, one for Web and one for AIR. And your code should be designed in a way that the rest of the system doesnt care what the implementing part is doing, only that it conforms to a certain interface. This way, each build simply replaces the implementing code for each desired platform.

You may find something useful under System or Capabilities in the docs.

Create 2 projects Air and Standalone and create 2 conditional compilation variables for example "standalone" and "air". (more here).
Go to Project->Properties->Flex Compiler and add
For air project:
-define=CONFIG::standalone,false -define=CONFIG::air,true
and for stanalone:
-define=CONFIG::debugging,true -define=CONFIG::air,false
In your code set:
CONFIG::standalone {
trace("this code will be compiled only when air=false and standalone=true");
}
CONFIG::air {
trace("this code will be compiled only when air=true and standalone=false");
}

umm... I just found out a way
var appName:String = Application.application.name;
this works since the web version is called "" and the desktop version is called " desktop"
but if anyone has a better way please go ahead.
thanks.

Related

Why do I need to deliver DevExpress.Printing.v14.2.Core.dll

I have implemented a WPF application using DevExpress controls.
When I was finished, I optimized my references in Visual Studio (using Resharper). I have the following references left:
DevExpress.Data.v14.2.dll
DevExpress.Mvvm.v14.2.dll
DevExpress.Xpf.Core.v14.2.dll
DevExpress.Xpf.Grid.v14.2.dll
DevExpress.Xpf.Grid.v14.2.Core.dll
DevExpress.Xpf.Ribbon.v14.2.dll
When starting the application on a clean OS, it crashes. With Process Monitor, I find that it is looking in 10 different places for DevExpress.Printing.v14.2.Core.dll and cannot find it.
That DLL is 3 MB in size and I'd like to avoid to deliver it, if possible.
Dependency walker seems to not work well for .NET.
I have read DevExpress about required libraries, but that is for XtraReports, which I'm not using in my application.
Why does my application look for that DLL if it is not referenced?
Found the answer using JetBrains dotPeek:
DevExpress.Xpf.Core.v14.2.dll and DevExpress.Xpf.Grid.v14.2.dll both have a reference to DevExpress.Printing.v14.2.Core.dll.
Your application contains the DXGrid. Thus, according to the DXGrid's required Redistributable Assemblies list, the DevExpress.Printing.v14.2.Core.dll assembly contains classes that allows to implement the functionality for DXGrid's printing and exporting based on DXPrinting library.

MvvmCross MvxApplication class overriding for different platforms. (Plus, encryption)

I've got two questions here. The first one is just specific and another one is more general, but is a source of the first one.
So, my specific problem: I want to use Encryption (actually, Hashing) algorithms with using System.Security.Cryptography namespace (for instance, SHA256Managed class).
I found out that (happily) Xamarin has implemented those in System.dll.
But it is not portable and obviously can not be used from Core application directly.
But I've also found another great project -- PclContrib -- which allows you to do that. But, unfortunately, they don't have the implementation for Touch and Android. (However, that still works great for Desktop (Web) and Windows Phone, plus, still can be included into Core (as it uses portable project)).
Anyway, to solve that nicely, I've decided to create some base class for the encryption methods and then override core methods which require the custom dll (for any custom system).
The way I did it (at least, trying to do) was:
Defining virtual method in Core App base class:
public virtual IEncryptionProvider CreateEncryptionProvider()
Overriding Core App class in Touch project with overriding CreateEncryptionProvider (which creates an instance of TouchEncryptionProvider class instance).
Core:
public class App : MvxApplication
Touch:
public class AppTouch : App
Launching it in Touch setup.cs:
protected override Cirrious.MvvmCross.ViewModels.IMvxApplication CreateApp (
{
return new AppTouch();
}
But, that does not work for me. On startup I've got this exception message in log:
"Exception masked KeyNotFoundException: Could not find view for Mynamespace.Etc.LoginViewModel", which works fine when I do new App() instead. I am not sure if that message shows actual problem (as before it was saying the same even that was a problem with some third-party dll, unrelated to views at all). But speaking shortly, that's just a primitive inheritance of App : MvxApplication, but placed not in Core but Touch project.
So, does it requeire some more custom initialization for such situations or do I miss something else?
And, actually, more general question is how should I build such Multiplatform approaches? Actually, now I've got similar problem with HttpUtility.UrlEncode, which I would want to use in my Core project.
What is the MvvmCross "philosophy" to handle such situations?
Thank you.
For the 'viewmodel not found' problem, this is caused because mvvmcross by default only looks for viewmodels in the Assembly containing your app.
If you want it to look in other assemblies, override ViewModelAssemblies in Setup.cs - see how this done in, for example, MvvmCross - structuring shared View Models and Views
For general multplatform approach, please read questions and answers like:
Platform-specific IoC in MVVMCross
Instantiation of ViewModels and Service classes
Please also remember you don't have to use PCLs - if you prefer to use file-linking between multiple platform-specific core projects, then you can of course use this approach.
Finally, please also try to ask one question per question - I find it makes stackoverflow work better for users and with search engines too. If you need to link questions, then you can just add a hyperlink reference - stackoverflow then marks them as related.

Sharing code between Flex and AIR

As you know, we could build a RIA application based on flex. Also, we could build an desktop application based on AIR. I have a question, If we want to build web & desktop application simultaneously. Could we use the same codes to ship our production to web & desktop?
If you design your application for it, you should have no problems in sharing 99% of your codebase between your Flex and AIR builds.
You will need a separate application MXML for the Flex / AIR versions as AIR uses a WindowedApplication and Flex uses Application
You will need to abstract your usage of any AIR-only APIs. That is, any class, property or method marked with the AIR-only icon (
) in the Online Documentation. You might find this process easier if you are using a Dependency Injection container like Swift Suspenders.
Alternatively, you can split your service definitions into two different source trees. This would result in your AIR project and Flex project sharing one source path, but also having their own source path. This way, code that accesses com.application.MyService would be shared across AIR and Flex but the implementation of com.application.MyService would differ depending on which 'service source path' was being used.
You may find it useful to configure each build with a compiler flag like -define+=CONFIG::AIR. This allows you to use conditional compilation so that you can compile the same file for both builds, but include specific code for the AIR build.
Here is an exmaple of conditional compilation:
public function getMyService() : IMyService
{
CONIFG::AIR
{
return new MyServiceThatUsesAnAIROnlyAPI();
}
return new FallbackServiceForFlex();
}
Unfortunately there is no way to 'negate' a conditional flag (ie. !CONFIG::AIR) so you either need to be smart about your usage of it, or include two flags (CONFIG::AIR and CONFIG::FLEX)
I'm surprised no one said it yet, but this is how I would do it:
Create a library project. this project will include all your
shared code.
Create a Flex project for web deployment
Create an AIR Project for AIR deployment
Both the Flex and AIR projects can reference and use code in the library project. The AIR project can use AIR specific functionality without affecting the web project.
If you need to perform different actions differently based on whether using the web project or the AIR project, you can create interfaces in the library project and implement them in the main project to use the respective APIs.
yes, you can do it.
there are some conditions you have to control in code.
Keep in mind, if the application is also a flex app, then it will be a single window app.
for every project I make needing this I create 3 projects
code base (the main control is a group or a canvas)
flex exporter => when you build this you will end up with a flex application
it has a control from #1 inside the main application
air exporter => when you build this you will end up with an Air app
it has a control from #1 inside the main window.

Flex WSDL to Actionscript as ant task?

Flex Builder 3 provides support to generate actionscipt from WSDL via the GUI ( Data->Import Web Service (WSDL) ) - but this sort of method requires that you check in the generated source. This is not desirable to us (we understand both sides of the 'should generated source be checked in' and we have decided that they should not) so we would like a method to generate the actionscript classes from an ant task. In this case, the WSDL would live in the file system.
Any ideas?
You could spent some time digging through Flex Builder's JARs to find the libraries they're using to do this, then invoke them from a very thin custom Ant Task you write yourself. The likelihood of this succeeding is small but might be worth investigating to save yourself a ton of work, just in case.
Short of that, I'd start with WSDL2Java to generate Java classes that represent your WSDL entities. The results won't necessarily be beautiful but you should get classes that adhere to the JavaBean spec. Then you could use one of the open source Java-to-ActionScript generators which include:
Granite Data Services' Gas3
Spicefactory's Pimento, which has Java->AS3 generation
I'm almost positive that Gas3 has an Ant Task you can use; not sure about Pimento.
From the comments in the generated code Flex Builder uses Apache Axis2
/**
* BaseBlaBlahService.as
* This file was auto-generated from WSDL by the Apache Axis2 generator modified by Adobe
* Any change made to this file will be overwritten when the code is re-generated.
*/
I've also found this on the Adobe Forum http://forums.adobe.com/thread/96006.
I'm also trying to solve this issue. I guess we need to create a feature request on the adobe flex website. Let me see if i can find my adobe.com user id....

Is it possible to create a 'command line' swf?

I'd like to be able to write a .swf file that is runnable as a command line app. In other words, I would be able to create actionscript classes which can interact with stdin and stdout, and could then execute that .swf directly in the command line.
I suspect that this isn't really possible. Can anyone confirm that?
EDIT:
A couple of the answers pointed out that using Flash for command line work probably isn't the best choice. I wholeheartedly agree in most situations. The reason I am asking about this is because I want to do some AS3 code generation, and reflecting on AS3 classes within the runtime would be easier than parsing the code or walking the intermediary XML that asdoc produces. I'm doing the XML approach now in Ruby, but would love to have a cleaner solution!
YES! It actually is possible.
You can create a pure AS3 AIR project (without any application window) and run from the command line using ADL (AIR Debug Launcher).
ADL will execute your SWF and will pass whatever arguments you give it directly to your application at runtime—all from the command line! To read the arguments from AS3 just add this code to your main class:
package
{
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.events.InvokeEvent;
public class CmdLine extends Sprite
{
public function CmdLine()
{
NativeApplication.nativeApplication.addEventListener(
InvokeEvent.INVOKE, onInvokeEvent);
function onInvokeEvent(invocation:InvokeEvent):void {
trace(invocation.arguments);
}
}
}
}
Your main class will still extend Sprite, but you won't see any UI unless you create NativeWindow objects. If you're using Flash Builder, just create a new AIR project and rename the extension of the main .mxml file to .as (before you finish the wizard).
Here is more about ADL: Using the AIR Debug Launcher (ADL)
Also, this will be very useful: AIR application invocation and termination
You can do all your output using trace(), write files, or even write directly to stdout, as seen here.
Apparently there is the Tamarin project which aims to create an open source implementation of AS3. This page gives a little detail of compiling an AS3 script and running it from a command line.
I'm not getting a good idea of how stable Tamarin is, but it might be your best bet for now. On the other hand, I have to strongly agree with #zenazn that you would be better off long-term learning a language more designed for general purposes, but if really want to just use Actionscript, don't let anyone stop you :)
There's no way to do this with a bare SWF right now.
However, you can publish your Flash content as an AIR app. The app can then be invoked from the command line, and you can collect the arguments from the arguments property of an InvokeEvent. The basic idea looks like this:
NativeApplication.nativeApplication.addEventListener(
InvokeEvent.INVOKE, onInvoke );
// ...
function onInvoke( e:InvokeEvent ) {
var numArguments:int = e.arguments.length;
// ...
}
Note, however, that this is essentially a one-way street. You can grab the command-line arguments, but Flash still doesn't grok the idea of stdin and stdout.
Actually, there is a project that makes it possible. RedTamarin is a project that extends AS3 (technically, the Tamarin project which is the Adobe/Mozilla ECMAScript project) to have access to low-level libraries (ie. POSIX). In its current state it appears to be good for stuff like shell-scripting-like programs which is what it sounds like what you're looking for.
Give it a try:
http://code.google.com/p/redtamarin/
You can interact with stdin, stdout and stderr with redtamarin
http://code.google.com/p/redtamarin/
see examples/docs here
http://code.google.com/p/redtamarin/wiki/System#stdout
http://code.google.com/p/redtamarin/wiki/System#stderr
http://code.google.com/p/redtamarin/wiki/System#stdin
there is a difference between Flash and ActionScript 3
Flash is a runtime, AS3 is a language
I don't see why AS3 would not be a good programming language
for the command line and/or the server side
Now, redtamarin is just that, a runtime that allow you to
run your AS3 source code on the command line.
Also, depending on your needs, you can use it in different ways
to run script on the command line
$ ./redshell myscript.as
run ABC or SWF files on the command line
$ ./redshell myscript.abc
$ ./redshell myscript.swf
run an exectuable
$ ./myscript
When you will run an AS3 script it will be dynamically interpreted,
using ASC you will be able to compile this same script to an ABC file
that can also be run from the command line.
If for example you need to assemble numerous ABC files together,
you can use swfmake to merge them into SWF file and the runtime
will run that SWF file too from the command line.
Finally, if you need to bundle everything in one executable,
you can use createprojector to take your ABC or SWF file
and merge it with the runtime itself to obtain an independent
executable.
Redtamarin provide native API that cover file system access,
sockets, operating system info, etc.
Now it is possible with AIR 2.0. Check this article to start.
If you are really that inclined, you could open a local socket, and then have a helper program, running from the command-line communicate with the open SWF.
This might be a good time to learn another language. May I suggest Java?
I had a similar question recently. It took me a few days to answer it for myself, but you can create a .swf and execute it entirely from the command line.
AS3 Filesystem Hello World
You could have a look at Haxe with is very similar to AS3 and could compile NekoVM Bytecode, which could be run on the command line.
Also interesting could be HippoHX, it is a kind of framework to create desktop applications out of flash movies. (similar to AIR, but with full access to the system.)
Nope--not possible. The best you can do is a standalone app (which can be made in Flash or with a Projector version of flash player, available from the Adobe website).
And why would you want to--Flash is awesome because of the great GUI capabilities. There are plenty of other programming languages that are much better suited for the command line (Python or Ruby or, god forbid, even Perl)

Resources