Return object between screens - c#

Asked

Viewed 94 times

-2

I have a question about returning an object from a screen B to a screen A, for example.

I use the form below, hiding the ShowDialog of the base class, making the "new Showdialog" return what I need. Follow code example of the screen class that will return the object:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //trato meu objeto
        Close();
    }

    public new tipo_meu_objeto ShowDialog()
    {
        base.ShowDialog();
        return meu_objeto;
    }

I also know, through other questions already asked here, that there are other ways, such as using the instance of the screen created and accessing the variable through it.

My question is: What I exemplified is considered a good way to solve this? If not, what would be the best way to treat this case?

Thanks in advance.

1 answer

-1

The best way to solve your problem would be to add a new property in your class that is called, and depending on the DialogResult informed, whether or not there is content. Example:

public class JanelaDialogo : Window
{
    public object Resultado { get; private set; }

    private void OnClick_Sucesso(object sender, RoutedEventArgs e)
    {
        // este será o valor de retorno do método ShowDialog()
        DialogResult = true;

        Resultado = "Sucesso";

        Close();
    }    
}

Utilizing:

private void OnDialogo(object sender, RoutedEventArgs e)
{
    var win = new JanelaDialogo();

    if (win.ShowDialog() == true)
    {
        var resultado = win.Resultado;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.