C# 如何使按钮执行非';主页中的t
我在主页上有一个按钮,我想执行另一个类(Shelf.cs)中存在的方法 我试着做click=“local:Button\u click”(local=using:Shelf),它不让我这么做,一直想添加一个“新的事件处理程序” MainPage.xaml中的按钮:C# 如何使按钮执行非';主页中的t,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,我在主页上有一个按钮,我想执行另一个类(Shelf.cs)中存在的方法 我试着做click=“local:Button\u click”(local=using:Shelf),它不让我这么做,一直想添加一个“新的事件处理程序” MainPage.xaml中的按钮: <Button x:Name="AddBookButton" Width="150" Height="150" Margin="675,0,0,0" click="Button_click"> // this create
<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中的方法?或者可能将活动安排在那里?如果我理解你想说的话,你就不能真正做到你想做的事(就我所知)。。。但是,您可以在邮件文件中使用按钮单击方法,在单独的类文件中调用另一个方法
如果我对你的问题理解错误,那么你能进一步解释一下吗?你需要在
MainForm
类中实例化Shelf
类,然后调用Shelf::ShelfMethod
。名字是我的名字-见下面的示例代码。我的笔记本电脑还不能执行UWP
(需要升级到Windows 1809),但这里有一个Winforms
示例。我认为实例化一个类和调用一个方法的基础在这两方面可能是相同的
在表单上,添加一个名为objButton
的按钮
和一个名为objTextBox的textbox
我将表单类命名为MainForm
我将我的项目命名为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());
}
}
}
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();
}
}
}
namespace CallOtherClassMethod
{
class Shelf
{
//Constructor (does nothing in this example)
public Shelf()
{
}
public string ShelfMethod()
{
return "Text sent by Shelf::ShelfMethod";
}
}
}
Shelf.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());
}
}
}
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();
}
}
}
namespace CallOtherClassMethod
{
class Shelf
{
//Constructor (does nothing in this example)
public Shelf()
{
}
public string ShelfMethod()
{
return "Text sent by Shelf::ShelfMethod";
}
}
}
最正确、最好的方法是通过创建MVVM模式和使用大量数据绑定,将项目分为三层
创建AppViewModel类(实现InotifyPropertyChanged),在MainPage.cs中为其创建字段,还将MainPage类声明为静态字段,将其用作整个应用程序的“shell”。。。这样做很好:在AppViewModel中,您可以实现或引用所有其他名称空间
向你问好,我想你得到了我想要的。你是说我在主页上调用已经在Shelf.csYeah中编写的方法!我想我明白了。所以基本上在MainPage中的单独类中调用该方法。