如何使用按钮更改片段中的布局 单击C# Xamarin

本文关键字:布局 单击 Xamarin 片段 何使用 按钮 | 更新日期: 2023-09-27 17:56:22

我是安卓开发的新手,我想问一个关于使用按钮单击更改片段布局的问题。当我尝试返回按钮上的视图时它说

转换为 void 返回委托的匿名函数无法返回值

按钮

似乎工作正常,因为当我单击按钮(没有返回值)时会弹出 Toast 文本。这是我的代码

public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
{
            AppPreferences ap = new AppPreferences(mContext);
            var rootView = p0.Inflate(Resource.Layout.Kategori, p1, false);
            Button btnTIM= rootView.FindViewById<Button>(Resource.Id.btnTIM);
            Button btnManShoes= rootView.FindViewById<Button>(Resource.Id.btnManShoes);
           btnTIM.Click += delegate
           {
                        var myActivity = (MenuUtama)this.Activity;
                        AndHUD.Shared.Show(myActivity, "Loading..");
                        if (myActivity.isOnline() == true)
                            ambilTanggalTIM(ap);
                        else
                            myActivity.internetDropDialog();
            };
        btnManShoes.Click += delegate
        {
                //on this button, i want to change the view to Resource.Layout.ManShoes
                          Toast.MakeText(mContext, "Clicked!", ToastLength.Long).Show();
                           rootView = p0.Inflate(Resource.Layout.ManShoes, p1, false);
                           return rootView;  //Got error when return the View
         };
        return rootView;
    }

我正在使用Visual Studio 2015 + Xamarin,提前感谢

如何使用按钮更改片段中的布局 单击C# Xamarin

我找到了为什么它不起作用的答案,这里是链接

片段的布局一旦设置,就无法替换。如果您需要 条件布局,然后您必须重新设计布局和 将其拆分为更小的片段,或者您可以将所有布局分组 元素在子容器中(如 LinearLayout),然后将它们全部放入 相对布局,位置相互重叠并交换可见性 这些 LinearLayoutswith setVisibility() 在需要时。

您可以看到我的代码,但它的方式不同。我不确定这是否是最好的方法。这是我的代码:

public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
    {
       AppPreferences ap = new AppPreferences(mContext);
       var i = Arguments.GetInt(ArgPlanetNumber);
       if (i == 0)
       {
         var rootView = p0.Inflate(Resource.Layout.Kategori, p1, false);
         Button btnTIM= rootView.FindViewById<Button>(Resource.Id.btnTIM);
         Button btnManShoes= rootView.FindViewById<Button>(Resource.Id.btnManShoes);
         btnTIM.Click += delegate
         {
            var myActivity = (MenuUtama)this.Activity;
            AndHUD.Shared.Show(myActivity, "Loading..");
            if (myActivity.isOnline() == true)
            ambilTanggalTIM(ap);
            else
            myActivity.internetDropDialog();
                };
            btnManShoes.Click += delegate
            {
              var fragment = new PlanetFragment();
              var arguments = new Bundle();
              arguments.PutInt(PlanetFragment.ArgPlanetNumber, 100);
              fragment.Arguments = arguments;
              FragmentManager.BeginTransaction()
              .Replace(Resource.Id.content_frame, fragment)
              .Commit();
            };
        }
       else if(i == 10)
       {
         var rootView = p0.Inflate(Resource.Layout.Example, p1, false);
         return rootView;
       }
       else if(i == 100)
       {
         var rootView = p0.Inflate(Resource.Layout.ManShoes, p1, false);
         return rootView;
       }
      return rootView;
    }

因此,您可以根据需要分配"i"变量和rootView。