简体   繁体   English

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

[英]Pass parameter value from one function to another in C#

The following function contains a navigation parameter that represents a value passed from the previous page 以下函数包含一个导航参数,该参数表示从上一页传递的值

 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 is a class that contains the items to always be passed when navigating between pages. QuizArgs是一个类,其中包含在页面之间导航时始终传递的项目。

class QuizArgs
{
    public string sector;

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

}

I want to use the Object navigationParameter value from the LoadState function in the click function. 我想在click函数中使用LoadState函数中的Object navigationParameter值。

I want to assign sector to the value in the navigation parameter instead of string "nav". 我想将sector分配给导航参数中的值,而不是字符串“ nav”。

How do I do use args.sector in the click function? 如何在点击功能中使用args.sector

Check this: 检查一下:

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


        }
    }

您可以将args声明为global。然后两个函数都可以使用args。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM