简体   繁体   English

Xamarin-MainActivity的调用片段方法

[英]Xamarin - call fragment method from MainActivity

I have a viewPager with 3 tab fragments. 我有一个带有3个标签片段的viewPager。 The code of tabFragment1 class (Note: V4Fragment = Android.Support.V4.App.Fragment): tabFragment1类的代码(注意:V4Fragment = Android.Support.V4.App.Fragment):

class tabFragment1 : V4Fragment
{

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
    {
        var v = inflater.Inflate(Resource.Layout.tabLayout1, container, false);

        // Set weather icon
        ImageView iv_icon = v.FindViewById<ImageView>(Resource.Id.iv_icon);
        iv_icon.SetImageResource(Resource.Drawable.ic_weather_cloudy_white_48dp);
        Snackbar.Make(container, "tabFragment; onCreateView()", Snackbar.LengthLong).Show();
        //UpdateContent(v);
        return v;
    }

    public void UpdateContent(View v)
    {

        // Sample values to read
        string locationCode = "528454";

        // Mapping views
        TextView tv_currentConditions = v.FindViewById<TextView>(Resource.Id.currentConditions);
        TextView tv_currentTemperature = v.FindViewById<TextView>(Resource.Id.currentTemperature);
        TextView tv_currentRealFeel = v.FindViewById<TextView>(Resource.Id.currentRealFeel);
        TextView tv_minMaxTemperature = v.FindViewById<TextView>(Resource.Id.minMaxTemperature);

        // Update view from database
        tv_currentConditions.Text = Sql.ReadValue(locationCode,"currentConditions");
        tv_currentTemperature.Text = Sql.ReadValue(locationCode, "currentTemperature") + "°C";
        tv_currentRealFeel.Text = GetString(Resource.String.feelLike) + " " + Sql.ReadValue(locationCode, "currentRealFeel") + "°C";
        tv_minMaxTemperature.Text = Sql.ReadValue(locationCode, "minTemperature") + " " + Sql.ReadValue(locationCode, "maxTemperature") + "°C";

    }

}

So, I want to call UpdateContent() method from MainActivity method: 因此,我想从MainActivity方法中调用UpdateContent()方法:

public void UpdateCurrentTab()
    {

        ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
        int currentItem = viewPager.CurrentItem;
        Snackbar.Make(drawerLayout, "CALL TO UPDATE Tab " + currentItem, Snackbar.LengthLong).Show();
        View fView = viewPager.GetChildAt(currentItem);
        if (currentItem == 1) { tabFragment1.UpdateContent(fView); }

    }

It works fine (cause I see I get correct itemIDs in Snackbar appearing every time I select a tab), but on the last row under if clause ... 它工作正常(因为我看到每次选择选项卡时我都会在Snackbar中看到正确的itemID),但是在if子句下的最后一行...

if (currentItem == 1) { tabFragment1.UpdateContent(fView); }

... i see Visual Studio error pop-up saying, that ...我看到Visual Studio错误弹出窗口说,

an object reference is required for the nonstatic field, method, or property 'tabFragment1.UpdateContent(fView)' 非静态字段,方法或属性'tabFragment1.UpdateContent(fView)'需要对象引用

What is the best way to silve the problem? 解决问题的最佳方法是什么? Thanks 谢谢

public void UpdateContent(View v) is object's method not type's method. public void UpdateContent(View v)是对象的方法,而不是类型的方法。 So for using it you need to create object of tabFragment1 and call it method. 因此,要使用它,您需要创建tabFragment1对象并调用它的方法。 Or you can change declaration for UpdateContent(View v) to static UpdateContent(View v) and call it as type's method. 或者,您可以将UpdateContent(View v)声明更改为static UpdateContent(View v)并将其作为类型的方法进行调用。

Changing declaration: 更改声明:

public static void UpdateContent(View v)
{
    // Put here your code from question       
}

And using: 并使用:

public void UpdateCurrentTab()
{
    ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
    int currentItem = viewPager.CurrentItem;
    Snackbar.Make(drawerLayout, "CALL TO UPDATE Tab " + currentItem, Snackbar.LengthLong).Show();
    View fView = viewPager.GetChildAt(currentItem);
    if (currentItem == 1) 
    { 
        // Now it's a type's method not object's
        tabFragment1.UpdateContent(fView); 
    }
}

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

相关问题 从Fragment调用Activity方法 - Call an Activity method from Fragment 将字符串/方法形式的MainPage传递给MainActivity Xamarin - Passing string/method form MainPage to MainActivity Xamarin 如何访问片段类中MainActivity类的公共成员(变量或方法) - how to access public Members(variable or method) of MainActivity class in fragment class 从 MainActivity class 将新字符串注入 Fragment/ViewModel - Inject new string into Fragment/ViewModel from MainActivity class 将事件中的字符串传递给Android Xamarin Visual Studio中的MainActivity - Passing a string from an event to MainActivity in Android Xamarin Visual Studio 从片段移动到xamarin中的活动 - move from fragment to an activity in xamarin Xamarin安卓 - 开关切换开/关振动来自MainActivity的AlarmReceiver中的通知 - Xamarin Android - Switch Toggle On/Off Vibration of a notification in AlarmReceiver from MainActivity 如何在Xamarin Forms中从父级调用子xaml中的方法? - How to call method in child xaml from parent in Xamarin Forms ? 如何从按钮(MVVM)中的Xamarin XAML代码调用方法 - How to call Method from Xamarin XAML Code in Button (MVVM) 通过单击xamarin按钮来调用绑定类的方法。 - call a method of the binding class from a button click xamarin.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM