By reference to this link https://developer.xamarin.com/api/namespace/System.Security.Cryptography/
May I know how do I include this in my Xamarim.Forms PCL project? When I include in, Visual Studio is giving error as the picture below
May I know if anybody has any idea how to solve this? Thanks.
The namespace is available both in Xamarin.iOS and Xamarin.Android. You could make platform specific implementations for both platforms and then resolve them with the DependencyService
You would have your interface for whatever you need in your PCL
public interface ICryptoService // or whatever
{
string Cipher(string stringToCipher);
string Decipher(string stringToDecipher);
}
and then implement these in your platform specific projects
using System.Security.Cryptography;
namespace MyApp.Droid
{
public class CryptoService : ICryptoService
{
// implement interface
}
}
To make the implementation visible to DependencyService you have to use the DependencyAttribute
[assembly: Xamarin.Forms.Dependency(typeof(MyApp.Droid.CryptoService)]
You can now obtain an instance in your PCL with
var cryptoService = DependencyService.Get<ICryptoService>();
and then use it. The steps for iOS are basically the same. For UWP you have to register the implementation manually, see here.
Edit:
Since it's likely that the implementation will be the same for all platforms, you could introduce a shared project and put the implementation there. All you have to do now is referencing the shared project from your iOS and Android projects.
Edit 2:
Adding a shared project to an existing Xamarin.Forms solution is quite easy. Just right-click your solution in VS, choose Add -> New Project... (I only have a german localized VS at hand at the moment, but it should be something in the lines of that). Now select Shared Project, give it a name and click OK, there will be a new shared project in your solution.
Now right-click your platform specific project and choose Add -> Reference.... The window to add a reference should open and on the left you can choose the source of the reference (Assemblys, Projects, Shared Projects, COM and Browse). Select Shared Project and then the project you just created. Any code file in your shared project will now be compiled with your platform specific project as if the code file was in the platform specific project (watch for namespaces!). Repeat for the other platform specific projects.
Related
I am developing UWP app in xamarin. The application works on IOS, Mac , Android, Windows. I have created UWP project in it according to the tutorial given in developer.xamarin.com. But it giving error saying Accessibility.App namespace not found.
Here is my code:
namespace Accessibility.UWP
{
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();
this.LoadApplication(new Accessibility.App());
}
}
}
According to your description, the app can't see Accessibility.App. As you claim that Android and iOS projects work there are two things that can cause the problem:
you don't have the reference to your Shared/PCL project in your UWP project (most likely).
you have possibly changed the namespace / class name in the Shared/PCL project to something else than Accessibility.App
This is usually caused by missing reference to the shared project or class library where Xamarin.Forms App class resides. From the description this project should be called Accessibility.
Right-click the UWP project select Add, Reference... then in Solution tab select the Accessibility project.
Also it might happen that the UWP project didn't pick up on the reference, so restarting Visual Studio might help as well.
If all fails, you can try to use class name binding with using. On top of the source code file add:
using FormsApp = Accessibility.App;
And then in code use:
this.LoadApplication(new FormsApp());
I'm trying to change the container to DryIOC of a Xamarin.Forms + Prism app.
Visual studio is compiling and starting the app without errors, but when the app starts it doesn't fire App.OnInitialized method keeping the app on a blank screen.
What did I do?
Removed Prism.Unity, Unity and Microsoft related packages from the Android and Shared projects
Added DryIoc.dll (v2.10.7) and Prism.DryIoc.Forms (v6.3.0.1) packages to Android and Shared projects
Changed prism xmlns in App.xaml pointing to:
xmlns:prism="clr-namespace:Prism.DryIoc;assembly=Prism.DryIoc.Forms"
Changed the AndroidInitializer class in MainActivity.cs to:
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainer container)
{}
}
I created a blank app using the Prism DryIoC template and it works fine. Also, I compared the App.xaml, App.xaml.cs and MainActivity.cs files with those in my project and everything is OK.
What else can I do?
If you're converting over you'll need to do the following:
1) Remove the following three packages
- Prism.Unity.Forms
- Unity
- CommonServiceLocator
2) Update the xml namespace to clr-namespace:Prism.DryIoc;assembly=Prism.DryIoc.Forms in your App.xaml
3) Remove the Unity namespaces from your App.xaml.cs and replace them with Prism.DryIoc and DryIoc. Do the same in any IPlatformInitializer implementations, also changing IUnityContainer to IContainer
4) Update any service registrations since the signatures do not match between Unity and DryIoc.
5) Update any services or anything you specifically have that may directly use IUnityContainer to use IContainer.
6) Delete your obj and bin folders, clean and rebuild.
If you run into any trouble you can compare your project to either the Hamburger Menu or Tabbed Navigation samples.
UPDATE
Starting in Prism 7.2 the XML Namespace that is recommended for use is simply http://prismlibrary.com this will replace any clr-namespace declaration that you may have had previously and will work regardless of whether you are using an official Prism package or one of the Extended versions of Prism.
What worked for me was creating a new project using Prism DryIoC template and moving all my code it.
I have a working PCL project for iOS and I'm trying to create the Windows Phone 8.1 version.
I'm following this tutorial: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/
And checking the app at: https://github.com/xamarin/xamarin-forms-samples/tree/master/UsingResxLocalization
But it is too deprecated. Even the git project is different from the tutorial, and none of them works.
The ILocalize interface for Windows should look like:
[assembly: Dependency(typeof(UsingResxLocalization.WinPhone.Localize))]
namespace UsingResxLocalization.WinPhone
{
public class Localize : UsingResxLocalization.ILocalize
{
public System.Globalization.CultureInfo GetCurrentCultureInfo ()
{
return System.Threading.Thread.CurrentThread.CurrentUICulture;
}
}
}
But System.Threading.Thread.CurrentThread.CurrentUICulture simply doesn't exist. I found out that I can use Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString() instead.
It works for the localized language resources but the default Resource is not working either for the default language "en" or any other non localized language like "ru". I get another error:
In the TranslateExtention class ProvideValue() method I get:
Key 'Start' was not found in resources 'AppNameSpace.AppResources' for
culture 'en'
Being "Start" the first key it tries to get from the resource. It happens for all the other keys on the project.
AppNameSpace.AppResources would be the right file, and "en" is the region I set, so it should work. But it's not.
I'm also getting the following warning when compiling:
The assembly "MyApp.dll"
does not have a NeutralResourcesLanguageAttribute on it. To be used in
an app package, portable libraries must define a
NeutralResourcesLanguageAttribute on their main assembly (ie, the one
containing code, not a satellite assembly). 4>C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppXPackage.Targets(1216,5):
warning APPX2002: Cannot find the neutral resource language for the
resources for portable library 'MyApp'. Verify that
the portable library defines a NeutralResourcesLanguageAttribute. The
build is continuing assuming the project's default culture of 'en-US'
correctly identifies the portable library's neutral resources.
4>MakePRI : warning 0xdef00522: Resources found for language(s) 'de,
es, fr, pt' but no resources found for default language(s): 'en-US'.
Change the default language or qualify resources with the default
language. http://go.microsoft.com/fwlink/?LinkId=231899
But I have no idea how to fix it.
On the tutorial it also says:
Windows Phone projects must be properly configured for localized text
to be displayed. Supported languages must be selected in the Project
Options and the WMAppManifest.xml files. If these settings are not
updated the localized RESX resources will not be loaded.
Fine, but those options doesn't exist anymore. At least where they should be. I even found a Package.appxmanifest file in my project, but it doesn't have those regional options.
So, I need help with an updated way to do it.
Thanks
So I found out that when you add a Windows Phone project to a solution with no Windows Phone projects, it doesn't add everything it needs.
Also the tutorials don't show everything that is necessary (no big news there).
All my project was missing was [assembly: NeutralResourcesLanguage("en-US")] in my PCL AssemblyInfo.cs file.
The RESX tutorial also says that you should use:
if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android)
{
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
}
In the TranslateExtention.cs file because Windows Phones don't need it. Well, that's false. At least for the emulator to get the right language, it needs to use the DependencyService and get the CultureInfo this way:
System.Globalization.CultureInfo ci = null;
ci = new System.Globalization.CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0].ToString());
return ci;
I am trying to create a Xamarin.Android Component to send to the Xamarin Component Store, and I need to bundle my Colors.xml with it, so it is accessible to everyone using my component. I am setting the build action for the Colors.xml tp "AndroidResource".
So I created an Android sample application, when I add a reference to my library project's dll, I am able to use the colors defined, my project compiles and it runs, but I get no intellisense on Xamarin Studio. So that would be a bad thing for anyone using my library, as they would not be able to even see that the colors are available to use. You can see in this picture the colors defined are not available in the suggestions box.
If I add the Colors.xml directly into my project, I get Intellisense support, like this:
Is there a way to achieve the same result above when adding just the dll reference? Did I do something wrong or Xamarin.Android/Xamarin Studio doesn't support this kind of scenario?
EDIT: To be clear, I know this is a problem with Intellisense not being able to pick up the values, but is there anything I can do to make it work?
First, I know Silverlight project can't reference to non-Silverlight based project like Windows class library or Asp.net MVC project. But I need to create my projects which can support both Silverlight-based project & Asp.net MVC project.
So, I created Silverlight-based project for my sharing source code. It works fine on VS.net 2008 & .Net 3.5 SP1. However, I found some error when I try to use some method of Silverlight-based project from .Net-based project like the following code.
Silverlight-based Method
public static void InitializeInstance(object obj)
{
// Initialize Field Value
foreach (FieldInfo fi in obj.GetType().GetFields())
{
foreach (Attribute attr in fi.GetCustomAttributes(true))
{
if (attr is DefaultValueAttribute)
{
DefaultValueAttribute dv = (DefaultValueAttribute)attr;
fi.SetValue(obj, dv.Value);
}
}
}
// Initialize Property Value
foreach (PropertyInfo pi in obj.GetType().GetProperties())
{
foreach (Attribute attr in pi.GetCustomAttributes(true))
{
if (attr is DefaultValueAttribute)
{
DefaultValueAttribute dv = (DefaultValueAttribute)attr;
if (pi.CanWrite)
{
pi.SetValue(obj, dv.Value, null);
}
}
}
}
}
.Net-based Method
private void Form1_Load(object sender, EventArgs e)
{
InitializeInstance(this);
}
Error Detail
System.IO.FileNotFoundException:
Could not load file or assembly
'System, Version=2.0.5.0,
Culture=neutral,
PublicKeyToken=7cec85d7bea7798e' or
one of its dependencies. The system
cannot find the file specified. File
name: 'System, Version=2.0.5.0,
Culture=neutral,
PublicKeyToken=7cec85d7bea7798e' at
InitializeInstance(Object obj)
Finally, I try to solve this problem by copying system.dll of Silverlight to output directory and reference it. It still shows same error. So, I think this error may be limitation of both .Net & Silverlight platform. Do you have any idea for avoid this issue?
PS. I know I can use this technique for a few sharing code. But it’s impossible to do this for my projects. Because it’s very complicate & very complex more than directly create Silverlight-based or .Net-based class library.
Thanks,
The trouble here is that those types share an assembly with a different strong name: System.Windows in Silverlight, PresentationFramework or PresentationCore on the desktop CLR.
So at runtime, the intended type cannot be loaded, and there are no type forwarders for the Silverlight-to-desktop types.
My recommended solution
Consider using file links, instead of actually trying to reference the same built binary.
This way, you can have a source structure for your project that may look like this:
MyApp\
Silverlight\
Page.xaml
Page.xaml.cs
(link) ..\AspMvc\MySharedDataFile.cs
AspMvc\
MySharedDataFile.cs
MyApp.cs
This way, the source will be re-compiled with both projects. We use this on the Silverlight Toolkit to build many controls, including the charting and data visualization controls, for both WPF and Silverlight. This is by rebuilding for each platform, instead of referencing the binaries from both.
To insert a link in Visual Studio, just right-click on your project or one of its folder, Add Existing Item, then find it in the explorer open file dialog. however, instead of just clicking the button, click on the little down arrow drop-down on the Add file button, and select the "Add as link" option.
Then, it simply builds that file from another location, but it is not a copy, so you can maintain it in one place and use in both.
A crazy solution
You can use .NET reflection from your desktop app that is of a much higher trust to actually create a new app domain, hook up to the assembly resolution event, and see what that does. You may be able to instead return the type from the desktop CLR, or just no-op these warnings.
No clue if it works.