问题
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);
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?
回答1:
you could make your ActionBar_Setup singleton:
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;
}
...
}
then when you create the ActionBar_Setup like this:
ActionBar_Setup fActionBarSetup = ActionBar_Setup.NewInstance();
finally call UpdateActionBar method in other class like this:
ActionBar_Setup.Instance.UpdateActionBar(3);
回答2:
you can pass parameters to your button events from the view as a GET or POST values on the button and assign the values to your method directly from the views
回答3:
Theyre multiple ways to achieve what you want as per what Ahmed Moawad says. Another possible solution would be to add a btnBack and a btnNext field in the class. Then assign the values to the fields. Once you execute the method. You should be able to grab the fields within the calling method.
I would say seperate the UpdateActionBar method from the form actions. So you'll have your UpdateActionBar in a seperate class as you do. But your btnBack and btnNext fields should probably be named some thing else. Then within the calling method assign the results of your fields to btnNext and btnBack.
来源:https://stackoverflow.com/questions/56120534/calling-a-method-from-a-different-class-that-uses-variables-outside-the-method