ResourceDictionary:
<locator:ConstructionLocator x:Key="Construction"/>
addEngineerView.xaml
<Page DataContext="{Binding Source={StaticResource Construction}, Path=AddEngineerViewModel}"/>
Locator:
public class ConstructionLocator
{
private static IDialogServices _dialog = new DialogServices();
private static IAddEngineerDataService _addEngineerDataServices = new AddEngineerDataServices(new AddEngineerData());
private static AddEngineerViewModel _addEngineerViewModel = new AddEngineerViewModel(_addEngineerDataServices, _dialog);
private static ViewEmployeeViewModel _viewEmployeeViewModel = new ViewEmployeeViewModel(_addEngineerDataServices, _dialog);
public static AddEngineerViewModel AddEngineerViewModel
{
get { return _addEngineerViewModel; }
}
public static ViewEmployeeViewModel ViewEmployeeViewModel
{
get { return _viewEmployeeViewModel; }
}
}
ViewModel:
public class AddEngineerViewModel:BindableBase
{
IAddEngineerDataService _engineerDataService;
IDialogServices _dialog;
public AddEngineerViewModel(IAddEngineerDataService AddEngineerDataService,IDialogServices Dialog)
{
_engineerDataService = AddEngineerDataService;
_dialog = Dialog;
LoadCommand();
}
private AddEngineerModel _addEngineer;
public AddEngineerModel AddEngineer
{
get { return _addEngineer; }
set
{
_addEngineer = value;
RaisePropertyChanged("AddEngineer");
}
}
public ICommand AddEngineerCommand { get; set; }
public ICommand ViewEngineerCommand { get; set; }
private void LoadCommand()
{
AddEngineerCommand = new CustomCommand(Add, CanAdd);
ViewEngineerCommand = new CustomCommand(Views, CanView);
}
private void Views(object obj)
{
_dialog.ShowDialog();
}
private bool CanView(object obj)
{
return true;
}
private bool CanAdd(object obj)
{
return true;
}
private void Add(object obj)
{
_engineerDataService.Add_Engineer_Details(_addEngineer);
}
}
When navigate to view on button click it shows Type initializer threw exception in locator file of addengineerviewmodel static property. Above are my locator and viewmodel. what would be the problem?
Related
How can I make sure that as soon as there is a change in the text field, the save button is enabled? or clicking edit (thus creating the button) the app enables editing and thus enabling the button,
and it has already been activated in case of insertion or deletion.
this a image of
app
Here is the code:
public GerenciarGenericoViewModel()
{
InserirProdutoCommand = new RelayCommand(InserirProduto);
ExcluirProdutoCommand = new RelayCommand(ExcluirProduto, CanExcluirProduto);
InserirUnidadeCommand = new RelayCommand(InserirUnidade);
ExcluirUnidadeCommand = new RelayCommand(ExcluirUnidade, CanExcluirUnidade);
InserirEmpresaCommand = new RelayCommand(InserirEmpresa);
ExcluirEmpresaCommand = new RelayCommand(ExcluirEmpresa, CanExcluirEmpresa);
InserirOperadorCommand = new RelayCommand(InserirOperador);
ExcluirOperadorCommand = new RelayCommand(ExcluirOperador, CanExcluirOperador);
SalvarCommand = new RelayCommand(Salvar, CanSalvar);
CancelarCommand = new RelayCommand(Cancelar);
ListaProdutos = new ObservableCollection<Produto>(ProdutosAccess.GetProdutos());
ListaUnidades = new ObservableCollection<UnidadeMedida>(ProdutosAccess.GetUnidadesMedida());
ListaEmpresas = new ObservableCollection<Empresa>(ProdutosAccess.GetEmpresas());
ListaOperadores = new ObservableCollection<Operador>(ProdutosAccess.GetOperadores());
}
public RelayCommand InserirProdutoCommand { get; }
public RelayCommand ExcluirProdutoCommand { get; }
public RelayCommand InserirUnidadeCommand { get; }
public RelayCommand ExcluirUnidadeCommand { get; }
public RelayCommand InserirEmpresaCommand { get; }
public RelayCommand ExcluirEmpresaCommand { get; }
public RelayCommand InserirOperadorCommand { get; }
public RelayCommand ExcluirOperadorCommand { get; }
int check = 0;
public RelayCommand SalvarCommand { get; }
public RelayCommand CancelarCommand { get; }
public ObservableCollection<Produto> ListaProdutos { get; }
public ObservableCollection<UnidadeMedida> ListaUnidades { get; }
public ObservableCollection<Empresa> ListaEmpresas { get; }
public ObservableCollection<Operador> ListaOperadores { get; }
private Produto _produtoSelecionado;
public Produto ProdutoSelecionado
{
get => _produtoSelecionado;
set => Set(() => ProdutoSelecionado, ref _produtoSelecionado, value);
}
private UnidadeMedida _unidadeSelecionada;
public UnidadeMedida UnidadeSelecionada
{
get => _unidadeSelecionada;
set => Set(() => UnidadeSelecionada, ref _unidadeSelecionada, value);
}
private Empresa _empresaSelecionada;
public Empresa EmpresaSelecionada
{
get => _empresaSelecionada;
set => Set(() => EmpresaSelecionada, ref _empresaSelecionada, value);
}
private Operador _operadorSelecionado;
public Operador OperadorSelecionado
{
get => _operadorSelecionado;
set => Set(() => OperadorSelecionado, ref _operadorSelecionado, value);
}
public void InserirProduto()
{
ListaProdutos.Add(new Produto());
check++;
}
public void ExcluirProduto()
{
ListaProdutos.Remove(ProdutoSelecionado);
check++;
}
public bool CanExcluirProduto()
{
return ProdutoSelecionado != null;
}
public void InserirUnidade()
{
ListaUnidades.Add(new UnidadeMedida());
check++;
}
public void ExcluirUnidade()
{
ListaUnidades.Remove(UnidadeSelecionada);
check++;
}
public bool CanExcluirUnidade()
{
return UnidadeSelecionada != null;
}
public void InserirEmpresa()
{
ListaEmpresas.Add(new Empresa());
check++;
}
public void ExcluirEmpresa()
{
ListaEmpresas.Remove(EmpresaSelecionada);
check++;
}
public bool CanExcluirEmpresa()
{
return EmpresaSelecionada != null;
}
public void InserirOperador()
{
ListaOperadores.Add(new Operador());
check++;
}
public void ExcluirOperador()
{
ListaOperadores.Remove(OperadorSelecionado);
check++;
}
public bool CanExcluirOperador()
{
return OperadorSelecionado != null;
}
public void Salvar()
{
var listaProdutos = ListaProdutos.ToList();
var listaUnidades = ListaUnidades.ToList();
var listaEmpresas = ListaEmpresas.ToList();
var listaOperadores = ListaOperadores.ToList();
listaProdutos = listaProdutos.Where(x => !string.IsNullOrWhiteSpace(x.Descricao)).ToList();
listaUnidades = listaUnidades.Where(x => !string.IsNullOrWhiteSpace(x.Descricao)).ToList();
listaEmpresas = listaEmpresas.Where(x => !string.IsNullOrWhiteSpace(x.Descricao)).ToList();
listaOperadores = listaOperadores.Where(x => !string.IsNullOrWhiteSpace(x.Nome)).ToList();
ProdutosAccess.SetListProdutos(listaProdutos);
ProdutosAccess.SetListUnidades(listaUnidades);
ProdutosAccess.SetListEmpresas(listaEmpresas);
ProdutosAccess.SetListOperadores(listaOperadores);
MessageBox.Show(messageBoxText: "Dados salvos com sucesso!",
caption: "Gerenciar Dados", button: MessageBoxButton.OK,
icon: MessageBoxImage.Information);
Sair();
}
public bool CanSalvar()
{
if (check == 0)
{
return false;
}
else
{
return true;
}
}
public void Cancelar()
{
Sair();
}
public void Sair()
{
Navigator.CloseWindowByDataContext(this);
}
}
As #BradleDotNET mentioned just set the UpdateSourceTrigger inside your TextBox Text binding to PropertyChanged and it should work.
I have a MVVM program with a model:
public class Deelnemer
{
public int Id { get; set; }
public string Voornaam { get; set; }
public string Werkplek { get; set; }
public List<string> Aanwezig { get; set; }
public Deelnemer()
{
}
}
In my View I have a listBox in which I want to be able to select multiple values (days to put in the list aanwezig).
<ListBox Name="listDagdelen" SelectionMode="Multiple" ItemsSource="{Binding Dagdelen}" SelectedItem="{Binding SelectedDagdeel, Mode=TwoWay}">
The ViewModel looks as follows:
class DeelnemerViewModel : INotifyPropertyChanged
{
#region Private Variables
private readonly Deelnemer dlnObject;
private readonly ObservableCollection<Deelnemer> deelnemers;
private readonly DeelnemerManager deelnemerManager;
private readonly ICommand addDeelnemerCmd;
private readonly ICommand deleteDeelnemerCmd;
#endregion
public ObservableCollection<string> Dagdelen { get; private set; }
#region constructor
public DeelnemerViewModel()
{
Dagdelen = new ObservableCollection<string>() { "maandagochtend", "maandagmiddag", "dinsdagochtend", "dinsdagmiddag", "woensdagochtend", "woensdagmiddag", "donderdagochtend", "donderdagmiddag", "vrijdagochtend", "vrijdagmiddag" };
dlnObject = new Deelnemer();
deelnemerManager = new DeelnemerManager();
deelnemers = new ObservableCollection<Deelnemer>();
addDeelnemerCmd = new RelayCommand(Add, CanAdd);
deleteDeelnemerCmd = new RelayCommand(Delete, CanDelete);
}
#endregion
#region Properties
private string _selectedDagdeel = null;
public string SelectedDagdeel
{
get { return _selectedDagdeel; }
set
{
_selectedDagdeel = value;
dlnObject.Aanwezig.Add(value);
OnPropertyChanged("SelectedDagdeel");
}
}
public int Id
{
get { return dlnObject.Id; }
set
{
dlnObject.Id = value;
OnPropertyChanged("Id");
}
}
public string Voornaam
{
get { return dlnObject.Voornaam; }
set
{
dlnObject.Voornaam = value;
OnPropertyChanged("Voornaam");
}
}
public string Werkplek
{
get { return dlnObject.Werkplek; }
set
{
dlnObject.Werkplek = value;
OnPropertyChanged("Werkplek");
}
}
public List<string> Aanwezig
{
get { return dlnObject.Aanwezig; }
set
{
dlnObject.Aanwezig = value;
OnPropertyChanged("Aanwezig");
}
}
public ObservableCollection<Deelnemer> Deelnemers { get { return deelnemers; } }
public Deelnemer SelectedDeelnemer
{
set
{
Id = value.Id;
Voornaam = value.Voornaam;
Werkplek = value.Werkplek;
Aanwezig = value.Aanwezig;
}
}
#endregion
#region Commands
public ICommand AddDeelnemerCmd { get { return addDeelnemerCmd; } }
public ICommand DeleteDeelnemerCmd { get { return deleteDeelnemerCmd; } }
#endregion
public bool CanAdd(object obj)
{
//Enable the Button only if the mandatory fields are filled
if (Voornaam != string.Empty && Werkplek != string.Empty)
return true;
return false;
}
public void Add(object obj)
{
var deelnemer = new Deelnemer { Voornaam = Voornaam, Werkplek = Werkplek, Aanwezig = Aanwezig };
if (deelnemerManager.Add(deelnemer))
{
Deelnemers.Add(deelnemer);
//string txt = string.Join(String.Empty,Aanwezig);
//MessageBox.Show(txt);
//ResetDeelnemer();
}
else
MessageBox.Show("Vul correcte waardes in!");
}
#region DeleteCommand
private bool CanDelete(object obj)
{
//Enable the Button only if the patients exist
if (Deelnemers.Count > 0)
return true;
return false;
}
private void Delete(object obj)
{
//Delete patient will be successfull only if the patient with this ID exists.
if (!deelnemerManager.Remove(Id))
MessageBox.Show("Deelnemer met dit id bestaat niet!");
else
{
//Remove the patient from our collection as well.
deelnemers.RemoveAt(GetIndex(Id));
ResetDeelnemer();
MessageBox.Show("Deelnemer succesvol verwijderd !");
}
}
#endregion
#region Private Methods
private void ResetDeelnemer()
{
Id = 0;
Voornaam = string.Empty;
Werkplek = string.Empty;
Aanwezig.Clear();
}
private int GetIndex(int Id)
{
for (int i = 0; i < Deelnemers.Count; i++)
if (Deelnemers[i].Id == Id)
return i;
return -1;
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
I can't figure out how I should use the List with the listbox values. How do I add (multiple) listbox values to the object's List?
The current code throws a nullreferenceexception at
dlnObject.Aanwezig.Add(value);
You must initialize the Aanwezig property of the Deelnemer object before you can add any values to it, either in the contructor of DeelnemerViewModel:
dlnObject = new Deelnemer();
dlnObject.Aanwezig = new List<string();
...or in the constructor of the Deeelnemer class:
public Deelnemer()
{
Aanwezig = new List<string();
}
Im using the MVVMCross 4.4.0 and my FirstView is a List View like WhatsApp. When I click an item, my SecondView is a common view with just a field to update a property and change my FirstView, but this is not happen.
My First View Code
public sealed partial class MyOrdersView : MvxViewController
{
UITableView tableView;
public MyOrdersView() : base("MyOrdersView", null)
{
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
tableView = new UITableView
{
Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, View.Bounds.Height),
CellLayoutMarginsFollowReadableWidth = true,
};
Add(tableView);
var tableSource = new MvxSimpleTableViewSource(tableView, OrderCell.Key, OrderCell.Key);
tableView.Source = tableSource;
var set = this.CreateBindingSet<MyOrdersView, Core.ViewModels.MyOrdersViewModel>();
set.Bind(tableSource).To(vm => vm.Tasks);
set.Bind(tableSource).For(s => s.SelectionChangedCommand).To(vm => vm.ItemSelectedCommand);
set.Apply();
tableView.ReloadData();
Title = "Meus Pedidos";
AppDelegate app = UIApplication.SharedApplication.Delegate as AppDelegate;
NavigationItem.SetLeftBarButtonItem(
new UIBarButtonItem(UIImage.FromBundle("threelines")
, UIBarButtonItemStyle.Plain
, (sender, args) => app.SidebarController.ToggleMenu()), true);
}
}
And ViewModel
public class MyOrdersViewModel : BaseViewModel
{
private IOrderService orderService;
public ObservableCollection<ValOrders2TO> tasks;
private MvxCommand<ValOrders2TO> _itemSelectedCommand;
public MyOrdersViewModel(IOrderService orderService)
{
this.orderService = orderService;
tasks = orderService.getAvailableOrders();
searchMyOrders();
}
private async Task<bool> searchMyOrders()
{
orderService.clearAvailableOrders();
orderService.addAllAvailableOrders(await orderService.searchMyOrders());
return true;
}
public ObservableCollection<ValOrders2TO> Tasks
{
get { return tasks; }
set { tasks = value; RaisePropertyChanged(() => Tasks); }
}
public ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<ValOrders2TO>(DoSelectItem);
return _itemSelectedCommand;
}
}
private void DoSelectItem(ValOrders2TO item)
{
ShowViewModel<MyOrdersDetailModel>(new { idOrder = item.id });
}
}
My Second View Code
public sealed partial class MyOrdersDetailView : MvxViewController<MyOrdersDetailModel>
{
public MyOrdersDetailView() : base("MyOrdersDetailView", null)
{
//ViewDidLoad();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var btnBack = UIButton.FromType(UIButtonType.System);
btnBack.Frame = new CGRect(2, 35, 50, 20);
btnBack.SetTitle("Voltar", UIControlState.Normal);
Add(btnBack);
var set = this.CreateBindingSet<MyOrdersDetailView, Core.ViewModels.MyOrdersDetailModel>();
set.Bind(txtItemDes).To(vm => vm.txtItemDes);
set.Bind(btnUpdate).To(vm => vm.update);
set.Apply();
}
}
And the ViewModel
public class MyOrdersDetailModel : MvxViewModel
{
public ValOrders2TO _valItem;
public INC<string> txtItemDes = new NC<string>();
public MyOrdersDetailModel(IMessageService messageService, IOrderService orderService)
{
}
public ValOrders2TO Item
{
get { return _valItem; }
set { _valItem = value; RaisePropertyChanged(() => Item); }
}
public void Init(ValOrders2TO item)
{
Item = item;
ConfigProp();
}
void ConfigProp()
{
txtItemDes.Value = Item.itemsDes;
}
public IMvxCommand Update
{
get
{
return new MvxCommand(() => updateOrder());
}
}
private bool updateOrder()
{
Item.itemsDes = txtItemDes.Value;
return true;
}
public IMvxCommand CloseCommand
{
get { return new MvxCommand(() => Close(this)); }
}
}
This is my Bean Code
public class ValOrders2TO
{
//[JsonProperty()]
public int id { get; set; }
//[JsonProperty()]
public string itemsDes { get; set; }
}
And this is my OrderCell
public partial class OrderCell : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName("OrderCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString("OrderCell");
private readonly MvxImageViewLoader _loader;
public OrderCell(IntPtr handle) : base(handle)
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<OrderCell, Core.ValOrders2TO>();
set.Bind(lblItemDes).To(item => item.itemsDes);
set.Apply();
});
}
public static OrderCell Create()
{
return (OrderCell)Nib.Instantiate(null, null)[0];
}
}
I know how to remove the items from my ListView like this:
orderService.deleteAvailableOrder(Item);
But I don't know how to update the items list.
Following attached my print screens
Try to implement RaisePropertyChanged for
public string itemsDes { get; set; }
I have a MvxDialogFragment and I want to show that using ViewModel, similarly when a activity is showed from ViewModel using ShowViewModel. Is it possible? How can I do that?
This is my scenario: I have a page that contains a MvxListView and, when the user click in a listview item, I want to show a MvxDialogFragment.
My ListView layout:
<Mvx.MvxListView
android:id="#+id/lstViewTasks"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:clickable="true"
android:focusableInTouchMode="true"
android:choiceMode="multipleChoice"
android:layout_alignParentTop="true"
local:MvxBind="ItemsSource Tasks; ItemClick ItemClickCommand"
local:MvxItemTemplate="#layout/projectmytasksitem" />
My ListView ViewModel:
public class ProjectMyTasksViewModel : MvxViewModel
{
#region [Atributos privados]
private ProjectService _service;
#endregion
#region [Propriedades]
private IList<Task> _tasks;
public IList<Task> Tasks
{
get { return _tasks; }
set { _tasks = value; RaisePropertyChanged(() => Tasks); }
}
private bool _isListaVazia;
public bool IsListaVazia
{
get { return _isListaVazia; }
set { _isListaVazia = value; RaisePropertyChanged(() => IsListaVazia); }
}
private Task _selectedTask;
public Task SelectedTask
{
get { return _selectedTask; }
set { _selectedTask = value; RaisePropertyChanged(() => SelectedTask); }
}
private string _mensagemErro;
public string MensagemErro
{
get { return _mensagemErro; }
set { _mensagemErro = value; RaisePropertyChanged(() => MensagemErro); }
}
#endregion
#region [Commands]
private IMvxCommand _itenClickCommand;
public IMvxCommand ItemClickCommand
{
get
{
this._itenClickCommand = this._itenClickCommand ?? new MvxCommand<Task>(this.ExecuteItemClickCommand);
return _itenClickCommand;
}
}
#endregion
#region [Construtores]
public ProjectMyTasksViewModel()
{
_service = new ProjectService();
this.CriaListaTeste();
IsListaVazia = (Tasks.Count > 0) ? true : false;
}
#endregion
#region [Execuções dos Comandos]
private void ExecuteItemClickCommand(Task task)
{
Dictionary<string, Task> parametros = new Dictionary<string, Task>()
{
{"Task", task }
};
this.ShowViewModel<TaskViewModel>(parametros);
}
#endregion
#region [Métodos]
public void CriaListaTeste()
{
Tasks = new List<Task>();
for (int indiceProjeto = 1; indiceProjeto <= 10; indiceProjeto++)
{
Tasks.Add(new Task { Name = $"Tarefa {indiceProjeto}", StartDate = DateTime.Now, FinishDate = DateTime.Now, IsCompleted = false });
}
}
#endregion
}
My Listview Activity:
[Activity(Label = "My Task",
ConfigurationChanges = ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait)]
public class ProjectMyTasksView : MvxActivity
{
public new ProjectMyTasksViewModel viewModel
{
get { return (ProjectMyTasksViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.SetContentView(Resource.Layout.ProjectMyTasksView);
}
}
My Dialog Fragment:
public class ProjectMyTaskDialog : MvxDialogFragment<TaskViewModel>
{
public override Dialog OnCreateDialog(Bundle savedState)
{
base.EnsureBindingContextSet(savedState);
this.BindingInflate(Resource.Layout.ProjectMyTasksDialog, null);
return base.OnCreateDialog(savedState);
}
}
My Dialog Fragment ViewModel:
public class TaskViewModel : BaseViewModel<TaskService, Task>
{
#region [Atributos Privados]
private readonly IMvxPictureChooserTask _pictureChooserTask;
#endregion
#region [Commands]
private MvxCommand _chooseImageCommand;
public MvxCommand ChooseImageCommand
{
get
{
_chooseImageCommand = _chooseImageCommand ?? new MvxCommand(ExecuteChooseImageCommand);
return _chooseImageCommand;
}
}
private MvxCommand _takePictureCommand;
public MvxCommand TakePictureCommand
{
get
{
_takePictureCommand = _takePictureCommand ?? new MvxCommand(ExecuteTakePictureCommand);
return _takePictureCommand;
}
}
private MvxCommand _completeTaskCommand;
public MvxCommand CompleteTaskCommand
{
get
{
_completeTaskCommand = _completeTaskCommand ?? new MvxCommand(ExecuteCompleteTaskCommand);
return _completeTaskCommand;
}
}
#endregion
#region [Construtores]
public TaskViewModel(IMvxPictureChooserTask pictureChooserTask)
{
_pictureChooserTask = pictureChooserTask;
}
#endregion
#region [Execuções dos Comandos]
private void ExecuteChooseImageCommand()
{
_pictureChooserTask.ChoosePictureFromLibrary(400, 95, OnImage, () => { });
}
private void ExecuteTakePictureCommand()
{
_pictureChooserTask.TakePicture(400, 95, OnImage, () => { });
}
public void ExecuteCompleteTaskCommand()
{
throw new NotImplementedException();
}
#endregion
#region [Métodos]
public void Init(Dictionary<string, Task> parametros)
{
Objeto = parametros["Task"];
}
private void OnImage(Stream imageStream)
{
var memoryStream = new MemoryStream();
imageStream.CopyTo(memoryStream);
Objeto.Image = memoryStream.ToArray();
}
#endregion
public class Parametros
{
public string TaskJson { get; set; }
}
}
There is no presenter for DialogFragments.
Instead you could have instantiate the Command from the Activity or have a callback into the Activity which determines what to do when the command is called, i.e. displaying the Dialog.
So for a simple example:
public class MyViewModel : MvxViewModel
{
public Action ShowTaskCommandAction {get;set;}
private MvxCommand _showTaskCommand;
public ICommand ShowTaskCommand =>
_showTaskCommand = _showTaskCommand ?? (_showTaskCommand = new MvxCommand(DoShowTaskCommand));
private void DoShowTaskCommand()
{
CommandAction?.Invoke();
// do other stuff here...
}
}
Then in your Activity or Fragment:
public class MyActivity : MvxActivity<MyViewModel>
{
public void OnCreate(Bundle _)
{
base.OnCreate(_);
ViewModel.ShowTaskCommandAction = () => {
var dialogFragment = new ProjectMyTaskDialog() {
DataContext = task
});
dialogFragment.Show(FragmentManager);
};
// whatever else code
}
}
I am a beginner to C# programming.
When I run this program, the eventhandler named - newMessagePublishedEventHandler of the class MessagePool is always null while executing the OnNewMessagePublished() function of the same class.
Can anyone please tell me why? and also how could I resolve this?
namespace ConsoleApp
{
public class MessageEventArgs : System.EventArgs
{
private int publisherId;
private string message;
public MessageEventArgs(int publisherId, string messageText)
{
this.publisherId = publisherId;
this.message = messageText;
}
public int PublisherId
{
get { return publisherId; }
}
public string Message
{
get { return message; }
}
}
public class Publisher
{
private int publisherId;
public Publisher(int publisherId)
{
this.publisherId = publisherId;
}
public int PublisherId
{
get { return publisherId; }
}
public void Publish(string message)
{
MessagePool.GetInstance().Publisher_PostMessage(this.publisherId, message);
Console.WriteLine("published message - " + message);
}
}
public class MessagePool
{
private static MessagePool theOnlyInstanceOfMessageBroker;
public static MessagePool GetInstance()
{
if (theOnlyInstanceOfMessageBroker == null)
{
return new MessagePool();
}
else
{
return theOnlyInstanceOfMessageBroker;
}
}
private MessagePool()
{
}
private EventHandler newMessagePublishedEventHandler;
public event EventHandler NewMessagePublished
{
add
{
newMessagePublishedEventHandler += value;
}
remove
{
newMessagePublishedEventHandler -= value;
}
}
public void Publisher_PostMessage(int publisherId, string message)
{
DateTime publishedTime = DateTime.Now;
this.OnNewMessagePublished(publisherId, message);
}
private void OnNewMessagePublished(int publisherId, string message)
{
MessageEventArgs eventArgs = new MessageEventArgs(publisherId, message);
if (newMessagePublishedEventHandler != null)
{
newMessagePublishedEventHandler(this, eventArgs);
}
}
}
public class Subscriber
{
private int subscriberId;
public Subscriber(int subscriberId)
{
this.subscriberId = subscriberId;
this.SubscribeToMessagebroker();
}
public int SubscriberId
{
get { return subscriberId; }
}
private void SubscribeToMessagebroker()
{
MessagePool.GetInstance().NewMessagePublished -= Subscriber_NewMessageArrived;
MessagePool.GetInstance().NewMessagePublished += Subscriber_NewMessageArrived;
}
private void Subscriber_NewMessageArrived(object sender, EventArgs eventArgs)
{
if (eventArgs != null && eventArgs is MessageEventArgs)
{
var data = eventArgs as MessageEventArgs;
if (data != null)
Console.WriteLine("Recieved message : '" + data.Message + "' from " + data.PublisherId);
}
}
}
class Program
{
static void Main(string[] args)
{
Subscriber subscriber = new Subscriber(1);
Publisher publisher = new Publisher(1001);
publisher.Publish("Hey man.. whats up?");
Console.ReadLine();
}
}
}
MessagePool is not a proper Singleton, you are always returning a new MessagePool.
public class MessagePool
{
private static MessagePool theOnlyInstanceOfMessageBroker;
private static object _syncRoot = new object();
public static MessagePool GetInstance()
{
if (theOnlyInstanceOfMessageBroker == null)
{
lock(_syncRoot)
{
if (theOnlyInstanceOfMessageBroker == null)
theOnlyInstanceOfMessageBroker = new MessagePool();
}
}
return theOnlyInstanceOfMessageBroker;
}
private MessagePool()
{
}
//...
}