i am new in mvvm cross native android C# so i need menu item color change i had to multiple view model then any view model i will call some method menu item color change how can i do
Loading
i am new in mvvm cross native android C# so i need menu item color change i had to multiple view model then any view model i will call some method menu item color change how can i do
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Feb 23, 2024, 11:44 PM EST
In the MVVM (Model-View-ViewModel) architecture for Xamarin.Android using MvvmCross, you can achieve the menu item color change by having a shared ViewModel or by using a messaging system to communicate between different ViewModels. Below are the steps you can follow:
Shared ViewModel:
Create a Shared ViewModel:
public class SharedViewModel : MvxViewModel { private MvxColor _menuItemColor; public MvxColor MenuItemColor { get => _menuItemColor; set { SetProperty(ref _menuItemColor, value); RaisePropertyChanged(() => MenuItemColor); } } }Initialize the ViewModel:
Setup.csfile or at the beginning of your application.protected override IMvxApplication CreateApp() { Mvx.IoCProvider.RegisterSingleton(() => new SharedViewModel()); // ... other setup code return new App(); }Use the Shared ViewModel in Other ViewModels:
public class YourViewModel : MvxViewModel { private readonly SharedViewModel _sharedViewModel; public YourViewModel(SharedViewModel sharedViewModel) { _sharedViewModel = sharedViewModel; } // Your ViewModel code }Change MenuItem Color:
MenuItemColorproperty of the shared ViewModel._sharedViewModel.MenuItemColor = MvxColor.Parse("#FF0000"); // Set your desired colorMessaging System (using MvxMessenger):
Define a Message:
public class ChangeMenuItemColorMessage : MvxMessage { public MvxColor NewColor { get; } public ChangeMenuItemColorMessage(object sender, MvxColor newColor) : base(sender) { NewColor = newColor; } }In the ViewModel where you want to react to the color change, subscribe to the message.
var messenger = Mvx.IoCProvider.Resolve(); messenger.Subscribe(OnMenuItemColorChanged); private void OnMenuItemColorChanged(ChangeMenuItemColorMessage message) { // Update your menu item color based on message.NewColor }The shared ViewModel approach may be more straightforward for simple scenarios, while the messaging system allows for more loose coupling between components.