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"
Related
I have an application that uses a static class to store settings:
namespace ABC
{
public static class MS
{
public static bool abc;
public static bool def;
...
}
}
When the app starts up it goes to the database and updates some of these settings.
I have been told that holding them in a static class is not ideal for when I am doing bindings and adding notification so I'm thinking I should new a class and the hold them in an instance. If that's the case can someone give me some advice. Should I do that when the application starts up and should it be in the constructor of the App() if that is the best way to do it then how can I do this. Any small 4-5 line example would be a big help
public partial class App : Application
{
public static xxx
public App()
{
AS.appUpdated = "Feb 1, 2017";
AS.appVersion = "0.1";
InitializeComponent();
MainPage = new MS.MainPage();
}
}
Have a look at James Montemagno's settings plugin.
https://github.com/jamesmontemagno/SettingsPlugin
This abstracts away a lot of the plumbing around settings and persisting them locally. Calling one of your settings from anywhere in your application then becomes as simple as calling Settings.MySettingName.
When it comes to binding these values to a view I would always create a property for them in your view model that simply returns the value from the settings. If need be you can put them in a base viewmodel and go from there.
public class MyViewModel : BaseViewModel
{
}
public class BaseViewModel
{
public bool SettingA => Settings.SettingA;
public bool SettingB => Settings.SettingB;
}
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;
}
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
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.
With control level styling I do not need to worry about adding a line of code for every control on every page.
Style Class
public static class Styles
{
#region "Entry"
public static readonly Style Entry_Standard = new Style(typeof(Entry))
{
new Setter {Property = Xamarin.Forms.Entry.BackgroundColorProperty, Value = Color.Red }
};
#endregion
}
Custom Control
public class Custom_Entry : Entry
{
public Custom_Entry()
{
this.Style = Styles.Entry_Standard;
}
}
All of the example documentation from Xamarin though appears to favor setting styles as a property at the page level after object creation either through a page resource or static class reference.
Is there a valid reason for this or is it just that the short examples do not bother to abstract away control styles?