C# 调用ContentView时自动添加

C# 调用ContentView时自动添加,c#,xamarin,xamarin.forms,popup,C#,Xamarin,Xamarin.forms,Popup,你好 我制作了名为WarningPopup的ContentView。我想在调用时自动将此ContentView添加到当前页面。 例如,我正在使用: WarningPopup wp = new WarningPopup { ButtonText = "OK" }; 但是我的警告弹出窗口没有显示在我的页面上。。。要显示我的弹出窗口,我应该使用例如 Content = new AbsoluteLayout { Children = {

你好

我制作了名为WarningPopup的ContentView。我想在调用时自动将此ContentView添加到当前页面。 例如,我正在使用:

WarningPopup wp = new WarningPopup {
            ButtonText = "OK"
        };
但是我的警告弹出窗口没有显示在我的页面上。。。要显示我的弹出窗口,我应该使用例如

 Content = new AbsoluteLayout {
            Children = {
                wp
            }
        };
所以。。。我希望在不使用最后几行代码的情况下自动添加到当前页面。或者我做错了一切

如果这是我的警告弹出类.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class WarningPopup : ContentView
{
    public static readonly BindableProperty ButtonTextProperty =
        BindableProperty.Create("ButtonText", typeof(string), typeof(WarningPopup), default(string));

    public string ButtonText {
        get { return (string)GetValue(ButtonTextProperty);  }
        set { SetValue(ButtonTextProperty, value);          }        
    }


    public WarningPopup ()
    {
        InitializeComponent ();
        innerLabel.SetBinding(Label.TextProperty, new Binding("ButtonText", source: this));


    }
}
还有我的XAML:

<ContentView 
        WidthRequest="180"
        HeightRequest="60"
        xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="MobileAMBPanel.WarningPopup">

      <Frame CornerRadius="4" HasShadow="False" OutlineColor="Silver" BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
        <StackLayout Orientation="Horizontal">
                <Image x:Name="innerImage"  VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand"/>
                <Label x:Name="innerLabel"  VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
        </StackLayout>
      </Frame>
</ContentView>


谢谢

1st:让我们将ContentView中的
绝对布局
属性移动到实际的父级,即ContentView本身(您将改变绝对位置,否则,我将调整其大小以适应/覆盖屏幕的“中心”(例如):

3rd:使用托管页面绝对布局的
x:Name
,您可以
添加
/
删除
需要时的“弹出式”内容视图:

button.Clicked += async (object sender, EventArgs e) =>
{
    var contentView = new WarningPopup();
    someContainer.Children.Add(contentView);
    await Task.Delay(1000);
    someContainer.Children.Remove(contentView);
};

这几乎是我想要实现的目标,但这给了我完成任务的想法。非常感谢。
<ContentPage 
    ~~~
    >
    <ContentPage.Content>
        <AbsoluteLayout x:Name="someContainer">
            <StackLayout> 
                ~~~~
            </StackLayout>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
button.Clicked += async (object sender, EventArgs e) =>
{
    var contentView = new WarningPopup();
    someContainer.Children.Add(contentView);
    await Task.Delay(1000);
    someContainer.Children.Remove(contentView);
};