【问题标题】:C# Xamarin Fragment won't launchC# Xamarin 片段不会启动
【发布时间】:2018-03-25 10:39:37
【问题描述】:

我正在努力解决一个问题!

我创建了一个片段来用文本填充页面,所以这里是完整的 OnCreateView:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.Inflate(Resource.Layout.ProductInfo, container, false);

    List<products> SelectedCoinList = (from productname in coinList
                                       where productname.ProductName.Contains(SelectedProductName, StringComparison.OrdinalIgnoreCase)
                                       select productname).ToList<products>();

    ImageView ProductLogo = view.FindViewById<ImageView>(Resource.Id.ProductInfoLogo);

    var imageBitmap = GetImageBitmapFromUrl("http://******/thumbnails/" + SelectedProductList[position].Symbol + ".png");
    ProductLogo.SetImageBitmap(imageBitmap);

    TextView txtProductInfoName = view.FindViewById<TextView>(Resource.Id.txtProductInfoName);
    txtProductInfoName.Text = SelectedProductList[position].FullName;

    TextView txtProductInfoPrice = view.FindViewById<TextView>(Resource.Id.txtProductInfoPrice);
    txtProductInfoPrice.Text = "€ " + SelectedProductList[position].Price;

    return view;
}

在 MainActivity.cs 我有这段代码(应该)启动片段:

public void ListViewProducts_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {
    //Give variables the right value to share with other classes
    ProductName = productsList[e.Position].ProductNameName;
    position = e.Position;

    // Set view to Info 
    SetContentView(Resource.Layout.ProductInfo);
}

public string GetProductName() {
    return ProductName; //return your mainActivity list
}

问题是 Fragment 没有启动或其他什么,因为它加载了 ProductInfo 页面,但带有默认文本。我通过在 OnCreateView 中放置断点来解决这个问题,但没有使用它们。

有什么建议吗?

【问题讨论】:

    标签: c# visual-studio xamarin android-activity fragment


    【解决方案1】:

    您需要创建片段实例并将其插入到 Activity 布局中的视图中。例如,您可以使用 FrameLayout。 在活动布局中添加框架布局

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    使用属性 android:visibility="gone"

    如果您希望 frameLayout 在活动开始时不可见且不占用空间。

    然后在你的活动中有这样的方法

    private YourFragment _fragment;
    private void DisplayFragment()
    {
        var fragmentContainer = FindViewById<FrameLayout> 
           (Resource.Id.fragmentContainer);
        _fragment= new YourFragment();
        var fragmentManager = FragmentManager.BeginTransaction();
        fragmentManager.Add(Resource.Id.fragmentContainer, _fragment);
        fragmentManager.Commit();
        fragmentContainer .Visibility = ViewStates.Visible; //Makes your fragment container visible if you set visibility=gone"
    
    }
    

    【讨论】:

    • 我是否需要将公共空白放在顶部
    • 对不起,它应该是私有的,因为它是一种方法,它可以在你的活动类中的任何地方进行
    • 和FrameLayout,需要放在ProductInfo页面还是ListView页面?
    • 你把它放在你的活动使用的任何布局中
    • 还是一样的结果 :(
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多