如何让按钮执行不在 MainPage 中的方法

How to make a button do a method that isn't in MainPage

我在 MainPage 中有一个按钮,我想执行另一个 class (Shelf.cs)

中存在的方法

我试过 click="local:Button_click" (local = using:Shelf) ,它不让我一直想添加一个 "new event handler"

MainPage.xaml中的按钮:

<Button x:Name="AddBookButton" Width="150" Height="150" Margin="675,0,0,0" click="Button_click"> // this creates a new method inside MainPage

Shelf.cs中的方法:

public async void Button_Click(object sender, RoutedEventArgs e)
    {
        StackPanel adddialog = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            Width = 484,
            FlowDirection = FlowDirection.RightToLeft
        };
        TextBox title = new TextBox
        {
            Header = "العنوان:",
            PlaceholderText = "عنوان الكتاب",
            Width = 156,
            Margin = new Thickness(0, 0, 8, 0),
            FlowDirection = FlowDirection.RightToLeft
        };
        TextBox author = new TextBox
        {
            Header = "المؤلف:",
            PlaceholderText = "مؤلف الكتاب",
            Width = 156,
            Margin = new Thickness(0, 0, 8, 0),
            FlowDirection = FlowDirection.RightToLeft
        };
        TextBox publisher = new TextBox
        {
            Header = "الناشر (اختياري):",
            PlaceholderText = "ناشر الكتاب",
            Width = 156,
            Margin = new Thickness(0, 0, 0, 0),
            FlowDirection = FlowDirection.RightToLeft
        };

        adddialog.Children.Insert(0, title);
        adddialog.Children.Insert(1, author);
        adddialog.Children.Insert(2, publisher);
        ContentDialog addFileDialog = new ContentDialog
        {
            Title = "أضف بيانات الكتاب الجديد",
            Content = adddialog,
            PrimaryButtonText = "إضافة",
            CloseButtonText = "إلغاء",
            FlowDirection = FlowDirection.RightToLeft,
            Width = 700
        };
        ContentDialogResult result = await addFileDialog.ShowAsync();

        if (result == ContentDialogResult.Primary)
        {
            Book book = new Book()
            {
                Title = title.Text,
                Author = author.Text,
            };
            try
            {
                book.Publisher = publisher.Text;
            }
            catch { }
            Books.Add(book);
            AddBookButton.Visibility = Visibility.Collapsed;
        }
    }
}

如何让它转到shelf.cs中的方法?或者可能将事件路由到那里?

如果我明白你想说什么,你就不能真正做你想做的事(据我所知)......但是,你可以在邮件文件中制作按钮点击方法,在单独的 class 文件中调用另一个方法。

如果我理解你的问题有误,请你进一步解释一下好吗?

您需要在MainFormclass中实例化Shelfclass,然后调用Shelf::ShelfMethod。名字是我的名字 - 请参见下面的示例代码。我的笔记本电脑还不能 UWP(需要升级到 Windows 1809),但这里有一个 Winforms 的例子。我认为实例化 class 和调用方法的基础知识在

中可能是相同的

在表格中,添加名为 objButtonbutton 和名为 objTextBox

textbox

我将表格命名为classMainForm

我将我的项目命名为CallOtherClassMethod

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CallOtherClassMethod
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainPage());
        }
    }
}

MainPage.cs

using System.Windows.Forms;

namespace CallOtherClassMethod
{
    public partial class MainPage : Form
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void ObjButton_Click(object sender, System.EventArgs e)
        {
            Shelf objShelf = new Shelf();
            objTextBox.Text = objShelf.ShelfMethod();
        }
    }
}

Shelf.cs

namespace CallOtherClassMethod
{
    class Shelf
    {
        //Constructor (does nothing in this example)
        public Shelf()
        {
        }

        public string ShelfMethod()
        {
            return "Text sent by Shelf::ShelfMethod";
        }
    }
}

执行此操作(以及其他操作)的最正确和最佳方法是通过创建 MVVM 模式并使用海量数据绑定将项目分为三层。

您创建了一个 AppViewModel class(它实现了 INotifyProperyChanged),您在 MainPage.cs 中为其创建了一个字段,您还声明了 MainPage class 作为静态字段,使用它作为整个应用程序的“shell”...您很高兴:在 AppViewModel 中,您可以实现或引用所有其他名称空间。

此致