简体   繁体   English

从另一个Fragment中的Fragment调用方法

[英]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. 现在我想从Fragment1.cs中的Fragment2.cs调用一个方法。 Here is the method from Fragment2.cs: 这是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! 在Fragment1.cs中我需要调用这个方法! How can I do that... I already know how to call a method from the MainActivity using this: 我该怎么做...我已经知道如何使用以下方法从MainActivity调用方法:

(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 'Fragment'是一种类型,在给定的上下文中无效

Can someone help me? 有人能帮我吗?

Please make search before ask, the question is very popular 请先询问,问题很受欢迎

google search 谷歌搜索

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

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