我有一个多页MVVM
应用程序,它有一些ViewModel。 (这是一个爱好应用程序,存储统计数据和东西)
以下课程
GamePageViewModel
MenuViewModel
PlayersViewModel
StartPagePresenter
Controller
(这充当PageSwitcher
以及存储数据库中所有TableAdapters
的模型)所有这些都有一些相同的变量
Universe
Character
Season
我想要做的是将这些变量移动到我的MainMenuViewModel
(所有页面上的菜单栏),在这里您可以选择Universe,Character,Season等,然后填充到另一个的ViewModels。
我的MenuViewModel
班级
public class MenuViewModel : ObservableObject
{
private DataView _Universes = Controller.UniverseTableAdapter.GetData().DefaultView;
private DataRowView _SelectedUniverse;
public DataView Universes
{
get { return _Universes; }
set
{
_Universes = value;
RaisePropertyChangedEvent("Universes");
}
}
public DataRowView SelectedUniverse
{
get { return _SelectedUniverse; }
set
{
_SelectedUniverse = value;
Controller.SelectedUniverse = _SelectedUniverse;
RaisePropertyChangedEvent("SelectedUniverse");
}
}
public ICommand GoHome { get { return new DelegateCommand(NavigateHome); } }
public ICommand GoPlayers { get { return new DelegateCommand(NavigatePlayers); } }
public ICommand GoGame { get { return new DelegateCommand(NavigateGame); } }
public void NavigateHome() { Controller.Switch(new StartPage()); }
public void NavigatePlayers() { Controller.Switch(new PlayerPage()); }
public void NavigateGame() { Controller.Switch(new GamePage()); }
}
我以为我会尝试将它们存储在这样的Controller
类中,但不确定如何将RaisePropertyChangedEvent
事件发送到每个ViewModel
public static class Controller
{
#region Variables
//Variables
private static MainWindow _Window;
private static MaddenDBDataSet _MaddenDB = new MaddenDBDataSet();
private static CharacterTableAdapter _CharacterTableAdapter = new CharacterTableAdapter();
private static FranchiseTableAdapter _FranchiseTableAdapter = new FranchiseTableAdapter();
private static GamePlayerTableAdapter _GamePlayerTableAdatper = new GamePlayerTableAdapter();
private static GameTableAdapter _GameTableAdapter = new GameTableAdapter();
private static PersonTableAdapter _PersonTableAdapter = new PersonTableAdapter();
private static SeasonTableAdapter _SeasonTableAdapter = new SeasonTableAdapter();
private static UniverseTableAdapter _UniverseTableAdapter = new UniverseTableAdapter();
private static UserYearTableAdapter _UserYearTableAdapter = new UserYearTableAdapter();
private static CollegeTableAdapter _CollegeTableAdapter = new CollegeTableAdapter();
private static view_PlayersTableAdapter _PlayerView = new view_PlayersTableAdapter();
private static view_UniverseCharactersTableAdapter _UniverseCharacterView = new view_UniverseCharactersTableAdapter();
private static UniverseCharacterTableAdapter _UniverseCharacterTableAdapter = new UniverseCharacterTableAdapter();
private static view_ScheduleTableAdapter _ScheduleView = new view_ScheduleTableAdapter();
private static view_YearsTableAdapter _YearView = new view_YearsTableAdapter();
private static View_GameStatsTableAdapter _GameStatsView = new View_GameStatsTableAdapter();
private static view_RegularSeasonTableAdapter _SeasonView = new view_RegularSeasonTableAdapter();
private static view_PlayoffsTableAdapter _PlayoffsView = new view_PlayoffsTableAdapter();
//Keep a tab of all of our ViewModels
private static DataRowView _SelectedUniverse = null;
public static DataRowView SelectedUniverse { get { return _SelectedUniverse; } set { _SelectedUniverse = value; } }
#region Methods
//Navigation
public static void Switch(UserControl newPage)
{
_Window.Navigate(newPage);
}
public static void Switch(UserControl newPage, object state)
{
_Window.Navigate(newPage, state);
}
#endregion
}
任何关于存储全局变量的指针都会非常感激!
答案 0 :(得分:0)
这是我使用MvvmCross做的事情,其中有一个用于存储所选语言的示例(需要全局变量)。我也对版本等使用相同的模式。这是我的SettingsPreferences类的一部分:
public static class Settings
{
public static ISettings AppSettings
{
get
{
return CrossSettings.Current;
}
}
}
设置是nuget包的包装,用于保存内容(支持Android,iOS,Windows Store(Windows Phone 8.1和Windows Store 8.1)和WPF项目):
var lang = SettingsPreferences.SelectedLanguage;
现在我的LanguageViewModel就像这样调用它:
SettingsPreferences.SelectedLanguage = "en";
要更新设置,只需:
template<typename From, typename To>
concept bool ConvertibleNoNarrow = requires(From f, To t) {
t = { f };
};
template<typename T>
class Wrapper {
T t;
public:
Wrapper(ConvertibleNoNarrow<T> u) : t(u) { }
private:
// should be PRIVATE
void operator()() { }
};
int main() {
Wrapper<long> w(1);
// should not be able to call this!
w();
}
它可以是真的。