How can I reference Windows.System namespace in a Xamarin project? - c#

I am trying to open a pdf file on the browser, acrobat or any other default pdf program. For this I need to call LaunchFileAsync() and I need to reference Windows.System to be able to use it. However, this is not working and I can't call any reference.
I tried looking for a specific library or nuget package but I couldn't find anything. The documentation just tells me to use that namespace but it doesn't say anything about how to add the reference. I also tried using a different approach by calling System.Diagnostics.Process.Start(path) and some variations but it doesnt't work using Xamarin apparently.
This is basically what I am trying to implement but there's no namespace found event though I did try implementing it.
string uriToLaunch = #"file://" + #file;
var uri = new Uri(uriToLaunch);
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
Any help would be appreciated.

Have you tried adding an assembly reference in the Reference menu?
In the Solution Explorer, right click your project and go to:
Add -> Reference -> Assemblies -> System.*
Also, make sure not to use platform specific libraries for a mobile project, since this will probably cause compatibility issues. .NET Standard is the way to go.

Use Xamarin.Essentials as it defines the various OS dependancies for you and on UWP uses the native Windows.System.Launcher.LaunchUriAsync:
Xamarin.Essentials.Launcher.OpenAsync
re: Xamarin Docs
Note: Opening file://-based URIs is very restricted in UWP.

If you are not targeting .NET Standard yet, make sure you do that if you want to use System libraries. There are some tutorials on this, but i highly suggest you backup your project before changing from PCL to .NET Standard

Related

Self-built C# Library not found

I have an issue including a self-built library to a C#-project. I have created an own class library called ClassLibrary1 just to learn how to add libraries on Visual Studio 2019.
So I have written some simple code in a newly created .NET-class library project and have clicked on "create new solution" (directly translated from my german IDE-language. Maybe it's called slightly different) after writing the code. Back in the C#-project, I have selected the dll-file from bin/Debug/ of the class library's project folder.
After I have set the checkmark, the dll-file was shown in the solution-explorer under Assemblys like expected. But the issue I now have is that I still cannot use the ClassLibrary1.dll-file in the cs-file in this very project as I expected via the command "using ClassLibrary1;". It only shows me the error message "type- or namespacename "ClassLibrary1" not found" when trying to compile the C#-project and I don't get, why this is the case.
It seems like it has to be a very obvious problem but after some research on the internet and trying some things by myself still nothing has changed.
Thanks in advance for helpful replies.
The by far easiest way to manage a library is to use project references. Ensure that your library and the project that uses the library is in the same solution. Then right click the "references" and select "add Reference", go to the project tab and add a checkbox for the library. Read more about managing references.
You might also need to add namespaces for the classes you wish to use in the source files.
I would not recommend managing using file-references to lose dll-files, since it can easily become a hassle to manage. I.e. if you create a new version of the library you would need to build, and explicitly replace this file in all other projects and update all the references.
If you want to share libraries between multiple solutions the more popular solution would be to setup a nuget server. This solves some of the updating problems by maintaining multiple versions of the same library, and provides a nice interface to update references in all projects. But this is a somewhat more complicated solution, so I would not recommend this for new developers.

ApplicationData using in Console with UWP parameters

I'm trying to follow UWP with Desktop Extension – Part 2 of UWP and WinForms desktop-bridge calling the processes and passing parameters. This example Console Program.cs code includes parameters string:
string parameters = ApplicationData.Current.LocalSettings.Values["parameters"] as string;
But the name ApplicationData does not exists in the current context, I'm trying to find out, if I've missed some reference or it is different version of C#
I'm not sure even if it is what it requires, but adding of reference Windows.Foundation.UniversalApiContract.windmd throws another error with Values:
Error CS0012 The type 'IPropertySet' is defined in an assembly that is
not referenced. You must add a reference to assembly
'Windows.Foundation.FoundationContract
#Nico Zhu - MSFT is correct, but I would also suggest alternative approach, which may make your life easier in the long run - the UWP APIs for desktop apps are now also distributed via NuGet as a package which takes care of referencing the right libraries for you.
It is in preview at the time of writing, so you can install as follows:
Install-Package Microsoft.Windows.SDK.Contracts -Version 10.0.18362.2002-preview
See the NuGet page for more info.
If you want to use ApplicationData class, please add Windows.winmd where in the C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd. Then add Windows.Storage namespace.
Detail steps
Right click project References -> Add References -> Browse(file type all file)-> select Windows.winmd

"System.Runtime.Serialization'. Are you missing an assembly reference?" after adding web reference

I have an iOS/Android Xamarin project that share a PCL project.
I have added a web reference to the PCL via ASMX as per Xamarin's Tutorial. The tutorial doesn't say how to get to the "Add Web Reference" window, but we found it by right-clicking our PCL project and going to Add->Web Reference.
On adding the web reference I get a roughly 500 line Reference.cs file that is added to the PCL.
However, on compilation, either iOS or Android, I am given several of these errors:
Reference.cs(74,74): Error CS0234: The type or namespace name IExtensibleDataObject' does not exist in the namespaceSystem.Runtime.Serialization'. Are you missing an assembly reference? (CS0234) (MyProject)
It is clear that I need to add the System.Runtime.Serialization assembly reference. However I cannot figure out how to do this and I have spent hours looking for documentation or any relevant solution. Most posts questions appear tangental at best.
This forum post references this issue, but give no solution other than it was supposedly “fixed” three years ago.
Now the aforementioned tutorial references an "Add References…" dialog box for adding the System.Web.Services.dll if you use "Add files" to add the proxy. However, I cannot find this dialog anywhere in the IDE.
I did find "Edit References" when right-clicking on references. This produced a window with these tabs:
Guessing that the assembly needs to be added here I search for it, but find nothing. Apparently there is no way to add assemblies that are packaged with the IDE, you're expected to "just know" where to find them. Searching for documentation proves fruitless.
Via one of the sample project, I was able to locate the .dll file at /Library/Frameworks/Mono.framework/Versions/4.4.2/lib/mono/xbuild-frameworks/.NETPortable/v4.5/Profile/Profile78/System.Runtime.Serialization.dll and manually add it via the .NET Assembly tab.
A colleague tried adding the reference via NuGet as well.
Unfortunately the compile errors remain after attempting either approach.
Also, of note, it appears that the ToDoASMX example project nor the folder containing the System.Runtime.Serialization.dll have any references to the System.Web.Services.dll that the tutorial mentions.
I believe these docs are referring to using them in Xamarin.iOS and Xamarin.Android(At least for the .asmx docs). You can get to System.Runtime.Serializationvia right clicking References in your Xamarin.iOS/ Xamarin.Android project, Going to Edit References, then going to the All tab which it should be listed under the BCL (Base Class LIbrary). You may need to go the route of using an Interface based pattern within your PCL to call the respective native web service instead. You could also try wrapping your .asmx into a REST service and using HttpClient as well.
Seeing this sample project(https://developer.xamarin.com/samples/xamarin-forms/WebServices/TodoASMX/) shows the Interface based pattern described above using ISoapService in the PCL and implementing in the native projects. You will see each project (Droid/iOS) have the Web Reference in them. The PCL is purely invoking the code.
It's more of a limitation of using .asmx given the framework built around them. Using WCF or REST have better options for consuming directly from a PCL. However note that PCLs are going to be replaced by NetStandard library flavor.

Error while attempting to use "using com.google.zxing;" to decode Data Matrix

I started off from the thread to [decode data matrix in C#] (How To Use ZXing C# Port)! but I am encountering an error that prevents me from using zxing.
error states: "The type or namespace name 'com' could not be found (are you missing a using directive or an assembly reference?)"
In my code i am (trying to) use:
using com.google.zxing;
Please let me know what I am doing wrong.
It'd be nice if C# could automatically detect the references you're using and download them, but this is not the case. You need to download the ZXing SDK and reference it before using it.
The easiest way to do so is via NuGet, which is built in Visual Studio from 2012 onwards, and available for VS 2010. Right-click on your project, click on "Manage NuGet Packages", then select "Online" from the left-hand menu and search with the search box in the top right.
There are two ZXing ports available on NuGet, so you'll have to look at them both and pick the one that seems right for you.
You can also find some ZXing DLL on the Internet and reference it from your project: right-click on your project's "References", click on "Add reference..." then on the "Browse" button.
However, that is not as convenient as NuGet because you need to manually check for updates to the library, and update the DLL, whereas NuGet will tell you when an update is available (it won't force you to update).
Just by looking at your code sample, I wouldn't suggest using a port that uses Java-like namespaces (a more C#-y version would be e.g. "Google.ZXing"), since it sounds like a "dumb" port that simply converted the Java syntax to C# without thinking about whether some C# features are better at doing whatever the original library wanted to do.
I've used ZXing.Net v0.14 in a Windows Phone app and it works well.
You have to replace
using com.google.zxing;
with
using ZXing;
if you are referencing and using the ZXing.Net assemblies.
I replaced the Java-stylish namespaces with a more .Net like version.
Old sample around the web doesn't work with ZXing.Net.
In the source code repository and the download section of ZXing.Net you will
find some more up-to-date samples. You should really start with the newer samples because I wrote some simplifications on top of the port.

Are there any known compatibility issues with Microsoft Media Platform: Player Framework (formerly SMF) and Silverlight 5?

I'm trying to get the default template (adaptive streaming) for the MMPPF framework to work.
Currently I'm not getting any error, but the player does not play any videos. I've tried adding other sources besides the default, I can see them added in the playlist, but they won't play either.
Using Fiddler I can see that the player does not even attempt to retrieve the manifest (or anything else).
The online example here works fine, I've even tried using that manifest as a source, but it still won't play when I run it in Visual studio.
So my question, could this have anything to do with having Silverlight 5 toolkit installed? I would assume that the MMPPF/SMF template is supposed to work out of the box (which is also stated on the Codeplex site). Or is there something else I'm missing?
You may need to add some references to your project. MMPPF is using MEF to resolve plugins at run-time, and it can fail silently if it doesn't find a suitable plugin.
For Smooth Streaming, you need both Microsoft.SilverlightMediaFramework.Plugins.SmoothStreaming and Microsoft.Web.Media.SmoothStreaming for it to work for instance.
Update:
I've encountered another reference issue when migrating a project. I had to remove and re-add System.ComponentModel.Composition, then add System.ComponentModel.Composition.Initialization.
I tried to uninstall the Smooth Streaming Client that the MMPPF client installed and installed the newest from here, added a reference to the new dll (Microsoft.Web.Media.SmoothStreaming.dll) and everything worked.

Categories