【问题标题】:Passing parameters to a Page using constructors使用构造函数将参数传递给页面
【发布时间】:2013-06-06 09:10:53
【问题描述】:

在我的应用程序中,我在导航窗口上放置了两个页面。

第 1 页主页面,顶部有三个按钮(如功能区菜单)和一个框架(在页面的后半部分,用于在框架内导航)

第 2 页 - 是当点击第 1 页中的按钮时将在框架内定向的第二个页面。

在第 2 页中,我有一个 Datagrid 显示用于测试环境的服务器列表 - TestServer1 .. TestServerN,用于开发 -DevServer1...DevServern 和用于生产 -PrdServer1..PrdServerN。

目前,当这些按钮被点击时,所有的服务器都会显示在数据网格中,但我关心的是按以下顺序显示,

当用户点击时

tstbutton(测试) - 它应该导航到第 2 页并仅显示属于测试系统的服务器。

devbutton (dev)- 它应该导航到第 2 页并仅显示属于开发系统的服务器。

prdbutton (prd) - 它应该导航到第 2 页并仅显示属于生产系统的服务器。

我尝试了以下方法,但我只能通过构造函数传递参数来实现一台服务器。

Page1.cs

public partial class Page1 : Page
{
public Page1()
{
 InitializeComponent();
}

private void PRD_Btn_Click(object sender, RoutedEventArgs e)
{
//  Server x = new Server();
Page2 pg = new Page2();
this.NavigationService.Navigate(pg);
}

private void TST_Btn_Click(object sender, RoutedEventArgs e)
{
// this works, only if I pass one paramaterized constructor
Server s = new Server();
Page2 pg = new Page2(s);
this.NavigationService.Navigate(pg);
}

private void DEV_Btn_Click(object sender, RoutedEventArgs e)
{
//  Server y = new Server();
Page2 pg = new Page2();
this.NavigationService.Navigate(pg);
}
}

Page2.cs

public partial class Page2: Page
{       
public Page2()
{
InitializeComponent();
}   
public Page2(Server s)
{
Db_Entities db = new Db_Entities();
string tst = "TEST";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(tst)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 
}

上面的代码工作正常.. 但是如果我取消注释 prdtst 和 devtst 按钮的其他两个单击事件的代码,并且我将 tha 参数与 (Server s) 一起传递.. 有歧义和错误.. 如何为另外两台服务器实现?有什么想法吗??

编辑

当我将这些额外的块添加到我的代码中时,我有以下错误

public Page2(Server x)
{
Db_Entities db = new Db_Entities();
string prd = "PRD";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(prd)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 
public Page2(Server x)
{
Db_Entities db = new Db_Entities();
string dev = "DEV";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(dev)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 

错误 1 - 我收到此错误 2 次 类型“FN_UI.Views.Page2”已经定义了一个名为“Page2”的成员,具有相同的参数类型

错误 2 - 我收到此错误 3 次 以下方法或属性之间的调用不明确:'FN_UI.Views.Page2.Page2(FN_UI.Server)' 和 'FN_UI.Views.Page2.Page2(FN_UI.Server)'

完整代码的链接 - https://gist.github.com/userXemY/c477c25c0c1641470c35

【问题讨论】:

  • 你能详细解释一下吗? (顺便说一句,模棱两可的方法覆盖通常是编译器关心的问题,一定要避免它们)。而且,page2 缺少关键字类
  • 好的,我将粘贴我的整个代码..以便您有一个想法..
  • 我已将代码添加到我的问题中,以供完整参考,请查看此链接 - gist.github.com/userXemY/c477c25c0c1641470c35
  • 就像我说的。您有三个具有相同签名的构造函数。这是不好的。你必须重新设计它们。没有编译器会接受这个(我知道)。
  • 是的,你听起来不错.. 但是如何传递参数?

标签: c# wpf wpf-controls wpfdatagrid


【解决方案1】:

一个类中不能有三个具有相同参数类型的构造函数。

你真的在任何地方使用Server s构造函数参数吗?

无论如何,只写一个构造函数,将搜索字符串添加为另一个参数:

public Page2(Server s, string what)
{
    InitializeComponent(); // and don't forget this

    Db_Entities db = new Db_Entities();
    var query1 = (from a in this.db.Servers
                  where a.ServerID.Contains(what)
                  orderby a.ServerID
                  select a).ToList();
    datagrid1.ItemsSource = query1.ToList();

    // where is s used?
} 

然后这样称呼它:

Server s = new Server();
Page2 pg = new Page2(s, "TEST");

Server s = new Server();
Page2 pg = new Page2(s, "PRN");

Server s = new Server();
Page2 pg = new Page2(s, "DEV");

【讨论】:

  • 每个系统的查询都不同(tst/prd 和 dev).. 那么我必须放置三个不同的查询吗?
  • 对不起,“s”没有在任何地方使用..我的错误
  • Vielen Dank für Ihren Vorschlag :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2015-10-11
相关资源
最近更新 更多