我尝试的是:创建一个从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 } };