如何调用在另一个页面中定义的对象(没有它是新的)

本文关键字:对象 定义 调用 何调用 另一个 | 更新日期: 2023-09-27 18:15:53

我的问题很简单,但是是双面的

我知道如何通过class.method调用方法,但我对如何从另一个页面调用对象感到困惑。我要做的是如下

点击SearchBtn1Clk

  1. TextBox读取输入,将其转换为int并设置为变量
  2. 使用该变量将其与DataTablerow的主键内容相匹配。
  3. 导航到新页面
  4. 将找到的行中的每个单元格设置为新页面上的单个标签。

4是我遇到问题的地方。我已经花了整整三天的时间浏览了其他的帖子、例子、教程和MSDN定义,但我仍然不能弄清楚。其中一个问题实际上是获得的变量持有用户输入到另一个页面,而没有它是new的另一个问题是能够设置该行的每个单元格来分离变量。

MainDataTable.cs

    public class MainDataTable
{
    public static DataTable dataMain = new DataTable("Customer Info Database");
    public static void CreateTable1()
    {

        dataMain.Columns.Add("CustID", typeof(int));
        dataMain.PrimaryKey = new DataColumn[] { dataMain.Columns["CustID"] };
        dataMain.Columns.Add("CustName", typeof(string));
        dataMain.Columns.Add("CustAge", typeof(int));
        dataMain.Columns.Add("CustAlign", typeof(string));
        DataSet MainSet = new DataSet("CustAcctsDataSet");
        MainSet.Tables.Add(dataMain);   
    }
    public static void EnterNewRows(int CustID, string CustName, int CustAge, string CustAlign)
    {
        dataMain.Rows.Add(CustID, CustName, CustAge, CustAlign);
    }

Page2.xaml.cs

 public partial class Page2 : Page
{
    public Page2()
    {
        InitializeComponent();
    }
    public void SearchBtn1Clk(object sender, RoutedEventArgs e)
    {

        int IDFind = Convert.ToInt32(searchIdTxtBox.Text);
        DataRow foundRow = MainDataTable.dataMain.Rows.Find(IDFind);
        if (foundRow != null)
        {
            MessageBox.Show(foundRow.ToString());
        }
        if (foundRow == null)
        {
            MessageBox.Show("No Customer Found with ID: " + IDFind);
        }
        this.NavigationService.Navigate(new Page3());
    }

Page3.xaml.cs

  public partial class Page3 : Page
{
    public Page3(Page2 pg2r)
    {
        pg2r = this.Page2();
        InitializeComponent();

    }
    public void SetLabels (Page2 page)
    {
        // pg2r.SearchBtn1Clk
        CustAgeLab.Content = "";
        CustIDLab.Content = "";
        CustNameLab.Content = "";
        CustAlgnLab.Content = "";
    }
    private void ReturntoMainMenu(object sender, RoutedEventArgs e)
    {
        this.NavigationService.Navigate(new Page0());
    }
}

谢谢!

如何调用在另一个页面中定义的对象(没有它是新的)

为了让您在使用WPF时充分利用MvvM和Binding,将:

  1. 创建模型,这是你的MainDataTable
  2. 创建视图模型,负责为视图提供数据
  3. 创建视图,这部分你已经有了

下面是ViewModel的样子:

namespace WpfApplication1
{
    public class MainVM : INotifyPropertyChanged
    {
        public MainVM()
        {
            InitialiseComponents();
        }
        private void InitialiseComponents()
        {
            LoginCommand = new RelayCommand(loginCommandMethod);
        }
        private string searchKey;
        public string SearchKey
        {
            get { return searchKey; }
            set { searchKey = value; OnPropertyChanged("SearchKey"); }
        }
        private RelayCommand _loginCommand;
        public RelayCommand LoginCommand
        {
            get { return _loginCommand; }
            private set { _loginCommand = value; }
        }
        private void loginCommandMethod(object parameter)
        {
            // do something
        }
        #region INotify
        public void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        public event PropertyChangedEventHandler PropertyChanged; 
        #endregion
    }
}

下面是RelayCommand:

namespace WpfApplication1.Commands
{
    public class RelayCommand : ICommand
    {
        private Action<object> _execute;
        private Predicate<object> _canExecute;
        public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
        {
            _execute = execute;
            _canExecute = canExecute;
        }
        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public void Execute(object parameter)
        {
            _execute.Invoke(parameter);
        }
    }
}  
在这一点上,你有Page2。xaml和ViewModel,让我们让它们一起工作:
<Page x:Class="WpfApplication1.Views.Page2"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WpfApplication1.Views"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="Page2">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding SearchKey, UpdateSourceTrigger=PropertyChanged}"/>
    <Button Content="Search" Command="{Binding SearchCommand}"/><!--There is an example of command in VM already called login command-->
</Grid>

现在为了让你使用Page,你需要使用Frame,所以让我们在主窗口中调用它。xaml :
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:views="clr-namespace:WpfApplication1.Views"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<!--<Window.DataContext>
    <local:MainVM/>
</Window.DataContext>-->
<Window.Resources>
    <local:MainVM x:Key="mainVM"/>
    <local:LoginPage x:Key="login" DataContext="{StaticResource mainVM}"/>
    <views:Page2 x:Key="SearchPage" DataContext="{StaticResource mainVM}"/>
    <!---->
    <ControlTemplate x:Key="ctrlTmpl">
        <local:LoginPage/>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <!--<Button x:Name="button" Content="Do something" Click="btnDoSomething" HorizontalAlignment="Left" Margin="221,60,0,0" VerticalAlignment="Top" Width="75"/>-->
    <!--<Control Template="{StaticResource ctrlTmpl}"/> This works-->
    <Frame Content="{StaticResource SearchPage}"/>
</Grid>

老实说,我不知道发生了什么,我不能张贴其余的代码。
你可以在GitHub上通过GitHub Link