Probably I'm searching for some wrong Keyword, but I couldn't find anything on this matter: I'm tinkering around with WPF MVVM and I'm now at the Point, I'm looking for a solution to give a ViewModel the possibility to request the Container to change to anhother ViewModel.
My approach is to pass a Callback to the created ViewModel, which is held on the Abstract BaseClass and can called, if desired. The method in the Container looks like this:
private void ApplyViewModel<T>()
where T : ViewModelBase
{
_exHandler.HandledAction(
() =>
{
var vm = _viewModelFactory.CreateViewModel<T>();
vm.ApplyViewModel = ApplyViewModel<T>;
CurrentContent = vm;
});
}
The interesting point is the second Line, where I pass the Method itself to the ViewModel.
For this matter, I created also a Delegate:
public delegate void ApplyViewModelDelegate<T>()
where T : ViewModelBase;
The problem lies now in the definition of the Callback in the ViewModelBase: It seems not possible to define it as an open generic Delegate. So I would like to create a Property with the following Signature:
public ApplyViewModelDelegate<T> ApplyViewModel { get; set; }
Is there kindahow a possibility to pass a Delegate as open generic Type, until I'd really like to call it with a specific Type?
Edit:
If I define the Property like this:
public ApplyViewModelDelegate<ViewModelBase> SwitchToViewModel { get; set; }
I can assign the Property from the Container, but my final idea would be to call this Property to target a specific ViewModel. For example, a Command could look like this:
public ViewModelCommand ToTest2
{
get
{
return new ViewModelCommand(
"To Test2",
new ActionCommand(() =>
{
SwitchToViewModel<Test2ViewModel>();
}));
}
}
Related
I'm learning ADO.NET and here is an example of method call from MS Documnetation:
workAdapter.TableMappings.Add("AuthorsMapping", "Authors");
Where workAdapter in an instance of DataAdapter class, TableMappings its property, and Add its method. I have never seen method being invoked in this way. I wasn't able to find an answer here nor in the documentation. Need help understanding this.
Properties and methods are invoked this way a lot. You can chain as many together as you like and it makes sense to do so if you only need to use each one once. Any time you could assign something to a variable and then access a member via that variable, you can access it for what you originally assigned. If you have these types:
public class Thing
{
public Stuff Stuff { get; set; }
}
public class Stuff
{
public void DoSomething()
{
// ...
}
}
then you could do this:
var s = new Stuff();
var t = new Thing { Stuff = s };
s.DoSomething();
t.Stuff.DoSomething();
In those last two lines, s refers to the same object as t.Stuff so you can call the same DoSomething method on both.
I need to respond to the NSStatusBarButton click to show a menu, which unfortunately only has the property "Action" to add a click handler.
The "Action" property requires a selector.
The method I use to handle the click is the following:
[Action("StatusBarClicked:")] public void StatusBarClicked(NSObject sender)
{
// do something
}
And I do the following to assign the button action:
statusBarButton.Action = new Selector("StatusBarClicked:");
Now, it all works when I do this in the DidFinishLaunching method of the app delegate and the StatusBarClicked method is a method of the AppDelegate class.
When I wrap the status bar code in a separate class and declare the method and the selector in that class, it doesn't work (StatusBarClicked is not called).
Strangely enough, if I keep the StatusBarClicked method also in the AppDelegate, that one is called instead:
public class SomeClass
{
public void Test()
{
var statusItem = NSStatusBar
.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Square);
var button = statusItem.Button;
button.Image = NSImage.ImageNamed("test");
button.Action = new Selector("StatusBarClicked:");
}
[Action("StatusBarClicked:")] public void StatusBarClicked(NSObject sender)
{
// never called
}
}
I must be missing something, any idea?
When you set an action the selector is sent to the Target property of the NSControl when the action triggers.
If the target is null the application travels up the responder chain to find the first object that responds to the selector. The app delegate is in the responder chain, that is why that works.
https://developer.apple.com/documentation/appkit/nscontrol/1428885-target?language=objc
If you want to use your custom class as the target I think it needs to subclass NSObject
After you make sure your class extends NSObject you can add this to your constructor:
button.Target = this;
I've inherited some code that implements WPF Commands as follows:
public ICommand pvToggleSelectMapCommand
{
get
{
return new CommandHandler(() => pvToggleSelectMap(), true);
}
}
This is fine without parameters and doesn't use a generic RelayCommand-like class to set up the command handling. I need to put a parameter into this now, and am struggling to find a simple way to handle on using this way of command handling.
Any suggestions?
Usually, when using some form of delegate ICommand, you can just add an object input parameter to get your CommandParameter object. Try this:
public ICommand pvToggleSelectMapCommand
{
get
{
return new CommandHandler((params) => pvToggleSelectMap(params), true);
}
}
...
public void pvToggleSelectMap(object params) { ... }
Of course, this might not work with your CommandHandler class as you didn't provide any information for that here.
I want main viewmodel to have a certain list, and then access from many other viewmodels.
For example, in MainViewModel.cs I will have a list of 50 numbers,
then in NumListViewModel.cs, I'd like to access it in order to show it as a list, and in AddNumViewModel.cs I'd like to be able to update that list.
It's been suggested that I use events / evenaggerator, which I did, but unfortunately, for all I know all I can do with it is send a num from one view to another and tell it to update the list, but the problem is, as the program grows, I will need to have a lot of subscribers in the main view model, and when something actually happens I will have to "publish" events according to the number of subscribers which makes it even harder to maintain.
I also found another answer, instructing to create an instance of anotherVM within the mainVM, with a parameter set to "this" which is a reference to the mainVM.
It works, but then again, it could get quite long.
So my question is, is there a better way to access a property from another VM?
Like literally have the an instance of the class that holds the list in the mainVM, and then just be able to update / access it from the other VMs, without having to explicitly program which VM can. Would make life so much easier.
In your answer, please try to avoid suggesting frameworks.
Although there are some really good ones, I want to be able to do at least that by myself.
For example:
MainVM.cs:
public class MainVM
{
List lst = new List(); //Let's just say it's full...
}
OtherVM.cs:
public class OtherVM
{
lst.Add(3);
}
PS: Yes I know it has been asked already, and yes I have done my research, BUT I the answers I found are too 'static', I guess?
If you want direct access to the list from an external ViewModel, then your options are to:
Pass the List to the OtherVM as a constructor argument or public property. Then the OtherVM can treat it like a member.
Pass the MainVM to the OtherVM as a constructor argument or public property. Then the OtherVM can access the List by first accessing the MainVM.
Example:
public class MainVM
{
public List<XX> MyList { get; set; }
}
public class OtherVM
{
public MainVM TheMainVM { get; set; }
public OtherVM(MainVM theMainVM)
{
TheMainVM = theMainVM;
// Access the MainVM's list
TheMainVM.MyList.Add(stuff);
}
}
Give the MainVM a static property called "Default" or "Instance," so you can access the static instance of MainVM from within OtherVM, without assigning it as a member field.
Example:
public class MainVM
{
private static MainVM _instance = new MainVM();
public static MainVM Instance { get { return _instance; } }
public List<XX> MyList { get; set; }
//other stuff here
}
//From within OtherVM:
MainVM.Instance.MyList.Add(stuff);
I would like to know how to pass two or more parameters to Prism event aggregator (event class inherits CompositePresentationEvent)?
I know I can create wrapper class like EventArgs and create property for each value I need, but I would rather to pass two distinct parameters.
Is this possible?
Unfortunately, the event aggregator is only set up to pass a single parameter. HOWEVER, that parameter can be a class or structure.
Here is an example of a "message" I pass with event aggregator, including a callback parameter.
public class OpenViewPayload
{
public string ViewName;
public object Context;
public Action callback;
}
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class OpenViewEvent : CompositePresentationEvent<OpenViewPayload>
{
}
Usage:
_eventAggregator.GetEvent<OpenViewEvent>().Publish(new OpenViewPayload() { ViewName = "CustomerView", Context = _selectedCustomerID, callback= ()=> { /* Close Current View */ } });