Мне нравится использовать Enums вместо магических строк в этом сценарии, потому что он предотвращает проблемы, вызванные опечатками, и делает параметры доступными для intellisense.
namespace TheGame
{
// declare enum with all available themes
public enum EnumGameTheme { theme1, theme2 };
// factory class
public class ThemeFactory
{
// factory method. should create a theme object with the type of the enum value themeToCreate
public static GameTheme GetTheme(EnumGameTheme themeToCreate)
{
throw new NotImplementedException();
// TODO return theme
}
}
// TODO game theme class
public class GameTheme { }
}
Код вызова завод дал тему выбран (скажем) lstThemes:
// get the enum type from a string (selected item in the combo box)
TheGame.EnumGameTheme selectedTheme = Enum.Parse(typeof(TheGame.EnumGameTheme), (string)lstThemes.SelectedValue);
// invoke the factory method
TheGame.GameTheme newTheme = TheGame.ThemeFactory.GetTheme(selectedTheme);
код, чтобы получить доступные темы как строки:
// get a string array of all the game themes in the Enum (use this to populate the drop-down list)
string[] themeNames = Enum.GetNames(typeof(TheGame.EnumGameTheme));
ли вы имеете в виду скины пользовательского интерфейса ? Как этот? http://www.codeproject.com/Articles/19782/Creating-a-Skinned-User-Interface-in-WPF –
Я думаю, что фактически изменяет существующий пользовательский элемент управления. Что я хотел бы, так это добавить новый пользовательский элемент управления в 'Grid.Children [reservedIndex]' во время выполнения –
Зачем вам нужен новый пользовательский элемент управления? Несколько неясно, что вы подразумеваете под «темой». –