Xamarin Cross Platform Application Package Name in C# - xamarin

Is there a way to programmatically access the application package name/namespace/version information in Xamarin portable C# code?
Like this (link is how to access for Android, I want to know if there is a pre-existing C# cross platform way to access this in the portable class code.)
Not this.

i got answer to your problem, but it is using dependency service to work. There is no other way if there is no Xamarin plugin.
Here is the code:
Interface in pcl project:
public interface IPackageName
{
string PackageName { get; }
}
Android implementation:
public class PackageNameDroid : IPackageName
{
public PackageNameDroid()
{
}
public string PackageName
{
get { return Application.Context.PackageName; }
}
}
iOS implementation:
public class PackageNameIOS : IPackageName
{
public PackageNameIOS()
{
}
public string PackageName
{
get { return NSBundle.MainBundle.BundleIdentifier; }
}
}
Don't forget to mark platform files for dependency service in Android file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameDroid))]
and iOS file:
[assembly: Xamarin.Forms.Dependency(typeof(PackageNameIOS))]
Now you're good to go and can use it in PCLs just like that:
string packageName = DependencyService.Get<IPackageName>().PackageName;
Hope it helps others.

Related

Access App Global variables in Xamarin Prism.Forms Module (Prism.Modularity)

I am developing Xamarin Forms cross platform Application with Prism.Forms.
Trying to implement modularity in my app using Prism.Modularity.
I have created couple of modules (xamarin forms portable class libraries) in addition to the existing default portable class library. Now I have to use the global static variables that i have declared in App.Xaml.cs (In APP class) in the module. Is there anyway i can access current App in module? Or is there any alternative to share data between modules and the main App.
Like shared services?
Declare an interface somewhere reachable by all modules that need it, and register an implementation singleton in one of the modules...
Example:
public interface ISomeDataProvider
{
int SomeNumber { get; set; }
}
internal class SimpleDataProvider : ISomeDataProvider
{
public int SomeNumber { get; set; }
}
internal class InAModule
{
public InAModule( ISomeDataProvider dataProvider )
{
dataProvider.SomeNumber = 1;
}
}
internal class InAnotherModule
{
public InAnotherModule( ISomeDataProvider dataProvider )
{
_dataProvider = dataProvider;
}
public void PrintSomeNumber()
{
Console.WriteLine( _dataProvider.SomeNumber.ToString() );
}
private readonly ISomeDataProvider _dataProvider;
}

How can I launch a Android Activity from Xamarin Forms Page?

I know that i can launch a Xamarin Forms page from an Native Android Activity, but how do i start a Native Android Activity from Xamarin Forms ContentPage. I tried with DependencyService but without success.
I figured it out.
First i needed to create a Interface like this in Xamarin Project
public interface IRecordVideoPage
{
void StartNativeIntentOrActivity();
}
then i launched the Native Android Activity from my Xamarin Forms Content Page using Dependency Service like this:
DependencyService.Register<IRecordVideoPage>();
DependencyService.Get<IRecordVideoPage>().StartNativeIntentOrActivity();
next I created a RecordActivity class in my Android Project that loads the Test Activity
[assembly: Xamarin.Forms.Dependency(typeof(RecordActivity))]
namespace StreamTest.Droid
{
[Activity(Label = "RecordActivity")]
public class RecordActivity : IRecordVideoPage
{
public void StartNativeIntentOrActivity()
{
var intent = new Intent(Forms.Context, typeof(Test));
Forms.Context.StartActivity(intent);
}
}
finally I set the content view in the Test class:
public class Test : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public Test() { }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Record);
}
}
}
DependencyService is your best option. The concept behind PCL (Portable Class Library) is exactly to be platform independet.
Maybe we can help if you show the error you are getting with Dependency Service

Visual Studio not resolving Dapper.Extension Namespace

I am trying to learn how to use Dapper.Extension but after multiple attempts installing and reinstalling the nuget package. I can't get the namespace to resolve and usable. I am creating a generic repository but when i attempt to include the namespace, VS doesn't even see it. I have looked all over their documentation and install guides but can't see anyone else having this issue. Is there something stupid i am over looking? See my Find method below using the extension.
public T Find(int id)
{
using(var conn = _ConnectionFactory.GetConnection())
{
return conn.Get<T>(id);
}
}
You have added the libraries correctly but you need to add using references at the top of your classes to import the dapper namespace and add the additional methods to the SqlConnection object. To do so add both of the lines below to the top of your class files, before any namespace declarations.
using Dapper;
using DapperExtensions;
The equivalent in VB.NET would be:
Imports Dapper
Imports DapperExtensions
There are two nuget packages "DapperExtensions" and "Dapper.Extensions". Make sure you have installed the first one. I did this mistake too
To test I used VS 2015:
Create new console application
Install-Package DapperExtensions
Install latest version of Dapper: Install-Package Dapper -version 1.50.2
Here is the test code:
using System.Data.SqlClient;
using DapperExtensions;
namespace ConsoleApplication2
{
public class Foo
{
public int Id { get; set; }
}
class Program
{
static void Main(string[] args)
{
var result = Find(1);
}
public static Foo Find(int id)
{
using (var conn = new SqlConnection(#"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
conn.Open();
var foo = conn.Get<Foo>(id);
return foo;
}
}
}
}
I had a similar issue but that was because I had installed "Dapper.Extension" via nuget instead of installing "DapperExtensions". Could you have done something similar?

Xamarin Linker stipping out static class properties?

In VS 2013, Xamarin Forms.
Our Android & IOS app has a static class:
public static class CacheKeys
{
public static string RememberMeEmail = "RememberMeEmail";
public static string RememberMeSwitch = "RememberMeSwitch";
}
This class is stripped by the linker in the iOS device debug build, but not in the
simulator or Android.
See attached screenshot.
I have set the Debug Linker for iPhone to Do Not Link, yet this static class is
removed.
??????
You can set a preserve attribute to your class: http://developer.xamarin.com/guides/ios/advanced_topics/linker/
See the section: Preserving Code
Example:
[Preserve]
public static class LinkerPreserve
{
static LinkerPreserve()
{
throw new Exception(typeof(SQLitePersistentBlobCache).FullName);
}
}
public class PreserveAttribute : Attribute
{
}
I also found that I had to check the "Enable generic value type sharing" in the Advanced tab of "iOS Build"

Entity framework asp.net MVC3 .NET 4

I Have a little snag. With my asp.net mvc3 application.
When I trying to compile my app I obtain this Error
Error 2 'Blog.Domain.Concrete.EFEntryRepository' does not implement interface member 'Blog.Domain.Abstract.IEntryRepository.SaveEntry(Blog.Domain.Entities.Entry)' D:\dokumenty\Visual Studio 2010\Projects\MVC3\Blog\Blog.Domain\Concrete\EFEntryRepository.cs 10 19 Blog.Domain
This is my Interface.
namespace Blog.Domain.Abstract
{
public interface IEntryRepository
{
IQueryable<Entry> Entries { get; }
void SaveEntry(Entry entry);
void DeleteEntry(Entry entry);
}
}
And this is my implementation of it.
public class EFEntryRepository : IEntryRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Entry> Entries
{
get { return context.Entries; }
}
public void SaveEntry(Entry entry)
{
if (entry.EntryID == 0)
context.Entries.Add(entry);
context.SaveChanges();
}
public void DeleteEntry(Entry entry)
{
context.Entries.Remove(entry);
context.SaveChanges();
}
}
This is link to my project. http://sigma.ug.edu.pl/~kkubacki/Blog.zip //NEW
Now I is compiling.
What I Do Wrong ?
I have new Information about the bug. Now the solution is compiling but the application crashes with the bug information
"{"The type 'Blog.Domain.Concrete.Entry' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject."} " Visual studio shows bug in the EFEntryRepository class.
I don't know what to do please help.
OK Problem IS Solved.
You have two different Entry classes in different namespaces.
Blog.Domain.Entities.Entry
Blog.Domain.Concrete.Entry
The interface is referring to one and the implementation is referring to the other.

Resources