Does using MediaLibrary class pose portability problems? - c#

In my Windows Phone project for SDK 7.1 (maybe I need to update it to use WP8 later) I want to add a test image into Windows Phone Emulator using this code. The latter uses MediaLibrary class and so I have to add a reference to Microsoft.Xna.Framework. No other code in my project needs that assembly.
I've found this MSDN document that says You can use certain XNA Framework assemblies in apps that target Windows Phone OS 8.0. and the list includes Microsoft.Xna.Framework.dll. So looks like I should not expect any problems but I'm not completely sure.
Will using MediaLibrary class and so having a reference to Microsoft.Xna.Framework cause any issues with portability of my code and its distribution?

No you can safely use MediaLibrary and other permitted Xna assemblies in your WP8 projects. In fact, The Rich media extensibility which adds certain extension methods to some classes in the Xna framework are exclusively available for WP8 only.

Related

In Xamarin, does using a Portable Class Library mean you do not write platform specific code?

My understanding is the compiler directives in a Share Access Project (SAP) make use of platform specific code.
For SAP Xamarin has,
Application projects can include platform-specific references that the
shared code can utilize (such as using Community.CsharpSqlite.WP7 in
the Tasky sample for Windows Phone).
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/sharing_code_options/#Shared_Projects
I didn't see anything on this page for PCL about not writing platform specific code. If I include the Xamarin Android library in the PCL, is that not specific code that would be in a SAP?
Does it mean that I could tweak or have some feature on Android and Windows Phone that I couldn't on iOS (maybe iOS didn't support it?)
EDIT: What does it look like to access the camera, for example, from PCL via DI, vs accessing the camera in a SAP (platform doesn't matter; Android is fine.)

The type or namespace name 'IPAddress' could not be found

I'm writing a Xamarin.Forms application and have added a PCL project into my main solution in Visual Studio 2013. I'm having problems with using several standard .net classes. Here's a list:
System.Net.IPAddress
System.Net.IPEndPoint
System.Net.WebClient
System.Net.Sockets.UdpClient
System.Timers.Timer
System.Timers.ElapsedEventArgs
System.Net.NetworkInformation.UnicastIPAddressInformation
My question is: How do I figure out which of the above are compatable with a Xamarin.Forms app, and how do I get them to work in my project/solution?
I've contacted Xamarin support, but get vague help and not specifics...
Does anybody have a good strategy for figuring out what classes are supported and what .dlls are needed in order to use them with a Xamarin.Forms project?
Thanks a bunch for your help.
PS: I've been given the following sites for reference, but they don't seem to have any concise information:
What is supported
NuGet package info
It sounds like you don't understand what Portable Class Libraries actually are. When you create a PCL, the subset of the .Net Framework classes that are available for you to use depends on what profile you choose. A profile are the platforms your PCL can support. In Xamarin, the default profile is Profile 78. Profile 78 supports .NET 4.5, WP8, Win8, WP Silverlight 8, Win Store 8, Xamarin.Android, Xamarin.iOS. So the classes that are available to you are the classes that are platform independent, that run on ALL platforms in profile 78.
Example: Take a look at the implementation of System.Net.IPAddress. The implementation makes use of Sockets. Upon further inspection of this implementation, you can see that that System.Net.Sockets is nothing more than a .NET wrapper around the WinSock API. The WinSock API is a Windows only DLL that is specific to the Windows operating family (ws2_32.dll). So, you can now see that System.Net.IPAddress are high level abstractions of Windows Socks (built for the Windows Platform). Since the ws2_32.dll does not exist in Android, nor iOS, etc, this API won't be available to you because Profile 78 specifies that we need to support other non-windows platforms.
Another example is System.Drawing.Bitmap. That's just a .NET wrapper around GDI+, which is based on the Win32 API. Of course Win32 is not available on non-windows platforms (even Windows Phone), which is why you won't see it in a PCL.
Now with that being said, you may think that PCLs are restrictive, but you can create a good PCL library if you think about them the right way. You can also use patterns like Dependency Injection and Bait and Switch.
So in short, don't count on Microsoft "adding support for all the base class libraries" because that's not what PCLs are.
You can still use System.Net.IpAddress in your Xamarin.Forms PCL by using DependancyService. Make an interface in PCL and declare a method "GetIpAddress" in it. Then , in android /iOS project, you can implement that interface in platform specific project and easily access System.Net.IpAddress. After that, Call DependancyService from PCL project to get the result which is IPAddress of your desire.
Cheers !!!
After some additional e-mail from Xamarin Support, and comments on my additional posting, it appears that MS is quite behind in supporting all of their classes via PCL. Some are supported, but not all by a long shot.
Anyway, in order to use my existing code, I would need to put the source .cs files that I would otherwise be using to create my new PCL project directly in the .iOS project and in the .droid project and recompile that way.
This basically defeats the advantage of using a PCL, but does provide an alternative that allows me to use most of my code unchanged. It would just be nicer if all the xamarin libraries were available as PCLs.. Oh well, eventually MS will catch up and get all of their PCLs up to date so that we can use our custom PCLs more often, instead of having to duplicate code all over the place. (Maintenance Nightmare)...
Anyway, thanks to all for the help.
(Working on the bleeding edge of technology is sometimes painful... But that's why we make the big bucks !!!)

Load a not SIlverlight dll into a Silverlight project type

I hope isn't a stupid question...
Is possible to load a no silverlight assembly into a silverlight project without using a COM object?
Thanks in advance.
You can use the Portable Class Library (PCL)
Use this project to write and build portable assemblies that work
without modification on multiple platforms, such as Windows 7, Windows
8, Silverlight, Windows Phone, and Xbox 360. For example, you can
create classes that contain shared business logic for desktop apps,
Windows Store apps, and mobile apps, and you can then reference those
classes from your apps.
The Portable Class Library project supports a subset of assemblies
from the .NET Framework, Silverlight, .NET for Windows Store apps,
Windows Phone, and Xbox 360, and provides a Visual Studio template
that you can use to build assemblies that run without modification on
these platforms. If you don't use a Portable Class Library project,
you must target a single app type, and then manually rework the class
library for other app types. With the Portable Class Library project,
you can reduce the time and costs of developing and testing code by
building portable assemblies that are shared across apps for different
devices.
Also, Microsoft's Scott Hanselman has written a few articles about the PCL, this one should be a good starting point to find the resources you need to get started: Cross-Platform Portable Class Libraries with .NET are Happening

Google APIs Client Library for .NET on Windows Phone 7 Project

Is it possible to use Google APIs Client Library for .NET in a WP7 project?
I've tried to add a new reference to DotNetOpenAuth.dll and I'm getting the folowing error.
Reference cannot be added to DotNetOpenAuth.dll because it was not built using the Windows Phone runtime. Windows Phone projects will work only with Windows Phone assemblies.
Is it possible to solve?
Try Portable Library Tools from MSDN. Read about them here. Now reference Google .Net library in the portable library, also reference this portable library project in WP7 project. Now, write a small wrapper over the Google apis in the portable library project and use them from your WP7 project. Referencing works but I did not use it, you can give it a try once.
I would think it would be possible to use the Google APIs from WP7. I've not used them myself, but it looks like you use them by making HTTP requests which, if memory serves me right, is possible on WP7.
With regards to the DotNetAuth.dll it is more then likely built against the Full or Client Profile version of the .NET Framework and therefore can't be used by WP7. In order to do this, then a WP7 specific version of the assembly will be have to be compiled.

I want to use a regular class library on XBox360!

why can't I use regular class libraries in XBox360 games?
I have application logic which I want to keep independent from XNA and use in both WPF and XNA applications.
Does anyone know good practice to share code between XBox/Phone7 applications and "regular" windows applications?
Have a look at Portable Class Libraries: http://msdn.microsoft.com/en-us/library/gg597391.aspx
Using the Portable Class Library
project, you can build portable
assemblies that work without
modification on the .NET Framework,
Silverlight, Windows Phone 7, or XNA
(Xbox) platforms. Without the Portable
Class Library project, you must target
a single platform and then manually
rework the class library for other
platforms. The Portable Class Library
project supports a subset of
assemblies from these platforms, and
provides a Visual Studio template that
makes it possible to build assemblies
that run without modification on these
platforms.
Edit: Check out JoDG's answer for a nicer solution.
You could try linked files...
In your Xbox project, right-click -> Add Existing File, then after selecting the file(s), click the little drop-down arrow on the "Add" button and click "Add As Link", as shown here:
This might not work if you need to share more than just files, but it's an easy way to share code.
JoDG's answer is probably the most practically useful. But just to provide additional information:
why can't I use regular class libraries in XBox360 games?
Because the different XNA platforms target different versions of the .NET framework.
XNA Game Studio itself provides a mechanism (documented here and more info here) for automatically mirroring the source files of a project between two projects. This mechanism can be used for creating copies of your library projects for each platform, as well as for your game projects.
For XNA-related work, this is the preferred method for creating cross-platform libraries. When you have to also make your library work on WPF you have to take additional steps:
On Windows, an XNA game is just like any other .NET application, and a XNA library for Windows is just like any other .NET library. Except for the fact that they reference XNA assemblies. So your WPF application can reference a Windows version of your XNA library. But if you want your application to work on systems without XNA installed, you need to remove the XNA assembly references from the Windows project for that XNA library.
XNA Game Studio will still mirror changes between the Windows library and the Xbox 360 library for you.

Categories