从使用该方法之外的变量的其他类中调用方法 [英] Calling a method from a different class that uses variables outside the method

查看:250
本文介绍了从使用该方法之外的变量的其他类中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我在这样的类中有一个方法:

I have a problem. I have a method in a class like this:

public void UpdateActionBar(int CurrentFragmentNum)
{
    if (CurrentFragmentNum == 1)
    {
        btnBack.Visibility = ViewStates.Invisible;
        btnNext.Visibility = ViewStates.Invisible;
    }
    else
    {
        btnBack.Visibility = ViewStates.Visible;
        btnNext.Visibility = ViewStates.Visible;
    }

    if (CurrentFragmentNum == 3)
    {
        btnNext.Text = "Finish";
    }
    else
    {
        btnNext.Text = "Next";
    }
}

现在在另一个类中,我这样调用此方法:

Now in another class I call this method like this:

new ActionBar_Setup().UpdateActionBar(CurrentFragmentNum);

但是正如您所见,我使用了2个变量: btnBack btnNext 当我从不同的类调用方法时,这些变量为null,因为未定义变量.但是我无法移动为这些变量分配值的行,因为它使用了视图变量.这是显示我的意思的代码:

But as you an see I use the 2 variables: btnBack and btnNext Those variables are null when I call the method from a different class, because then the variables are not defined. But I can't move the lines that assign a value to those variables because it uses a view variable. Here is the code to show what I mean:

public class ActionBar_Setup : Android.Support.V4.App.Fragment
{
    Button btnBack;
    Button btnNext;

    public int CurrentFragmentNum = 1;
    public int PreviousFragmentNum = 1;
    public string Direction;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.setup_nav_bar, container, false);

        btnBack = view.FindViewById<Button>(Resource.Id.btnBack);
        btnNext = view.FindViewById<Button>(Resource.Id.btnNext);

        btnBack.Click += btnBack_Click;
        btnNext.Click += btnNext_Click;

        UpdateActionBar(CurrentFragmentNum);

        return view;
    }

    public void btnBack_Click(object sender, EventArgs e)
    {
        if (CurrentFragmentNum > 1)
        {
            PreviousFragmentNum = CurrentFragmentNum;
            CurrentFragmentNum -= 1;
            Direction = "Backwards";
            UpdateActionBar(CurrentFragmentNum);
            (Activity as MainActivity)?.ShowFragment(CurrentFragmentNum, PreviousFragmentNum, Direction);
        }
    }

    public void btnNext_Click(object sender, EventArgs e)
    {
        if (CurrentFragmentNum < 3)
        {
            PreviousFragmentNum = CurrentFragmentNum;
            CurrentFragmentNum += 1;
            Direction = "Forwards";
            UpdateActionBar(CurrentFragmentNum);
            (Activity as MainActivity)?.ShowFragment(CurrentFragmentNum, PreviousFragmentNum, Direction);
        }
    }

    public void UpdateActionBar(int CurrentFragmentNum)
    {
        if (CurrentFragmentNum == 1)
        {
            btnBack.Visibility = ViewStates.Invisible;
            btnNext.Visibility = ViewStates.Invisible;
        }
        else
        {
            btnBack.Visibility = ViewStates.Visible;
            btnNext.Visibility = ViewStates.Visible;
        }

        if (CurrentFragmentNum == 3)
        {
            btnNext.Text = "Finish";
        }
        else
        {
            btnNext.Text = "Next";
        }
    }
}

如何解决此问题,以便可以在其他类中调用该函数?

How can I fix this, so I can call the function in a different class?

推荐答案

您可以将您的 ActionBar_Setup 单身:

public class ActionBar_Setup : Android.Support.V4.App.Fragment
 {

    public static ActionBar_Setup Instance;
    public static ActionBar_Setup NewInstance()
    {
        if (Instance== null)
        {
            Instance= new ActionBar_Setup();

        }

        return Instance;
    }
    ...
}

然后在创建 ActionBar_Setup 时,如下所示:

then when you create the ActionBar_Setup like this:

ActionBar_Setup fActionBarSetup = ActionBar_Setup.NewInstance();

最终在其他类中调用 UpdateActionBar 方法,如下所示:

finally call UpdateActionBar method in other class like this:

ActionBar_Setup.Instance.UpdateActionBar(3);

这篇关于从使用该方法之外的变量的其他类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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