调用时 ContentView 自动添加
ContentView autoAdd when called
美好的一天,
我制作了名为 WarningPopup 的 ContentView。我想在调用时自动将此 ContentView 添加到我的当前页面。
例如我正在使用:
WarningPopup wp = new WarningPopup {
ButtonText = "OK"
};
但是我的 WarningPopup 没有显示在我的页面上...为了显示我应该使用的弹出窗口,例如
Content = new AbsoluteLayout {
Children = {
wp
}
};
所以...我想在不使用最后几行代码的情况下自动添加到我的当前页面。或者也许我做错了一切?
如果这是我的 WarningPopup class .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 中的 AbsoluteLayout
属性移动到实际父级,即 ContentView 本身,(否则您将追求改变思维的绝对定位,对于我的示例,我将其大小调整为 fit/overlay 屏幕的 "center"):
<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="Whosebug" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
</StackLayout>
</Frame>
</ContentView.Content>
</ContentView>
2nd: 在将承载此 "popup" 的页面中,确保根布局是 AbsoluteLayout
,如果不是,则仅包含一个页面的所有现有内容都可以在不对实际内容进行任何其他更改的情况下使用,并确保向其添加 x:Name
。
<ContentPage
~~~
>
<ContentPage.Content>
<AbsoluteLayout x:Name="someContainer">
<StackLayout>
~~~~
</StackLayout>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
3rd: 使用托管页面的 AbsoluteLayout 的 x:Name
,您可以 Add
/ Remove
"popup" ContentView当您需要时:
button.Clicked += async (object sender, EventArgs e) =>
{
var contentView = new WarningPopup();
someContainer.Children.Add(contentView);
await Task.Delay(1000);
someContainer.Children.Remove(contentView);
};
美好的一天,
我制作了名为 WarningPopup 的 ContentView。我想在调用时自动将此 ContentView 添加到我的当前页面。 例如我正在使用:
WarningPopup wp = new WarningPopup {
ButtonText = "OK"
};
但是我的 WarningPopup 没有显示在我的页面上...为了显示我应该使用的弹出窗口,例如
Content = new AbsoluteLayout {
Children = {
wp
}
};
所以...我想在不使用最后几行代码的情况下自动添加到我的当前页面。或者也许我做错了一切?
如果这是我的 WarningPopup class .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 中的 AbsoluteLayout
属性移动到实际父级,即 ContentView 本身,(否则您将追求改变思维的绝对定位,对于我的示例,我将其大小调整为 fit/overlay 屏幕的 "center"):
<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="Whosebug" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
</StackLayout>
</Frame>
</ContentView.Content>
</ContentView>
2nd: 在将承载此 "popup" 的页面中,确保根布局是 AbsoluteLayout
,如果不是,则仅包含一个页面的所有现有内容都可以在不对实际内容进行任何其他更改的情况下使用,并确保向其添加 x:Name
。
<ContentPage
~~~
>
<ContentPage.Content>
<AbsoluteLayout x:Name="someContainer">
<StackLayout>
~~~~
</StackLayout>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
3rd: 使用托管页面的 AbsoluteLayout 的 x:Name
,您可以 Add
/ Remove
"popup" ContentView当您需要时:
button.Clicked += async (object sender, EventArgs e) =>
{
var contentView = new WarningPopup();
someContainer.Children.Add(contentView);
await Task.Delay(1000);
someContainer.Children.Remove(contentView);
};