如何从弹出窗口中的控件返回值并将它们添加到主页? [英] How to return values from controls in popup and add them to the main page?

查看:28
本文介绍了如何从弹出窗口中的控件返回值并将它们添加到主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主页有一个用于添加新项目的按钮.当用户按下它时,会出现一个弹出窗口.我将此代码用于 popup.xaml:

The main page has a button for adding new items. When the user presses it, a popup window appears. I used this code for popup.xaml:

<xct:Popup xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MReport.popup01"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             Size="300,400">
    
    
    <ScrollView>
        <StackLayout Orientation="Vertical" Spacing="10">
            <Label Text="From Time:" Padding="5,5,0,0" TextColor="Black"
                HorizontalTextAlignment="Start" />
            <TimePicker HorizontalOptions="Center" Time="00:00" />
            <Label Text="To Time" Padding="5,5,0,0" TextColor="Black"
                HorizontalTextAlignment="Start" />
            <TimePicker HorizontalOptions="Center"/>
            <Label Text="Your Activities:" Padding="5,5,0,0" TextColor="Black" />
            <Editor x:Name="editor_value" VerticalOptions="CenterAndExpand" HeightRequest="200" />
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="1" Text="Add" Clicked="Button_Clicked_1" />
                <Button Grid.Column="0" Text="Cancel" Clicked="Button_Clicked" />
            </Grid>
        </StackLayout>
    </ScrollView>

</xct:Popup>

对于 popup.xaml.cs:

For popup.xaml.cs:

using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MReport
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class popup01 : Popup
    {
        public popup01()
        {
            InitializeComponent();
        }
        private void Button_Clicked(object sender, EventArgs e)
        {
            Dismiss("Cancelled");
        }

        private void Button_Clicked_1(object sender, EventArgs e)
        {
            
        }
    }
}

对于标签页:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MReport.TabbedMainPage">
  <!--Pages can be added as references or inline-->
    <ContentPage Title="New Report" IconImageSource="NewReport.png" BackgroundImageSource="blue_windows.jpg">
        <StackLayout>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <ScrollView>
                    <StackLayout>
                        <Label x:Name="lbl01" />
                    </StackLayout>
                </ScrollView>
                <Button Grid.Row="1" Text="Add New Item" Clicked="Button_Clicked"/>
            </Grid>
            <Button Grid.Row="1" Text="Send Report" />
        </StackLayout>
    </ContentPage>
    <ContentPage Title="Report History" IconImageSource="History.png" BackgroundImageSource="blue_windows.jpg" />
    <ContentPage Title="Messages" IconImageSource="Message.png" BackgroundImageSource="blue_windows.jpg"/>

</TabbedPage>

对于 TabbedPage.xaml.cs:

For TabbedPage.xaml.cs:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;
using Xamarin.CommunityToolkit.Extensions;

namespace MReport
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TabbedMainPage : Xamarin.Forms.TabbedPage
    {
        public TabbedMainPage()
        {
            InitializeComponent();
            On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
            On<Android>().SetIsSmoothScrollEnabled(false);
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Navigation.ShowPopup(new popup01());
        }
    }
}

我使用 CommunityToolKit 作为弹出窗口.弹出窗口有两个按钮.ADD 按钮会将在弹出窗口中输入的信息添加到主页面.我希望输出就像我提供的图像一样.每次用户按下弹出窗口中的添加按钮时,都会添加这些框架框及其文本.这些框架框根据其中的文本体积进行扩展.应该为 ADD 按钮编写什么代码?请帮帮我.

I use CommunityToolKit for the popup. The popup has two buttons. The ADD button will add the information entered in the popup to the main page. I want the output to be like the image I've provided. These frame boxes and their texts are added each time the user presses the add button in the popup. Those frame boxes are expanded based on the text volume inside them. What code should be written for the ADD button? Please help me.

推荐答案

ShowPopupAsync 可以返回一个值给调用者

ShowPopupAsync can return a value to the caller

var result = await Navigation.ShowPopupAsync(popup);

任何传递给Dismiss()的参数都会返回给调用者

whatever parameter is passed to Dismiss() will be returned to the caller

这篇关于如何从弹出窗口中的控件返回值并将它们添加到主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆