我有问题。我在这样的类中有一个方法:
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";
}
}
现在在另一个类中,我这样调用此方法:
new ActionBar_Setup().UpdateActionBar(CurrentFragmentNum);
但是正如您所看到的,我使用了两个变量:btnBack
和btnNext
当我从其他类调用方法时,这些变量为null,因为未定义变量。但是我无法移动为这些变量分配值的行,因为它使用了视图变量。这是显示我的意思的代码:
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";
}
}
}
如何解决此问题,以便可以在其他类中调用该函数?
答案 0 :(得分:1)
您可以使您的 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 时,如下所示:
ActionBar_Setup fActionBarSetup = ActionBar_Setup.NewInstance();
最终像这样在其他类中调用 UpdateActionBar 方法:
ActionBar_Setup.Instance.UpdateActionBar(3);
答案 1 :(得分:0)
您可以在视图中将参数作为按钮上的GET或POST值传递给按钮事件,并直接从视图中将值分配给您的方法
答案 2 :(得分:0)
按照艾哈迈德·穆阿瓦德(Ahmed Moawad)所说,他们有多种途径来实现您想要的。另一种可能的解决方案是在类中添加btnBack和btnNext字段。然后将值分配给字段。一旦执行该方法。您应该能够获取调用方法中的字段。
我会说将UpdateActionBar方法与表单操作分开。因此,您将像执行操作一样将UpdateActionBar放在单独的类中。但是您的btnBack和btnNext字段可能应该命名为其他名称。然后在调用方法中,将字段的结果分配给btnNext和btnBack。