C# WPF - Accessing another XAML element by code

Asked

Viewed 422 times

0

Hello, I have two screens a Window and a Page loaded inside the screen

Class Window1{
   //to do ..
 __mainFrame; // acessa normal
}

Class page1{
 //to do
__mainFrame; // sem  acesso ao frame
}

Inside the window class I have a <Frame x:Name="__mainFrame"></frame> where I press the pages and wish when clicked on a button that is inside page1 was loaded in this iframe as the two are in different classes I am not able to load, would have some way to make this access ?

Note, using the extend of one class inside the other does not work As in the example below

Class window1 : page1{

}

2 answers

1

The way you did it solves the problem, but in an environment with multiple classes and multiple levels of objects you will have reference problems. Outside the coupling between the classes that will only grow, if you need to change the name __mainframe at some point, you will have to change several files. Anyway, the maintenance will become extremely complex.

A better solution (there are others, for sure), is for you to create an Action type property on your page1, assign a function to it on your Windows1, and then run on page1 when you need it.

Example:

Class Window1{

    construtor(){
        // crie uma instancia de page1 ou acesse uma já existente, como exemplo, vou criar
        var page1 = new page1();
        page1.FazerAlgumaCoisa = () => {
            __mainFrame; // aqui você tem acesso ao objeto __mainFrame para fazer o que quiser      
        }
    }  
    __mainFrame; // acessa normal
}

Class page1{
    public Action FazerAlgumaCoisa { get; set;}

    public void ExecutarAcaoNoMainFrame(){

        this.FazerAlgumaCoisa?.Invoke();

    }
}

You can also pass parameters if necessary, just change your Action property, as in the example below where I pass a string.

Class Window1{

    construtor(){
        // crie uma instancia de page1 ou acesse uma já existente, como exemplo, vou criar
        var page1 = new page1();
        page1.FazerAlgumaCoisa = (parametro1) => {
            __mainFrame; // aqui você tem acesso ao objeto __mainFrame para fazer o que quiser      
//parametro1 pode ser usada aqui
        }
    }  
    __mainFrame; // acessa normal
}

Class page1{
    public Action<string> FazerAlgumaCoisa {get;set;}

    public void ExecutarAcaoNoMainFrame(){

        this.FazerAlgumaCoisa?.Invoke("qualquer valor");

    }
}
  • thanks for the answer, now I’m doing in a slightly different way using an instance controller class so I evetia that there are eventual Crashing by files not integrated so it gets public Static Tipopage Nomecess; then I make the check if the file is instantiated and I call for it

0


Well I got the way to do it here

Class page1{

((Window1)System.Windows.Application.Current.MainWindow).__mainFrame;

}

Where Window is the name of my class Window1 which references the xmal I want to access and Mainwindow is the x:Name of the window element

this way you can access any external element to its class

Browser other questions tagged

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