Could a switch statement that switches resources be more generic? - c#

I am working on a kiosk application that has a button to choose a language.
The button after that has a picture of the languages flag and it's name. Right now I am using a switch statement to set the image, but I would like to have it more generic so when adding languages I wouldn't have to change code everywhere. I am getting the image from my apps resources, and I would like to keep it that way.
The only way I can think of would be by retrieving the images from files and naming them according to the language string they are for. The language string is in this format "en-US". Also the language string is declared in a function before this one is called.
private void SetLanguageButton()
{
switch (language)
{
case "en-US":
buttonLanguage.Image = Properties.Resources.en_US;
break;
case "hu-HU":
buttonLanguage.Image = Properties.Resources.hu_HU;
break;
case "sk-SK":
buttonLanguage.Image = Properties.Resources.sk_SK;
break;
}
buttonLanguage.Text = resourceManager.GetString("languageName", cultureInfo);
}

If you have cultural-ized resources, you can get data as following
buttonLanguage.Image =
(Icon)Properties.Resources.ResourceManager.GetObject("langicon", cultureInfo);
buttonLanguage.Text =
Properties.Resources.ResourceManager.GetString("langname", cultureInfo);

Related

how do i change background colors based on current one by using a switch

so i am trying to change the background color based on the current color by using a switch however iam getting an error.
switch (BtnColor.BackgroundColor)
{
case Color.Red:
BtnColor.BackgroundColor = Color.White;
break;
case Color.White:
BtnColor.BackgroundColor = Color.Blue;
break;
default:
BtnColor.BackgroundColor = Color.Red;
break;
}
i am getting the following error on: case Color.White: and case Color.Red:
A constant value is expected
Colors are not constants, and that's what you're supposed to use when using Switch statements.
Predefined Colors are Static Properties of the Color class, so that can't be used.
In this case you can use if/then/else statements to achieve the same exact thing.
Also refer to this answer

implement different language into UWP application from code behind using multilingual

Is it possible to implement different language into UWP application from code behind using multilingual toolkit & not by setting the desired language from the settings but instead from the dropdown list of language within the application
You don't actually need the Multilingual App Toolkit to modify the language of your app. By default, the app will configure itself based on the settings on the machine, but you can override that by setting the CurrentCulture and/or CurrentUICulture.
Say for instance you had a Combobox that contained "English", "Spanish" and "French"... and your desired behavior is to switch your language to whichever value the user selects. All you'd need to do is hook up the SelectionChanged event. Here's what the code might look like:
private void ChangeLanguage(object sender, SelectionChangedEventArgs e)
{
var newlySelected = e.AddedItems[0] as ComboBoxItem;
string newLanguage = newlySelected.Content.ToString();
switch (newLanguage)
{
case "English":
{
CultureInfo.CurrentCulture = new CultureInfo("en");
CultureInfo.CurrentUICulture = new CultureInfo("en");
break;
}
case "Spanish":
{
CultureInfo.CurrentCulture = new CultureInfo("es");
CultureInfo.CurrentUICulture = new CultureInfo("es");
break;
}
case "French":
{
CultureInfo.CurrentCulture = new CultureInfo("fr");
CultureInfo.CurrentUICulture = new CultureInfo("fr");
break;
}
default:
{
throw new NotImplementedException("Unidentified Language");
}
}
}
Naturally, I'd encourage you to do all the appropriate error checking (Make sure that the cast to ComboBoxItem works, etc...).
Also, remember that you need to set CurrentCulture if you want things like Dates and Times to show in the appropriate locale and use CurrentUICulture if you want to modify which resources your ResourceLoader uses to populate the UI.
Hopefully this should get you up and running!
--Dante

Prompt user to answer boolean choice using Revit API in C#

I created a Revit plugin in C# that allow users totally new to 3D technology to choose a family, and insert it in their project. But right now the user does not have the choice between placing an object on the point anywhere or on a face. It's either one or the other.
Right now my code looks like this :
bool useSimpleInsertionPoint = false; //or true
bool useFaceReference = true; //or false
if (useSimpleInsertionPoint)
{
//my code for insertion on point here
}
if (useFaceReference)
{
//my code for face insertion here
}
What I would like to do is ask the user what does he want to do.
Does TaskDialog.Show would do the trick or is it something else ?
Thanks in advance.
Vincent's approach is good. The one thing that I like a little bit more is to use the CommandLink options with TaskDialog. This gives you the "big option" buttons to pick from, provides both an answer as well as an optional line of "explanation" about each answer.
The code looks like:
TaskDialog td = new TaskDialog("Decision");
td.MainContent = "What do you want to do?";
td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
"Use Simple Insertion Point",
"This option works for free-floating items");
td.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
"Use Face Reference",
"Use this option to place the family on a wall or other surface");
switch (td.Show())
{
case TaskDialogResult.CommandLink1:
// do the simple stuff
break;
case TaskDialogResult.CommandLink2:
// do the face reference
break;
default:
// handle any other case.
break;
}
This should do the trick:
TaskDialog dialog = new TaskDialog("Decision");
dialog.MainContent = "What do you want to do?";
dialog.AllowCancellation = true;
dialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
TaskDialogResult result = dialog.Show();
if(result == TaskDialogResult.Yes){
// Yes
TaskDialog.Show("yes", "YES!!");
}
else
{
// No
TaskDialog.Show("no", "NO!!");
}
Code tested and proved to work in a Revit macro in 2014 so should work fine anywhere else in an add-in as well.

Setting the last background image as the default

I have added four images and by default i have background image. I used a button to change the background randomly. It is panorama page and i just want my app to save the last state(i.e to remember the last background image) and if my app is activated then the last image should be the default background image. Since i have already added some images to my app so i think this doesn't need Isolated Storage. What i need is if the current background image(imguri) is bg1.jpg, and if i exit the app and if i relaunch it then the default background image should be bg1.jpg. Need help!
private void BackgroundBrowser_Click(object sender, RoutedEventArgs e)
{
string imguri = "";
click_count = click_count % 5;
switch (click_count)
{
case 0: imguri = "Image/bg.jpg"; break;
case 1: imguri = "Image/bg1.jpg"; break;
case 2: imguri = "Image/bg3.jpg"; break;
case 3: imguri = "Image/bg2.jpg"; break;
case 4: imguri = ""; break;
}
click_count++;
var app = Application.Current as App;
app.appBmp = new BitmapImage(new Uri(imguri, UriKind.Relative));
ImageBrush imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.UniformToFill;
imageBrush.Opacity = 0.7;
imageBrush.ImageSource = app.appBmp;
this.LayoutRoot.Background = imageBrush;
app.appbrush = imageBrush;
app.backchanged = true;
}
You can use Application or User Settings. Go to project properties and click the Settings tab. Then create a setting name LastImagePath with String as a type:
Now just before this line:
var app = Application.Current as App;
Add this to save the path to the LastImagePath settings:
Properties.Settings.Default.LastImagePath = imguri;
Properties.Settings.Default.Save();
To load the last image, you can load up the setting wherever you want like this:
if (!(Properties.Settings.Default.LastImagePath == null))
imgpath = Properties.Settings.Default.LastImagePath;
you need save the last image name in a file when your application exit and read image name from the file and load it when your application start again. I think this is the simplest solution.
You can also use System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings in a similar manner to what DelegateX has shown. Mind that regardless of how do you 'save' your setting, it will be stored in the isolated storage space. It is just nicely wrapped and hidden as a Properties/ApplicationSettings/Session etc proeprties or class names, but in fact the data will land on ISO, and will evaporate when you uninstall the app from the device.
All items stored in the User/Application settings need to be serializable (there is a note at the bottom of the documentation here). Learn more about serialization here.

How to get Windows Phone 7 Theme Colour with XNA

I've been trying to use the guide available at http://geekswithblogs.net/mikebmcl/archive/2010/09/16/using-wp7-themes-in-your-xna-game.aspx but I cannot find the Application name, nor do I seem to be able to find a replacement for SolidColorBrush.
Unfortunately there is no library or easy to use code on the net to programmitcally get the tile colour in XNA on windows phone, even though its simple with Silverlight.
Any ideas how to go about this?
You can get the theme (dark/light) on the phone in a shorter way (works for XNA too):
Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
if(darkBackgroundVisibility == Visibility.Visible)
//Theme is Dark
else
//Theme is Light
To get the AccentColor, you need a but more code (I got it from this article on MSDN: How to: Apply Theme Resources for Windows Phone). I shortened the code from the switch-statement for readability and put it in a method. I also tested this in an XNA app and this works fine! :)
var currentAccentColorHex = (System.Windows.Media.Color)Application.Current.Resources["PhoneAccentColor"];
string currentAccentColor = ColorNameFromHex(currentAccentColorHex);
private string ColorNameFromHex(System.Windows.Media.Color hexColor)
{
switch(hexColor.ToString())
{
case "#FF1BA1E2": return "Blue";
case "#FFA05000": return "Brown";
case "#FF339933": return "Green";
case "#FFE671B8": return "Pink";
case "#FFA200FF": return "Purple";
case "#FFE51400": return "Red";
case "#FF00ABA9": return "Teal";
case "#FF8CBF26":
case "#FFA2C139": return "Lime";
case "#FFFF0097":
case "#FFD80073": return "Magenta";
case "#FFF09609": return "Mango";
default: return "custom eleventh color"; //Manufacturer color
}
}
Instead of returning a string containin 'Red' you could return a 'real' Color. For that you'll have to change return type of the method and the value.
Hope this helps!
You can get the current theme from the Resources for example getting the background color like this. In an App you could check this in the Application_Launching as well as Application_Activated to see if the theme changed while the App was in the background.
I'm pretty sure you can do a similar thing in an XNA game:
public enum PhoneTheme
{
Light,
Dark
};
public static PhoneTheme CurrentTheme { get; private set; }
Following in your activated/startup code:
string theme = Resources["PhoneBackgroundColor"].ToString();
CurrentTheme = theme == "#FF000000"
? PhoneTheme.Dark
: PhoneTheme.Light;

Categories

Resources