如何使用一个的MainPage方法从变量appbar支点app.xaml.cs [英] How to use a method in MainPage from app.xaml.cs with variable appbar pivot

查看:191
本文介绍了如何使用一个的MainPage方法从变量appbar支点app.xaml.cs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用带有可变appbar枢轴控件。
依我之见,该appbar被链接到你把按钮和它的方法的信息app.xaml.cs的资源。它看起来是这样的:

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>



每个diferent按钮有一个方法,其实他们的人的谁导航到另一个部分正在罚款:

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

和中的MainPage方法是:

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

    }



但是,这给了MI和异常:

But this gives mi and exception:

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

任何想法?

推荐答案

您的问题是, RootVisual PhoneApplicationFrame ,因此不能被转换为的MainPage ,从的PhoneApplicationPage 。

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

相反,你应该访问内容帧。

(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.

这篇关于如何使用一个的MainPage方法从变量appbar支点app.xaml.cs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆