从另一个窗口调用方法(类)的问题 [英] Calling method from another window (Class) issue

查看:147
本文介绍了从另一个窗口调用方法(类)的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在后面的WPF应用程序的主窗口的文件代码我有quering与LINQ一个数据库,SQL和写作的结果到一个ObservableCollection的方法:

 公共无效GetStateByDate(字符串shcode)
{
MydbDataContext contextSts =新MydbDataContext();
_ShAvaQuCollection.Clear();

变种STS从contextSts.SAties那里p.ShID == shcode的选择P P =;

的foreach(在STS VAR P)
_ShAvaQuCollection.Add(新ShAvaQu
{
ShCode = p.ShID,
SiID = p.SiID,
PrCat = p.PrCat
});
}

当我打电话从隐藏文件相同的代码此方法(在同一窗口) ,一切都OK了。



如果我从另一个调用窗口此方法中,使用主窗口的instanse,的ObservableCollection是空的:

  SWindow SW =新SWindow(); 
sw.GetStateByDate(stringpar);



是什么原因呢?请问在这种情况下,方法创建的ObservableCollection的又一实例?



(我可以在 sw._ShAvaQuCollection 包含的值的调试器看到的。是西南。 _ShAvaQuCollection 不征收相同instanse为 _ShAvaQuCollection ?如果是,如何解决?)



编辑(添加)



的ObservableCollection声明是这样的:

 的ObservableCollection< ShAvaQu> _ShAvaQuCollection = 
新的ObservableCollection< ShAvaQu>();

公众的ObservableCollection< ShAvaQu> ShAvaQuCollection
{{返回_ShAvaQuCollection; }}

公共类ShAvaQu
{
公共字符串ShCode {搞定;组; }
公共字符串SiID {搞定;组; }
公众诠释PrCat {搞定;组; }
}



我调用该方法从一个窗口,其中另一个集合 ShQuCollection 通过ListView控件显示。在SelectionChanged事件处理程序,我要为这个数据库quering参数:

 私人无效ShSelList_SelectionChanged(对象发件人,SelectionChangedEventArgs E)
{
SWindow SW =新SWindow();
字符串str = sw.ShQuCollection [ShSelList.SelectedIndex] .ShCode;
sw.GetStateByDate(STR);
关闭();
}
}


解决方案

1 )最重要的是你不应该从你打电话分贝逻辑窗口/表格。你应该抽象出来到另一个类。然后,你可以有你的方法返回一个观察的集合。



然而,在你的情况我假设你正在尝试使用辅助形式来重新加载/加载的收集和你希望您的主要形式使用。这个问题是要创建的窗体的新实例,以便您的收藏被填充,但不是你的主要形式,但副本上。



有几个方法可以尝试绕开这个问题。



1),使该方法的静态和你观察的集合静态的,所以它更新一个实例。



2 )把你的主要形式的一个实例句柄到您的辅助形式,让你重新使用已有的实例。所以,你是不是所有的地方创造新的情况下,这将是可取的。



在第二种形式的构造函数,你可以在主窗口的实例传递这样那么你可以直接使用它。这应该解决您的问题。



更​​新:下面是一些代码示例。基本上有很多方法来传递一个引用



您可以用一个构造函数做这样的:

  //这是你的第二个窗口
私人窗口_parentHandle构造函数;

公共SecondWindow(窗口OBJ)
{
this._parentHandle = OBJ;
}



从有,你会打开一个窗口,这样的方式您的主要形式。然后

  SecondWindow W =新SecondWindow(本); 
w.Show();

现在你的第二个窗口有直接的句柄你的第一个窗口,以便您可以调用你的方法上变它会更新。



另一种方式是让你的第二个窗口在公共setter方法​​为好。

 公共窗口ParentContext 
{
{返回this._parentHandle; }
集合{this._parentHandle =价值; }
}



然后,你可以这样创建表单实例:

  SecondWindow W =新SecondWindow(); //所以就像普通
w.ParentContext =这一点; //实例设置为调用形式
w.Show();

这是基础。这种类型的场景中工作只是你需要传递一个参考的情景。



希望有所帮助。


In code behind file of the main window of WPF application I have a method quering a database with LINQ to SQL and writing results to an ObservableCollection:

    public void GetStateByDate(string shcode)
    {
        MydbDataContext contextSts = new MydbDataContext();
        _ShAvaQuCollection.Clear();

        var sts = from p in contextSts.SAties where p.ShID == shcode select p;

        foreach (var p in sts)
            _ShAvaQuCollection.Add(new ShAvaQu
            {
                ShCode = p.ShID,
                SiID = p.SiID,
                PrCat = p.PrCat
            });
    }

When I call this method from the same code behind file (the same window), everything is OK.

If I call this method from another window, using an instanse of the main window, ObservableCollection remains empty.:

SWindow sw = new SWindow();
sw.GetStateByDate(stringpar);

What is the reason for this? Does in this case method create yet another instance of ObservableCollection?

(I can see in debugger that sw._ShAvaQuCollection contains values. Is sw._ShAvaQuCollection not the same instanse of collection as _ShAvaQuCollection? If yes, how it can be resolved?)

Edited (added)

The ObservableCollection declared this way:

    ObservableCollection<ShAvaQu> _ShAvaQuCollection =
            new ObservableCollection<ShAvaQu>();

    public ObservableCollection<ShAvaQu> ShAvaQuCollection
    { get { return _ShAvaQuCollection; } }

    public class ShAvaQu
    {
        public string ShCode { get; set; }
        public string SiID { get; set; }
        public int PrCat { get; set; }
    }

I call the method from a window, where another collection ShQuCollection displayed through ListView. In SelectionChanged event handler I take an argument for this database quering:

private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {           
        SWindow sw = new SWindow();         
        string str = sw.ShQuCollection[ShSelList.SelectedIndex].ShCode;
        sw.GetStateByDate(str);
        Close();
    }
}

解决方案

1) Most importantly you shouldn't be calling db logic from you windows / forms. You should abstract it out into another class. Then you could have your method return a observable collection.

However in your case I am assuming that you are trying to use the secondary form to reload / load the collection and you want it used on your primary form. The problem with this is you are creating a new instance of the form so your collection is being populated but not on your main form but a copy.

There are a couple ways you can try to get around that.

1) Make the method static and your observable collection static so that it updates a single instance.

2) Pass an instance handle of your primary form into your secondary form so that you re-use the instance you already have. This would be preferable so that you are not creating new instances all over the place.

In the constructor of the second form you could pass in the instance of your primary window so then you can use it directly. This should solve your problem.

UPDATE: Here is some code samples. Basically there are many ways to pass a reference.

You could do it like this with a constructor:

// This is the constructor for your second window
private Window _parentHandle;

public SecondWindow(Window obj)
{
    this._parentHandle = obj;
}

Then from your primary form that has the method you would open that window like this.

SecondWindow w = new SecondWindow(this);
w.Show();

Now your second window has a direct handle to your first window so you can call your method on that variable and it will update.

Another way is to have a public Setter method on your second window as well.

public Window ParentContext
{
   get { return this._parentHandle; }
   set { this._parentHandle = value; }
}

Then you could create your form instance like this:

SecondWindow w = new SecondWindow();  // so just like normal
w.ParentContext = this; // set the instance to the calling form
w.Show();

That is the basics. This type of scenario works in just about any scenario where you need to pass a reference.

Hope that helps.

这篇关于从另一个窗口调用方法(类)的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆