简体   繁体   中英

Calling method from Fragment inside another Fragment

I have a problem. I have 2 fragments with methods. Now I want to call a method from Fragment2.cs inside Fragment1.cs. Here is the method from Fragment2.cs:

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";
    }
}

And in Fragment1.cs I need to call this method! How can I do that... I already know how to call a method from the MainActivity using this:

(Activity as MainActivity)?.Method();

But now I want the method from another fragment.
I have already tried something like this:

(Fragment as Fragment2)?.Method();

But that gives me the error:

'Fragment' is a type, which is not valid in the given context

Can someone help me?

Please make search before ask, the question is very popular

google search

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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