gpt4 book ai didi

xamarin - xamarin.form 中的弹出消息框

转载 作者:行者123 更新时间:2023-12-02 17:54:49 25 4
gpt4 key购买 nike

我想做的与 DisplayAlert 类似,弹出一个显示页面,其中包含图像、内容和右上角的小关闭按钮。显示页面不应覆盖整个手机,而应覆盖手机 UI 的 80% 左右,背景保留为父页面。

我正在尝试使用 PushModalAsync 和 PopModalAsync,但是没有运气。输出不是我所期望的。

基本上,我有一个 ListView ,每当从屏幕上选择项目时,它都会调用 popUpMethod:

 list.ItemSelected += MyMethod;

在 MyMethod 中我将调用 popUpPage

async void MyMethod(object sender, SelectedItemChangedEventArgs e){
Content = await PopUpPage();
}

这是我的 PopUpPage 方法

private async Task<StackLayout> PopUpPage()
{
StackLayout objPopUp = new StackLayout() { HeightRequest = 100, WidthRequest= 100, VerticalOptions = LayoutOptions.CenterAndExpand};

Label lblMessage = new Label();
lblMessage.Text = "Welcome";

objPopUp.Children.Add(lblMessage);

return objPopUp;
}

我正在尝试设置弹出页面内的高度和宽度。但是,它仍然覆盖整个屏幕,这不是我想要的。如果需要任何充值信息,请告诉我,谢谢。

P/S:我在xamarin.Form(可移植)中设计了它

最佳答案

您可以创建自定义弹出窗口来在 Xamarin.Forms 中完成此操作

这是我创建的自定义ContentView。它使用 BoxView 提供背景淡化的外观,并使用 Frame 向弹出窗口添加阴影。

我还使用动画来使自定义弹出窗口看起来就像从屏幕上弹出一样!

示例应用

此示例应用程序的代码可在 Github 上找到:

https://github.com/brminnick/InvestmentDataSampleApp

Custom Pop-up In Xamarin.Forms

代码片段

public class WelcomeView : ContentView
{
readonly BoxView _backgroundOverlayBoxView;
readonly Frame _overlayFrame;
readonly StackLayout _textAndButtonStack;

readonly RelativeLayout _relativeLayout;

public WelcomeView()
{
const string titleText = "Welcome";
const string bodyText = "Enjoy InvestmentDataSampleApp";
const string okButtonText = "Ok, thanks!";

var whiteWith75Opacity = new Color(255, 255, 255, 0.75);
_backgroundOverlayBoxView = new BoxView
{
BackgroundColor = whiteWith75Opacity
};
_backgroundOverlayBoxView.Opacity = 0;

_overlayFrame = new Frame
{
HasShadow = true,
BackgroundColor = Color.White
};
_overlayFrame.Scale = 0;

var titleLabel = new Label
{
FontAttributes = FontAttributes.Bold,
Text = titleText,
HorizontalTextAlignment = TextAlignment.Center
};

var bodyLabel = new Label
{
Text = bodyText,
HorizontalTextAlignment = TextAlignment.Center
};

var blackWith75PercentOpacity = new Color(0, 0, 0, 0.75);
var okButton = new Button
{
BackgroundColor = blackWith75PercentOpacity,
TextColor = Color.White,
BorderWidth = 1,
BorderColor = blackWith75PercentOpacity,
FontAttributes = FontAttributes.Bold,
Margin = new Thickness(5),
Text = okButtonText
};
okButton.Clicked += (sender, e) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await this.FadeTo(0);
this.IsVisible = false;
this.InputTransparent = true;
});
}

_textAndButtonStack = new StackLayout
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Spacing = 20,
Children = {
titleLabel,
bodyLabel,
okButton
}
};
_textAndButtonStack.Scale = 0;

_relativeLayout = new RelativeLayout();
Func<RelativeLayout, double> gettextAndButtonStackHeight = (p) => _textAndButtonStack.Measure(_relativeLayout.Width, _relativeLayout.Height).Request.Height;
Func<RelativeLayout, double> gettextAndButtonStackWidth = (p) => _textAndButtonStack.Measure(_relativeLayout.Width, _relativeLayout.Height).Request.Width;


_relativeLayout.Children.Add(_backgroundOverlayBoxView,
Constraint.Constant(-10),
Constraint.Constant(0),
Constraint.RelativeToParent(parent => parent.Width + 20),
Constraint.RelativeToParent(parent => parent.Height)
);
_relativeLayout.Children.Add(_overlayFrame,
Constraint.RelativeToParent(parent => parent.Width / 2 - gettextAndButtonStackWidth(parent) / 2 - 20),
Constraint.RelativeToParent(parent => parent.Height / 2 - gettextAndButtonStackHeight(parent) / 2 - 10),
Constraint.RelativeToParent(parent => gettextAndButtonStackWidth(parent) + 30),
Constraint.RelativeToParent(parent => gettextAndButtonStackHeight(parent) + 30)
);

_relativeLayout.Children.Add(_textAndButtonStack,
Constraint.RelativeToView(_overlayFrame, (parent, view) => view.X + 15),
Constraint.RelativeToView(_overlayFrame, (parent, view) => view.Y + 15)
);

if (Device.OS == TargetPlatform.Android)
{
_overlayFrame.IsVisible = false;
_textAndButtonStack.BackgroundColor = whiteWith90Opacity;
}

Content = _relativeLayout;
}

public void DisplayView()
{
Device.BeginInvokeOnMainThread(async () =>
{
var animationList = new List<Task>
{
_backgroundOverlayBoxView.FadeTo(1,AnimationConstants.WelcomeViewAnimationTime),
_textAndButtonStack.ScaleTo(AnimationConstants.WelcomeViewMaxSize, AnimationConstants.WelcomeViewAnimationTime),
_overlayFrame.ScaleTo(AnimationConstants.WelcomeViewMaxSize,AnimationConstants.WelcomeViewAnimationTime)
};
await Task.WhenAll(animationList);

animationList = new List<Task>
{
_textAndButtonStack.ScaleTo(AnimationConstants.WelcomeViewNormalSize, AnimationConstants.WelcomeViewAnimationTime),
_overlayFrame.ScaleTo(AnimationConstants.WelcomeViewNormalSize, AnimationConstants.WelcomeViewAnimationTime)
};
await Task.WhenAll(animationList);
});
}
}

关于xamarin - xamarin.form 中的弹出消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29739136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com