Xamarin - 从MainActivity调用片段方法

时间:2017-07-01 16:24:49

标签: c# android android-fragments xamarin android-viewpager

我有一个带有3个标签片段的viewPager。 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";

    }

}

所以,我想从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); }

    }

它运行正常(因为我看到每次选择选项卡时都会在Snackbar中显示正确的itemID),但是在if子句下的最后一行...

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

...我看到Visual Studio错误弹出说,

  

非静态字段,方法或属性需要对象引用tabFragment1.UpdateContent(fView)&#39;

解决这个问题的最佳方法是什么?感谢

1 个答案:

答案 0 :(得分:1)

public void UpdateContent(View v)是对象的方法而不是类型的方法。因此,要使用它,您需要创建tabFragment1的对象并将其称为方法。或者,您可以将UpdateContent(View v)的声明更改为static UpdateContent(View v),并将其称为类型的方法。

更改声明:

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

使用:

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