简体   繁体   English

从Fragment调用Activity方法

[英]Call an Activity method from Fragment

I'm using ToDoActiviy.cs for user login, this class got this method: 我使用ToDoActiviy.cs进行用户登录,此类获得了以下方法:

[Java.Interop.Export()]

public async void LoginUser(View view) { if(await authenticate())..

This method is called from .axml file from Button widget android:onClick="LoginUser" I changed this for android:onClick="LoginUserClick" This last method create a dialog fragment for show different logins accounts. 这种方法是从按钮组件.axml文件名为android:onClick="LoginUser"我改变了这对android:onClick="LoginUserClick"这最后的方法创建显示不同的登录帐户的对话片段。

Now from the Dialog Fragment(Is situated on another class) I want to hand the event for the button click on the dialog fragment and call this method from ToDoActivity.cs . 现在,从Dialog Fragment(位于另一个类上)中,我想将按钮的事件交给对话框片段,然后从ToDoActivity.cs调用此方法。

On dialog fragment class I hand the click event like this: 在对话框片段类上,我将click事件传递给这样的事件:

private void ButtonSignInFacebook_Click(object sender, EventArgs args){

//Here code for call to LoginUser method from 'ToDoActivity.cs' ToDoActiviy.cs act = new ToDoActivity(); act.LoginUser(); }

I need to pass a View but I tried a lot of things and any works.. 我需要通过一个视图,但是我尝试了很多事情和任何工作。

Someone can help me? 有人可以帮我吗?

Thanks in advance ;) 提前致谢 ;)

I would like to make a slight modification to @guido-gabriel 's answer. 我想对@ guido-gabriel的答案进行一些修改。

In C# syntax, it will be 在C#语法中,它将是

((ToDoActivity)Activity).yourPublicMethod();

Getter/Setter Methods in Java are mapped to Getter Setter properties in Xamarin.Android Java中的Getter / Setter方法映射到Xamarin.Android中的Getter Setter属性

Finally I fix it ! 终于我解决了! I had to change the parameters of the method and create it without parameters.. and now Is working. 我不得不更改方法的参数,并创建没有参数的方法..现在可以正常工作了。 Both solutions are good: 两种解决方案都不错:

((ToDoActivity)Activity).LoginUserFacebook(); //ToDoActivity act = new ToDoActivity(); //act.LoginUserFacebook();

You have to call the method from the activity. 您必须从活动中调用方法。 Have you tried? 你有没有尝试过?

((YourActivityClassName)getActivity()).yourPublicMethod();

IE IE浏览器

((ToDoActivity)getActivity()).yourPublicMethod();

Adapt and use the snipped below in your fragment 调整并使用片段中的以下片段

var casted = Activity as MyActivityName;

    if (casted != null) {
        casted.IWantToCallThisMethodFromMyFragment();
    }

This is not really a good practice to do. 这确实不是一个好习惯。 Why? 为什么?

Doing this couples the Fragment tightly to this particular Activity type, meaning it will not be possible to reuse the Fragment elsewhere in the code. 这样做会使Fragment与该特定的Activity类型紧密相关,这意味着将无法在代码的其他地方重用Fragment。

Instead I suggest you rely on the Activity subscribing to an event or implementing some kind of callback method in order to do the desired action after login. 相反,我建议您依靠Activity订阅事件或实现某种回调方法,以便在登录后执行所需的操作。

It could also seem like your Activity might be containing a lot of logic that could be split out into a shared library of some kind. 似乎您的“活动”中可能包含许多逻辑,这些逻辑可以拆分为某种共享库。 Making it possible to reuse that code on another platform, for instance iOS in the future. 将来可以在其他平台(例如iOS)上重用该代码。

So since your are in charge of newing up the Fragment, I would do something like this instead: 因此,由于您负责更新Fragment,因此我将执行以下操作:

public class LoginFragment : Fragment
{
    Action _onLoggedIn;

    public static void NewInstance(Action onLoggedIn)
    {
        var fragment = new LoginFragment();
        fragment._onLoggedIn = onLoggedIn;

        return fragment;
    }

    private void Login()
    {
        // login user
        // after loggedin
        _onLoggedIn?.Invoke();
    }
}

Then in your Activity: 然后在您的活动中:

private void LoginUser()
{
    // whatever
}

var loginFragment = LoginFragment.NewInstance(LoginUser);
// fragment transaction here...

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

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