代码之家  ›  专栏  ›  技术社区  ›  Tobe

创建顶部有固定视图的ContentPage模板

  •  1
  • Tobe  · 技术社区  · 7 年前

    我尝试的是:创建一个从ContentPage继承的类“SyncInfoContentPage”。我已经编写的所有ContentPage现在将不再从ContentPage继承,而是从SyncInfoContentPage继承。 SyncInfoContentPage会自动获取其内容,并将其替换为包含SyncInfo和原始内容的新Stacklayout。通过这样做,我不必重写我们已有的77页内容。

    这段代码在Android上运行良好,但在iOS上SyncInfo不可见,而且(更糟糕的是)从SyncInfoContentPage继承的我的contentpage不再对任何内容做出反应。

    public class SyncInfoContentPage : ContentPage
        {
            private readonly Frame SyncInfo;
    
            public SyncInfoContentPage()
            {
                SyncInfo = BuildSyncInfo(); //Creates the frame with the sync Information
                PropertyChanged += SyncInfoContentPage_PropertyChanged;
            }
    
            private void SyncInfoContentPage_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
              // Add the SyncInfo Frame on top of the Content when the Content gets changed
                if (e.PropertyName.Equals("Content"))
                {
                    bool change = false;
              // If the content already is a StackLayout, check if the SyncInfo already got added, so that theres no infinite loop.
                    if (Content is StackLayout)
                    {
                        var check = Content as StackLayout;
                        if (!check.Children.Contains(SyncInfo))
                        {
                            change = true;
                        }
                    }
                    else  // if the Content is no StackLayout, the SyncInfo Frame can't be inside the Content yet
                    {
                        change = true;
                    }
    
                    if (change)
                    {
                        var layout = Content;       // This is a reference, probably the error?
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            Content = new StackLayout
                            {
                                Children = { SyncInfo, layout }
                            };
                        });
                    }
                }
            }
        }
    

    问题可能是iOS不喜欢这一部分:

    var layout = Content;
    Content = new StackLayout { Children = { SyncInfo, layout } };
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Tobe    7 年前

    我让它工作了。解决办法很简单,但很奇怪。在添加SyncInfo状态栏之后,必须添加原始内容。

                        var layoutOld = Content; 
                        var layoutNew = new StackLayout
                        {
                            Children = { SyncInfo }
                        };
                        Content = layoutNew;
                        layoutNeu.Children.Add(layoutOld);
    
    推荐文章