I want the user to choose his language in my app so if he chooses a different Language (because he wants to test his skills in other languages) than the Phone default language, I want all my globalized Strings to be changed during runtime.
Is there the possibility to override the culture Info in c# for wp7 or wp8 for the runtime of current app?
In App.xaml.cs, in the InitializePhoneApplication method:
private void InitializePhoneApplication()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
.......
}
Be sure to edit you .csproj project file to set support for the languages you want to use in the SupportedCultures tag:
<SupportedCultures>en;fr-FR</SupportedCultures>
Related
I tried to launch the UWP application from c# console application. It tried with below code which uses APPID
Process.Start(#"C:\Program Files (x86)\Windows Kits\10\App Certification Kit\microsoft.windows.softwarelogo.appxlauncher.exe", "1a75- 6f75 - 5ed3 - 8944 - 6b7df2bee095");
Is there any better way to launch UWP application programatically.
To launch any UWP app on the system you can use the following API:
AppListEntry.LaunchAsync Method
To get the AppListEntry for the desired application, use the PackageManager APIs:
PackageManager.FindPackageForUser(String, String) Method
Package.GetAppListEntriesAsync Method
Alternatively, you can use the following Win32 API insteadp of the AppListEntry API:
IApplicationActivationManager::ActivateApplication method
And when your console application is written in some exotic languages, for example Java, you can do something like
https://www.c-sharpcorner.com/article/launch-uwp-app-via-commandline/
For better results do omit these two attributes:
<!-- Executable="$targetnametoken$.exe"
EntryPoint="$targetentrypoint$"-->
Then carry on as described here.
I tried to launch the UWP app through Protocol. The below link will help how to create a protocol
Automate launching Windows 10 UWP apps
Now you can launch your application by using
Process.Start("URL:myapplication://");
The process class is available in System.Diagnostics. And also need to add the following method in App.xaml.cs file
protected override void OnActivated(IActivatedEventArgs args)
{
Initialize(args);
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
// Always navigate for a protocol launch
rootFrame.Navigate(typeof(MainPage), eventArgs.Uri.AbsoluteUri);
// Ensure the current window is active
Window.Current.Activate();
}
}
I have an application in Xamarin crossplatfrom. I created a solution for localize strings and it is working perfect. I am setting:
StringResources.Culture = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
But I have a problem with image resoruces. I created folders in android app:
drawable, drawable-pl-rPL, drawable-pl-rPL-hdpi, drawable-pl-rPL-ldpi, drawable-pl-rPL-mdpi
But I have no idea how to set global culture for app. I was trying to put in MainActivity class:
var ci = new System.Globalization.CultureInfo(lastLanguage);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Where lastLanguage is a code for lasdt selcted language (en or pl-PL). But it is not working, image is always taken from drawable folder, no from drawable-pl-rPL
Can you help me somehow?
I am developing a mobile application using Xamarin.Forms
I had the following Home page contains login info:
How can we have the application to automatically save the user name, so that they do not have to type it in each time (as in a browser)?
You can use Properties dictionary in Xamarin.Forms Application class. And let the Xamarin.Forms framework handle persisting user name between app restarts and pausing/resuming your app.
Save user name by writing it to Properties dictionary
var properties = Xamarin.Forms.App.Current.Properties;
if(!properties.ContainsKey("username")
{
properties.Add("username", username);
}
else
{
properties["username"] = username;
}
Then, when your login screen is about to appear (for example in OnAppearing method) check Properties for user name:
var properties = Xamarin.Forms.App.Current.Properties;
if(properties.ContainsKey("username")
{
var savedUsername = (string)properties["username"];
}
If it's not there, then it means that this is first time when user log in into your application.
A very similar question was posed just a few days ago - my answer on that question also applies to your question: The best way to save Configuration data in Xamarin.Forms based app?
Essentially, you want to store the information using the native settings functionality. I would advise against using Application.Properties for now. It is currently not reliable on Android, and in the past has had other problems. The nuget package referenced in my linked answer is a better approach and will save you some headache in the future.
The right way to be done is through the App settings plugin
https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings
What i did in my application is.
1) Installed Plugin.Settings from nuget
2)Added to Helpers->Settings.cs (autogenerated file by plugin) the following
public static class Settings
{
private static ISettings AppSettings
{
get { return CrossSettings.Current; }
}
private const string UserNameKey = "username_key";
private static readonly string UserNameDefault = "demo";
public static string UserName
{
get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}
}
3)In order to keep the username in the Application Context set
Settings.UserName = ViewModel.Username;
4)When you login screen starts
string username = Settings.UserName;
The answer is simple: persistance. Servers do this by setting cookies containing the data (or reference to it) that they want you to see when rendering the form field.
In order to do this in an app (with Xamarin for instance), you need to store the user's data into a file or database somewhere. Since you're using Xamarin you can probably use some sort of ConfigurationManager to keep track of this.
Obviously you could just create a config file in the local storage you have for your app (I don't think you need permissions to create files in that space).
When you have the info stored somewhere, just retrieve it and set the input's value to it.
I have a simple Xamarin Forms app. I've now got a simple POCO object (eg. User instance or an list of the most recent tweets or orders or whatever).
How can I store this object locally to the device? Lets imagine I serialize it as JSON.
Also, how secure is this data? Is it part of Keychains, etc? Auto backed up?
cheers!
You have a couple options.
SQLite. This option is cross-platform and works well if you have a lot of data. You get the added bonus of transaction support and async support as well. EDIT: In the past I suggested using SQLite.Net-PCL. Due to issues involving Android 7.0 support (and an apparent sunsetting of support) I now recommend making use of the project that was originally forked from: sqlite-net
Local storage. There's a great nuget that supports cross-platform storage. For more information see PCLStorage
There's also Application.Current.Properties implemented in Xamarin.Forms that allow simple Key-Value pairs of data.
I think you'll have to investigate and find out which route serves your needs best.
As far as security, that depends on where you put your data on each device. Android stores app data in a secure app folder by default (not all that secure if you're rooted). iOS has several different folders for data storage based on different needs. Read more here: iOS Data Storage
Another option is the Xamarin Forms settings plugin.
E.g. If you need to store a user instance, just serialize it to json when storing and deserialize it when reading.
Uses the native settings management
Android: SharedPreferences
iOS: NSUserDefaults
Windows Phone: IsolatedStorageSettings
Windows RT / UWP: ApplicationDataContainer
public User CurrentUser
{
get
{
User user = null;
var serializedUser = CrossSettings.Current.GetValueOrDefault<string>(UserKey);
if (serializedUser != null)
{
user = JsonConvert.DeserializeObject<User>(serializedUser);
}
return user;
}
set
{
CrossSettings.Current.AddOrUpdateValue(UserKey, JsonConvert.SerializeObject(value));
}
}
EDIT:
There is a new solution for this. Just use Xamarin.Essentials.
Preferences.Set(UserKey, JsonConvert.SerializeObject(value));
var user= JsonConvert.DeserializeObject<User>(Preferences.Get(UserKey, "default_value");
Please use Xamarin.Essentials
The Preferences class helps to store application preferences in a key/value store.
To save a value:
Preferences.Set("my_key", "my_value");
To get a value:
var myValue = Preferences.Get("my_key", "default_value");
If you want to store a simple value, such as a string, follow this Example code.
setting the value of the "totalSeats.Text" to the "SeatNumbers" key from page1
Application.Current.Properties["SeatNumbers"] = totalSeats.Text;
await Application.Current.SavePropertiesAsync();
then, you can simply get the value from any other page (page2)
var value = Application.Current.Properties["SeatNumbers"].ToString();
Additionally, you can set that value to another Label or Entry etc.
SeatNumbersEntry.Text = value;
If it's Key value(one value) data storage, follow below code
Application.Current.Properties["AppNumber"] = "123"
await Application.Current.SavePropertiesAsync();
Getting the same value
var value = Application.Current.Properties["AppNumber"];
Let's say my application doesn't support culture es-MX so when this is the current setting on the phone I'd like to set the culture to es-ES. Is this possible to do in Windows Phone and if so how?
This is definitely possible!
See this list for cultures and their identifiers
So for example in the App.xaml.cs constructor you could access Thread.CurrentThread.CurrentCulture to determine the actual culture the app is running in. Now if you want to force the UI to use another culture you can do that by setting the CurrentUICulture. For example:
if (Thread.CurrentThread.CurrentCulture.Name.Equals("es-MX"))
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-ES");
}