如何调用在另一个页面的 xaml.cs 中定义的对象(不是新的)

How to Call an Object Defined Within Another Page's xaml.cs (without it being new)

我的问题很简单,但是有两个方面

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

点击后SearchBtn1Clk

  1. TextBox 读取输入,将其转换为 int 并将其设置为变量。
  2. 使用该变量将其与 DataTable 中的 row 的主键内容相匹配。
  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 和绑定将是:

  1. 创建模型,那是你的MainDataTableclass
  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 所以让我们在 MainWindow.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>

老实说,我不知道发生了什么,我无法 post 其余的代码。
通过此 Git Hub Link

,您可以在 GitHub 上使用它