【问题标题】:How to make an alterable Xamarin.Form Xaml Page?如何制作可更改的 Xamarin.Form Xaml 页面?
【发布时间】:2020-06-02 02:59:00
【问题描述】:

我想在 Xamarin.Form 中实现一个 Xaml 页面,可以通过用户输入进行修改。例如,如果用户单击一个按钮,应用程序将显示一个日期选择器,如果用户单击另一个按钮,应用程序将删除日期选择器并在完全相同的位置显示一个文本条目。 我尝试使用“IsVisible”属性,但它没有按预期工作(xaml 对象仍然存在但只是不可见,所以我无法替换它)。

有什么建议吗?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    您可以移除一个孩子。

    像这样:

    Children.RemoveChildren.RemoveAt

    参考:

    https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.layout-1.children?view=xamarin-forms

    【讨论】:

    • 不知道,但它有效。非常感谢!
    【解决方案2】:

    如果您想让您的 xaml 页面像警报一样显示,您可以使用来自 nuget 的插件 Rg.Plugins.Popup

    用法

    <?xml version="1.0" encoding="utf-8" ?>
    <pages:PopupPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
        xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
        x:Class="MyProject.MyPopupPage">
        <!--You can set an animation in the xaml file or in the csharp code behind-->
        <pages:PopupPage.Animation>
            <animations:ScaleAnimation 
                PositionIn="Center"
                PositionOut="Center"
                ScaleIn="1.2"
                ScaleOut="0.8"
                DurationIn="400"
                DurationOut="300"
                EasingIn="SinOut"
                EasingOut="SinIn"
                HasBackgroundAnimation="True"/>
        </pages:PopupPage.Animation>
    
        <!--You can use any elements here which are extended from Xamarin.Forms.View-->
        <StackLayout 
            VerticalOptions="Center" 
            HorizontalOptions="Center" 
            Padding="20, 20, 20, 20">
            <Label
                Text="Test"/>
        </StackLayout>
    </pages:PopupPage>
    
    public partial class MyPopupPage : Rg.Plugins.Popup.Pages.PopupPage
        {
            public MyPopupPage()
            {
                InitializeComponent();
            }
    
            protected override void OnAppearing()
            {
                base.OnAppearing();
            }
    
            protected override void OnDisappearing()
            {
                base.OnDisappearing();
            }
    
            // ### Methods for supporting animations in your popup page ###
    
            // Invoked before an animation appearing
            protected override void OnAppearingAnimationBegin()
            {
                base.OnAppearingAnimationBegin();
            }
    
            // Invoked after an animation appearing
            protected override void OnAppearingAnimationEnd()
            {
                base.OnAppearingAnimationEnd();
            }
    
            // Invoked before an animation disappearing
            protected override void OnDisappearingAnimationBegin()
            {
                base.OnDisappearingAnimationBegin();
            }
    
            // Invoked after an animation disappearing
            protected override void OnDisappearingAnimationEnd()
            {
                base.OnDisappearingAnimationEnd();
            }
    
            protected override Task OnAppearingAnimationBeginAsync()
            {
                return base.OnAppearingAnimationBeginAsync();
            }
    
            protected override Task OnAppearingAnimationEndAsync()
            {
                return base.OnAppearingAnimationEndAsync();
            }
    
            protected override Task OnDisappearingAnimationBeginAsync()
            {
                return base.OnDisappearingAnimationBeginAsync();
            }
    
            protected override Task OnDisappearingAnimationEndAsync()
            {
                return base.OnDisappearingAnimationEndAsync();
            }
    
            // ### Overrided methods which can prevent closing a popup page ###
    
            // Invoked when a hardware back button is pressed
            protected override bool OnBackButtonPressed()
            {
                // Return true if you don't want to close this popup page when a back button is pressed
                return base.OnBackButtonPressed();
            }
    
            // Invoked when background is clicked
            protected override bool OnBackgroundClicked()
            {
                // Return false if you don't want to close this popup page when a background of the popup page is clicked
                return base.OnBackgroundClicked();
            }
        }
    

    该插件具有默认动画,您可以非常简单地使用它们。更多详情可以查看https://github.com/rotorgames/Rg.Plugins.Popup/wiki/PopupPage

    【讨论】:

    • 非常感谢您冗长而完整的回答,但作为初学者,我想我会为我的应用程序做一些更简单的事情。无论如何,谢谢你花时间回答我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2020-03-04
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多