Return object between screens - C #

-2

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

I use the form below, hiding the ShowDialog of the base class, making the "new ShowDialog" return what I need. Here is a 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 created screen instance 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 handle this?

Thank you in advance.

    
asked by anonymous 23.08.2018 / 13:46

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 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();
    }    
}

Usage:

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

    if (win.ShowDialog() == true)
    {
        var resultado = win.Resultado;
    }
}
    
23.08.2018 / 14:16