早安,
我制作了称为WarningPopup的ContentView。调用时,我想将此ContentView自动添加到当前页面。 例如我正在使用:
WarningPopup wp = new WarningPopup {
ButtonText = "OK"
};
但是我的WarningPopup没有显示在我的页面上...显示我应该使用的弹出窗口
Content = new AbsoluteLayout {
Children = {
wp
}
};
所以...我想在不使用最后几行代码的情况下自动添加到当前页面。还是我做错了什么?
如果这是我的WarningPopup类.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>
谢谢!
答案 0 :(得分:1)
第一:让我们将ContentView中的AbsoluteLayout
属性移动到实际的父对象,即ContentView本身(您将追寻改变思维的绝对定位,否则,我将其调整大小以适合/覆盖示例的屏幕“中心”):
<ContentView
~~~
AbsoluteLayout.LayoutBounds=".5,.5,.5,.5" AbsoluteLayout.LayoutFlags="All">
<ContentView.Content>
<Frame CornerRadius="4" HasShadow="False" OutlineColor="Silver" BackgroundColor="Red">
<StackLayout Orientation="Horizontal">
<Image x:Name="innerImage" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand"/>
<Label x:Name="innerLabel" Text="StackOverflow" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
</StackLayout>
</Frame>
</ContentView.Content>
</ContentView>
第二个::在将要托管此“弹出窗口”的页面中,确保根布局为AbsoluteLayout
,如果不是,则将所有现有页面内容都包含在其中在不更改实际内容的情况下可以正常工作,并确保在其中添加x:Name
。
<ContentPage
~~~
>
<ContentPage.Content>
<AbsoluteLayout x:Name="someContainer">
<StackLayout>
~~~~
</StackLayout>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
第3次::只要使用托管页面的AbsoluteLayout的x:Name
,就可以在需要时Add
/ Remove
“弹出” ContentView:>
button.Clicked += async (object sender, EventArgs e) =>
{
var contentView = new WarningPopup();
someContainer.Children.Add(contentView);
await Task.Delay(1000);
someContainer.Children.Remove(contentView);
};