WP7 - how to let user set app theme - windows-phone-7

My app has 10 pages and all of them have black background. I want to let user change background color of all pages using radioButton in my app. How can I do this in easiest way?

Go through these blogs
1. Theme Forcing for Windows Phone 7 or,
2. Windows Phone Mango Custom application Theme
these might prove helpful to you. You can study these and modify to put them in your settings page.
Thank You :)

Ok so you have 10 pages and on each page you want to change the background colour of those pages through the settings menu. What you can do is use the Windows Phone IsolatedStorageSettings.
Firstly you want to initialize the IsolatedStorageSettings. You can do that like this:
IsolatedStorageSettings MyAppSettings = IsolatedStorageSettings.ApplicationSettings;
Then you will have to set a default value for it so it doesn't throw an exception. You can do this:
MyAppSettings.Add("PageBackgroundColor", "#000000"); // you can set whatever the default colour you want here. i.e. Black
the best place i think would be is to add this code in:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("PageBackgroundColor"))
{
// Don't do anything because you've already set the default background colour for the pages
}
else
{
// add the default color
}
}
Now in your MainPage you can reinitialize the IsolatedStorageSettings. Once you've done that you will want to get the value of the setting and depending on the value you will want to change the background color. To read the value:
string Sortval = (string)MyAppSettings["PageBackgroundColor"];
You can add this in the:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
Or
public MainPage
{
InitializeComponent();
}
Remember that public MainPage will only run once and the OnNavigatedTo runs every time the page is loaded so if you want to update the background color right after adding the setting, OnNavigatedTo is the way to go but if you want to apply the changes after a restart, public Mainpage is it.
Now to read the value and change it you want to do something like:
string val = (string)MyAppSettings["PageBackgroundColor"];
if (val == "#000000")
{
//change to black
}
else if (val == "your hex color")
{
//change to whatever color
}
else if (val == "another hex color")
{
//...
}
Now to save the value you want to reinitialize the IsolatedStorageSettings in your settings page and to save the values it would be something like this:
MyAppSettings.Remove("PageBackgroundColor");
MyAppSettings.Add("PageBackgroundColor", "your hex color");
MyAppSettings.Save();
This is untested however It should give you the very basic idea on how to do it in terms of saving and loading setting and then applying it

Related

How do I override the Xamarin Forms TabbedPage item fonts for iOS?

Wanting to achieve a consistent look for my Xamarin Forms app, I need to know how to change the font for the tabbed page tab bar icons. Using UITabBarItem.Appearance as the iOS API would suggest does not appear to have any effect. What's necessary to do this?
U need to write a custom renderer like this one , take a clue from below code ! it has what u r seeking
[assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(TabbedPageCustom))]
namespace App.iOS
{
public class TabbedPageCustom : TabbedRenderer
{
public TabbedPageCustom ()
{
TabBar.TintColor = UIKit.UIColor.White;
TabBar.BarTintColor = UIKit.UIColor.White;
TabBar.BackgroundColor = UIKit.UIColor.Red;
}
protected override void OnElementChanged (VisualElementChangedEventArgs e)
{
base.OnElementChanged (e);
// Set Text Font for unselected tab states
UITextAttributes normalTextAttributes = new UITextAttributes();
normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
normalTextAttributes.TextColor = UIKit.UIColor.Blue;
UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
}
public override UIViewController SelectedViewController {
get {
UITextAttributes selectedTextAttributes = new UITextAttributes();
selectedTextAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 20.0F); // SELECTED
if (base.SelectedViewController != null)
{
base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
}
return base.SelectedViewController;
}
set {
base.SelectedViewController = value;
foreach (UIViewController viewController in base.ViewControllers)
{
UITextAttributes normalTextAttributes = new UITextAttributes();
normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
normalTextAttributes.TextColor = UIKit.UIColor.Blue;
viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
}
}
}
}
}
This was a particularly interesting problem. I tried:
UITabBarItem.Appearance
Using the UITabBarItem.Appearance.SetTitleTextAttributes method to update the UITextAttribute to my font (size 9.0f) for UIControlState.Normal. This didn't appear to make any difference.
UINavigationBar.Appearance
I found out that setting UINavigationBar.Appearance.SetTitleTextAttributes would update both the UINavigationBar text appearance as well as the UITabBarItem text appearance.
Which was a problem because the tabbed page item font size was far too large.
Customizing the TabbedRenderer
Inspired by a sample I saw somewhere, I subclassed TabbedRenderer in the iOS project.
I tried overriding the settor for TabbedRenderer.SelectedViewController property and loop through the ViewControllers property to set their items. The icons would display with the standard font, but once the user changed the tab they would all update to the desired font. Almost there!
I then tried overriding AddChildViewController and updating that controller's TabBarItem after it was added which ended up having no effect. The TabBarItem for the added page was being updated at some point after the controller was added.
Eventually I found out that overriding ViewWillAppear and setting the appearance for all the tab bar items at that time seemed to do the job I desired.
I've included a sample in this gist.

Why RecyclerView is not working setBackground function?

I tried many Times for setting backgroundColor on RecyclerView. but i try to scroll then background was removed.So I can fix backgroundColor in RecyclerView. Help me please.
Or I Want to change ForegroundColor.
My Issue Video
https://www.youtube.com/watch?v=C29qhPb44FE
I don't know the reason...
You need to first understand how RecyclerView works.
As you scroll through the cells, the views that gets out of the screen will be RECYCLED, and they will subsequently be reused to display the incoming views. Hence the name RecyclerView. This way, views will always be recycled and reused, thus saving memory.
What you need to do is something like this:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//mList and mSelectedObjects are array lists
View yourView = holder.itemView.findViewById(R.id.your_view);
Object object = mList.get(position);
yourView.setTag(object);
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Object object = (Object) v.getTag();
if (mSelectedObjects.contains(object)) {
mSelectedObjects.remove(object);
v.setBackground(null);
} else {
mSelectedObjects.add(object);
v.setBackgroundColor(Color.GRAY);
}
}
});
}
If you set programmatically the background color. You have to set every time the normal color and the selected color.
RecyclerViews are reusing their views. When an item leaves the screen it will be reused to increase the performance of the recycler view.
In this case when one set programmatically a background color and the item leaves the screen. It will be reused in the new item and the background color is still the same as when the item has left the screen.

Xamarin forms background image for all Pages

It´s possible to set an image to be the background of all Pages on my application (for all platforms IOS, Android and WP) ?
Page has a BackgroundImage property, so I can create a base page, set the background and make all my pages extends from it.
I want to know if there is a more appropriate solution, to handle this.
You could use Styles in your App.cs :
public App ()
{
Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(Page)){
Setters = {
new Xamarin.Forms.Setter { Property = Page.BackgroundImageProperty, Value = "back.png"},
}
});
}
much less preferred or advised but for the sake of showing another option you could attach an event handler to your NavigationPages and set the background from there:
yourRootNavigationPage.Pushed+= (sender, e) => e.Page.BackgroundImage = "back.png";

Page navigation - keeping data on previous page

In a windows phone app,a page is navigated to another page and on pressing back button, goes back to previous page.Now in previous page the previous data has to be displayed. But its not displaying immediately and takes some time to load. How to solve this issue?
Overall the question is how to maintain the content of page(containing dynamic data) displayed in back navigation?
Use PhoneApplicationService class to keep the data while you navigate between the pages. Here is some samples. Actually, it's very easy:
protected override void OnNavigatedFrom(NavigationEventArgs args)
{
if (ContentPanel.Background is SolidColorBrush)
{
Color clr = (ContentPanel.Background as SolidColorBrush).Color;
if (args.Content is MainPage) (args.Content as MainPage).ReturnedColor = clr;
// save color
PhoneApplicationService.Current.State["Color"] = clr;
}
base.OnNavigatedFrom(args);
}
protected override void OnNavigatedTo(NavigationEventArgs args)
{
// restore color
if (PhoneApplicationService.Current.State.ContainsKey("Color"))
{
Color clr = (Color)PhoneApplicationService.Current.State["Color"];
ContentPanel.Background = new SolidColorBrush(clr);
}
base.OnNavigatedTo(args);
}
How to preserve and restore page state for Windows Phone
You can use single ton object of View Model to store data in a page provided all the controls are bind to a property in View Model.
Then if you do not clear the values of the controls when navigating away from the page, the data will be displayed in the page when you navigate back to that screen provided all the controls

How to handle back button in windows phone 7?

I have an application, the main page contains few functions. To explain in detail - I have save, color palette button in my main page. When any of these buttons are clicked, save pop up or color palette appears. How to handle back button of the device, when color palette or save pop up is opened. When back button is pressed at these scenario it should just make them invisible and stay on the main page. When nothing is being performed in the main page, then it should come out of the app. I tried to make their visibility collapsed on back button press. But it still is coming out of the application.
Please, guide me in this. Thanks in advance.
Override PhoneApplicationPage.OnBackKeyPress and then set CancelEventArgs.Cancel to true if you want to stop it from actually going back.
protected override void OnBackKeyPress(CancelEventArgs args)
{
if (PanelIsShowing)
{
HidePanel();
args.Cancel = true;
}
}
The back button behaves as is intended by Microsoft.
If you change its behavior you risk your application not being certified for the Marketplace.
If you want the back button to close the popups, turn the popups into pages so the back button navigates back to the main page..
You need to use
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (_popup.IsOpen)
{
_popup.IsOpen= false;
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
That should do the trick.

Resources