Frage

Im using a pivot control with a variable appbar. As I see, the appbar is linked to a resource in app.xaml.cs where you put the info of the buttons and methods of it. It looks like this:

<shell:ApplicationBar x:Key="AppBar1" IsVisible="True" IsMenuEnabled="False">
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.places.png" Text="TEXT" Click="ApplicationBarIconButton_Click11" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.calendar.month.png" Text="TEXT" Click="ApplicationBarIconButton_Click12" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.star.png" Text="TEXT" Click="ApplicationBarIconButton_Click13" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.people.multiple.png" Text="TEXT" Click="ApplicationBarIconButton_Click14" />
    </shell:ApplicationBar>

    <shell:ApplicationBar x:Key="AppBar2" IsVisible="True" IsMenuEnabled="False">
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.warning.circle.png" Text="TEXT" Click="ApplicationBarIconButton_Click21" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.qr.png" Text="TEXT" Click="ApplicationBarIconButton_Click22" />
    </shell:ApplicationBar>

    <shell:ApplicationBar x:Key="AppBar3" IsVisible="True" IsMenuEnabled="False">
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.add.png" Text="TEXT" Click="ApplicationBarIconButton_Click31" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.message.png" Text="TEXT" Click="ApplicationBarIconButton_Click32" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.email.png" Text="TEXT" Click="ApplicationBarIconButton_Click33" />
        <shell:ApplicationBarIconButton IconUri="/Images/iconos/appbar.warning.circle.png" Text="TEXT" Click="ApplicationBarIconButton_Click34" />
    </shell:ApplicationBar>

Each of the diferent buttons have a method, actually the one's of them who navigate to another section are working fine:

 private void ApplicationBarIconButton_Click11(object sender, EventArgs e)
    {
        (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/seccmapa/localeslist.xaml", UriKind.Relative));
    }

But now I want one of this buttons call a method in Main page, I tried something like this:

private void ApplicationBarIconButton_Click51(object sender, EventArgs e)
    {
        ((MainPage)App.Current.RootVisual).cambioperfil();
    }

And the method in MainPage is:

public async void cambioperfil()
    {
        objetoslistas.setprofileinput paquete = new objetoslistas.setprofileinput();
        paquete.nickName = nombre.Text;
        paquete.statusMessage = status.Text;
        paquete.isMen = isMen;
        paquete.birthDate = Fecha_nacimiento.Value.ToString();
        if (Visibilidad.IsChecked == true)
        {
            paquete.mapVisibility = true;
        }
        else
        {
            paquete.mapVisibility = false;
        }
        Uri url = new Uri("url");
        string respuesta = await metodosJson.jsonPOST(url, paquete);

    }

But this gives mi and exception:

{System.InvalidCastException: Unable to cast object of type 'Microsoft.Phone.Controls.PhoneApplicationFrame' to type 'X.MainPage' 

Any Idea?

War es hilfreich?

Lösung

Your issue is that the RootVisual is a PhoneApplicationFrame and so can't be cast to a MainPage, which inherits from PhoneApplicationPage.

Instead you should access the Content of the frame.

(MainPage)(((System.Windows.Controls.ContentControl)(App.RootFrame)).Content).cambioperfil();

You'll probably want to add the appropriate checks for when the calling page isn't MainPage though.

Andere Tipps

Here is a generic how to based on the code I needed for an app I am writing. I know the circumstances are slightly different, but it may help someone else who is after a similar solution:

In MainPage.xaml you create a method as follows:

public static void InMainPage()
{
    System.Diagnostics.Debug.WriteLine("Hi I am a method in MainPage.xaml.cs");
}

Now in App.xaml.cs you can call it in any method as such:

MainPage.InMainPage();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top