在C#中将参数值从一个函数传递到另一个函数

时间:2013-11-18 16:22:58

标签: c# visual-studio-2012 windows-8 parameters parameter-passing

以下函数包含一个导航参数,该参数表示从上一页传递的值

 protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {

            QuizArgs args = navigationParameter as QuizArgs;
            var SectorGroups = QuizDataSource.GetGroups(args.sector);
            this.DefaultViewModel["Groups"] = SectorGroups;
        }

        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {

             var itemId = ((QuizGroups)e.ClickedItem).UniqueId;
            var sectorId = new QuizArgs
            {
                sector = "nav",

                question = 2,
                Total = 0,
                type=itemId
            };


            this.Frame.Navigate(typeof(Quiz), sectorId);


        }

QuizArgs是一个类,它包含在页面之间导航时始终传递的项目。

class QuizArgs
{
    public string sector;

    public int Total;
    public int question;
    public string type;

}

我想在点击功能中使用Object navigationParameter函数中的LoadState值。

我想将sector分配给导航参数中的值,而不是字符串“nav”。

如何在点击功能中使用args.sector

2 个答案:

答案 0 :(得分:1)

检查一下:

    public class YourClass: YourBaseClass
    {
        QuizArgs args = null;
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {

            args = navigationParameter as QuizArgs;
            var SectorGroups = QuizDataSource.GetGroups(args.sector);
            this.DefaultViewModel["Groups"] = SectorGroups;
        }

        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
             if(args == null)
                 throw new NullReferenceException();

             var itemId = args.UniqueId;
             var sectorId = new QuizArgs
             {
                 sector = "nav",
                 question = 2,
                 Total = 0,
                 type=itemId
             };


             this.Frame.Navigate(typeof(Quiz), sectorId);


        }
    }

答案 1 :(得分:0)

您可以将args声明为global.Then应该可以在两个函数中访问args。