在另一个页面中创建网格(Xamarin.forms)

h7appiyu  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(140)

我有一个“AddNote”页和一个“HomePage”页。我想在“AddNote”页中单击“TakeNoteBtn”按钮时在“HomePage”页上创建一个网格。如何操作?

XAML button code in AddNote page;
<Button
                CornerRadius="30"
                Text="Take Note"
                x:Name="TakeNoteBtn"
                Margin="20, 30, 20, 0"
                Clicked="TakeNoteBtn_Clicked"
                BackgroundColor="FloralWhite"/>
AddNote page c#;
       private void TakeNoteBtn_Clicked(object sender, EventArgs e)
       {
            //Create grid
        }
        public Grid CreateGrid(string header, string note)
        {
            Label headerLabel = new Label();
            Label noteLabel = new Label();
            headerLabel.Text = header;
            noteLabel.Text = note;
            Grid noteGrid = new Grid();
            noteGrid.Children.Add(headerLabel);
            noteGrid.Children.Add(noteLabel);
            noteGrid.WidthRequest = 50;
            noteGrid.HeightRequest = 50;
            noteGrid.BackgroundColor = Color.Red;
            return noteGrid;
        }
HomePage page xaml;
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             FlowDirection="LeftToRight"
             x:Class="ArduinoNotes.HomePage"
             BackgroundImageSource="background.png"
             IconImageSource="logo.png"
             Title="Arduino Notes">
    <ContentPage.Content>
        <RelativeLayout x:Name="HomePageGridZone">
        </RelativeLayout>
    </ContentPage.Content>
</ContentPage>

我尝试了ArduinoNotes.HomePage.HomePageGridZone.Add(CreateGrid(noteHeader,noteBody));但不起作用。

jhdbpxl9

jhdbpxl91#

是的,您可以使用MessageCenter来实现此目的。
如果你想用MessageCenter实现这个.可以参考下面的代码:
1.在AddNotePage.xaml页上添加按钮(Take Note with messagecenter

<Button 
         Text="Take Note with messagecenter"
         x:Name="TakeNoteBtn2"
         Margin="20, 30, 20, 0"
         Clicked="TakeNoteBtn_Clicked_withMessageCenter"
        />

以及AddNotePage.xaml.cs上的函数TakeNoteBtn_Clicked_withMessageCenter

private void TakeNoteBtn_Clicked_withMessageCenter(object sender, EventArgs e) 
    {
        //Create grid
        //method 2
        string[] values = { mHeaderEntry.Text, mNoteEntry.Text };

        MessagingCenter.Send<AddNotePage, string[]>(this, "Note", values);

        Navigation.PopAsync();
    }

2.在HomePage上,在HomePage的构造方法上订阅消息,页面收到消息后,可以在此页面添加网格
public partial class:public String getString();

MessagingCenter.Subscribe<AddNotePage, string[]>(this,
    "Note", async (sender, values) =>
    {
        string header = values[0];
        string content = values[1];

        Grid grid = CreateGrid(header, content);
        HomePageGridZone.Children.Add(grid);

    });

    }
// other code
}

此外,您还可以使用EventHandler来实现这一点。
而我已经实现了这个功能,可以参考下面的代码:
假设HomePage是主页面,我们可以单击HomePage上的按钮导航到AddNotePage。(add note),我们可以传递两个参数(note Headernote content)到HomePage。在这里,我们可以通过使用HomePage上的两个传递参数note Headernote content来创建Grid。

主页.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EventHandlerApp.HomePage"
              Title="HomePage"
             >
    <ContentPage.Content>
        <StackLayout x:Name="HomePageGridZone">

         <Button  Text="navigate to add note page" Clicked="Button_Clicked"/>
        </StackLayout>
      
    </ContentPage.Content>
</ContentPage>

首页.xaml.cs

public partial class HomePage : ContentPage 
    {
        public HomePage()
        {
            InitializeComponent();

        MessagingCenter.Subscribe<AddNotePage, string[]>(this,
    "Note", async (sender, values) =>
    {
        string header = values[0];
        string content = values[1];

        Grid grid = CreateGrid(header, content);
        HomePageGridZone.Children.Add(grid);

    });
        }

        void BackCall(object send, NoteModel obj)
        {

           DisplayAlert("Alert", "add a new grid", "OK");

            // here we can get the passed parameter s
            if (send != null && obj!=null)
            {
                Grid grid = CreateGrid(obj.Header, obj.NoteContent);
                HomePageGridZone.Children.Add(grid);
            }
        }

        public Grid CreateGrid(string header, string note)
        {
            Label headerLabel = new Label();
            Label noteLabel = new Label();
            headerLabel.Text = header;
            noteLabel.Text = note;

            //Grid noteGrid = new Grid();
            Grid noteGrid = new Grid
            {
                RowSpacing = 0,
                ColumnSpacing = 0,
                RowDefinitions =
            {
                new RowDefinition(),
                new RowDefinition(),
            },
              ColumnDefinitions =
            {
                new ColumnDefinition(),
            }
            };

            noteGrid.Children.Add(headerLabel,0,0);
            noteGrid.Children.Add(noteLabel,0,1);
            noteGrid.WidthRequest = 50;
            noteGrid.HeightRequest = 50;
            noteGrid.BackgroundColor = Color.Red;
            return noteGrid;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            AddNotePage page = new AddNotePage();
            page.mEventHandler += delegate (object s, NoteModel a)
            {
                BackCall(s, a);
            };

            Navigation.PushAsync(page);
        }
    }

AddNotePage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EventHandlerApp.AddNotePage"
             Title="AddNotePage"
             >
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <Label Text="add a new note"
                VerticalOptions="Start" 
                HorizontalOptions="CenterAndExpand" />

            <StackLayout   Orientation="Vertical" >

                <StackLayout Orientation="Horizontal">
                    <Label Text=" header: " WidthRequest="60" ></Label>
                    <Entry x:Name="mHeaderEntry"  Placeholder="header"  WidthRequest="200"  />
                </StackLayout>

                <StackLayout Orientation="Horizontal">
                    <Label Text="note: " WidthRequest="60" ></Label>
                    <Entry x:Name="mNoteEntry"  Placeholder="note" WidthRequest="200" />
                </StackLayout>
            </StackLayout>

            <Button
                CornerRadius="30"
                Text="Take Note"
                x:Name="TakeNoteBtn"
                Margin="20, 30, 20, 0"
                Clicked="TakeNoteBtn_Clicked"
               />

           <Button 
            Text="Take Note with messagecenter"
            x:Name="TakeNoteBtn2"
            Margin="20, 30, 20, 0"
            Clicked="TakeNoteBtn_Clicked_withMessageCenter"
           />

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

AddNotePage.xaml.cs

public partial class AddNotePage : ContentPage 
{

    public  EventHandler<NoteModel> mEventHandler; // define a EventHandler

    public AddNotePage()
    {
        InitializeComponent();
    }

    private void TakeNoteBtn_Clicked(object sender, EventArgs e)
    {
        //Create grid

        EventHandler<NoteModel> handler = mEventHandler;
        if (handler != null)
        {
            NoteModel model = new NoteModel();
            model.Header = mHeaderEntry.Text;
            model.NoteContent=mNoteEntry.Text;

            mEventHandler(this, model);

            Navigation.PopAsync();
        }
    }

    private void TakeNoteBtn_Clicked_withMessageCenter(object sender, EventArgs e) 
    {
        //Create grid
        //method 2
        string[] values = { mHeaderEntry.Text, mNoteEntry.Text };

        MessagingCenter.Send<AddNotePage, string[]>(this, "Note", values);

        Navigation.PopAsync();
    }

}

NoteModel.cs

public class NoteModel 
{
    public string Header { get; set; }
    public string NoteContent { get; set; }
}

相关问题