-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows form how to call forms correctly from container #97
Comments
What you can try is hiding the management around showing (and disposing) your forms behind an abstraction. For instance: public partial class FormMain
{
private IFormManager manager;
public FormMain(IFormManager m) => manager = m;
private void BtnOpen_Click(object sender, EventArgs e)
{
this.manager.ShowDialog<FormUsetsNew>();
}
} Such manager can be implemented as follows: class Manager : IFormManager
{
private Container container;
public Manager(Container c) => container = c;
public void ShowDialog<TForm>()
where TForm : Form
{
using (var form = container.GetInstance<TForm>())
{
form.ShowDialog();
}
}
} I'm sure @TheBigRic can give you a more detailed, and better, answer. |
hmm, and what about if another form is called from FormUserNew so we would have chain like: FormMain (starting form) --> FormUserNew --> FormOther (how handle this form?) I was also thinking about instead forms having their dependencies inside constructor to have dependencies as properties and in constuctor body give default dependency value - what about that? in this case i don;t need to give form;s constructor arguments but still can test in unit tests. What you think? P.S How it's accmomplished in WPF ? If not mistaken there is also main form so same workaround could be done? |
In that case you would inject
I've actually written how to integrate with UWP in my book, which is very similar to WPF. You can read the section about UWP online here. Manning allows you to read part of that section for free, so it might be enough to get started. |
I once wrote an answer on stack overflow with a very similar but more complete
You can use the same
Why? The Windows Forms Designer can handle a non default ctor pretty well. So don't do this, you do not need it I think. |
@TheBigRic I have to more questions: 1. Let's say there is one constructor parameter for some form besides normal dependencies. How can i pass value not using container? Example:
I mean i could do in container staticly : But i would prefer to say in this place dynamically somehow:
We have:
i mean i want myForm to be of type FormUsersNew - the real instance type (not type of Form) |
I've registered my FormUserNew:
_container.Register<FormUsersNew>();
which is called from
FormMain
(starting form), however i am not sure how should i take instance of that inFormMain
then call it.Here's my code below:
FormUsersNew:
How then to call
FormUsersNew
to use my dependencies from container? :The text was updated successfully, but these errors were encountered: