如何将对象从MainWindow传递到我的自定义类 [英] How to pass object from MainWindow to my custom class

查看:120
本文介绍了如何将对象从MainWindow传递到我的自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MainWindow中,我有

In my MainWindow, I have

public partial class MainWindow : Window
    {
        public List<ChannelResource> activeChannels = new List<ChannelResource>();





在我的自定义中class,



In my custom class,

public class Dial
   {
       public ChannelResource m_ChannelResource;
       public VoiceResource m_VoiceResource;
       private TelephonyServer m_TelephonyServer;

       public Dictionary<string, string> dict = new Dictionary<string, string>();

       public OutboundDial(TelephonyServer telephonyServer)
       {
           m_TelephonyServer = telephonyServer;
       }
       public Dictionary<string, string> RunScript(AppointmentReminder callData, CancellationToken cToken)
       {
           try
           {
               try
               {
                   m_ChannelResource = m_TelephonyServer.GetChannel();



我想要的是访问/传递activeChannels到方法 RunScript 。所以我可以添加如下:


What I want is to access/pass activeChannels to the method RunScript. So I can add it such as:

activeChannels.Add(m_ChannelResource);





谢谢。



编辑TPL代码:2014年10月30日下午2:14



Thank you.

EDIT for TPL code: 2:14 PM 10/30/2014

private CancellationTokenSource cTokenSource;
private CancellationToken cToken;

 private void Start_Click(object sender, RoutedEventArgs e)
 {
    cTokenSource = new CancellationTokenSource();
    cToken = cTokenSource.Token;

    var producer = Producer();
    var consumer = Consumer();
    cToken.Register(() =>
    {
        foreach (ChannelResource cr in activeChannels)
        {
                // Stop dial on every channel one at a time.
               cr.StopDial();
        }
   });
 }



对于客户部分:


And for the customer part:

async Task Consumer()
{
    try
    {
       var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
       {
            MaxDegreeOfParallelism = 5,
            CancellationToken = cToken
       };
       var consumerBlock = new ActionBlock<AppointmentReminder>(
remainder =>
{
    if (cToken.IsCancellationRequested)
        return;
    OutboundDial dial = new OutboundDial(ts);
    Dictionary<string, string> dict = dial.RunScript(remainder);
    activeChannels.Add(dial.m_ChannelResource); // but m_ChannelResource is null
    },
executionDataflowBlockOptions);

    m_Queue.LinkTo(
consumerBlock, new DataflowLinkOptions { PropagateCompletion = true });
    await consumerBlock.Completion;
}

推荐答案

你不能真正做到这一点,不是没有让事情变得复杂的事情远远超出合理的水平:通用的拨号类不应该知道任何与表单相关的(或其他用户界面细节)类都存在,更不用说它们包含的内容了。

相反,你的Dial类应该创建一个表单处理的事件和表单然后将数据添加到它的集合中。



听起来很复杂,我知道 - 但事实并非如此!

见这里:在两个表格之间传递信息,第2部分:孩子到父母 [ ^ ] - 拨号是孩子,表格是父母。





@ OriginalGriff,问题是通用的Dial类用于TPL(任务p并行库),说一个ActionBlock中的多个拨号。我处理MainWindow的并行性。换句话说,我可能需要父母表格中的这个集合。





当你发出信号时, form类获取Dial类的实例,像往常一样将其作为sender参数引发:

You can't really do that, not without complicating things far beyond sensible levels: the generic Dial class shouldn't know that any form related (or other user interface detail) classes even exist, much less what they contain.
Instead, your Dial class should create an event which the form handles, and the form then adds the data to it's collection.

Sounds complicated, I know - but it really isn't!
See here: Transferring information between two forms, Part 2: Child to Parent[^] - Dial is the child, the Form is the parent.


"@OriginalGriff, the thing is that the generic Dial class is used in TPL(task parallel library), say multiple Dials in an ActionBlock. I handle the parallelism from the MainWindow. In another word, I may need this collection from the parent form."


And when you signal the event, the form class gets the instance of the Dial class that raised it as the sender parameter as usual:
void child_DataAvailable(object sender, EventArgs e)
    {
    Dial dial = sender as Dial;
    if (dial != null)
        {
        activeChannels.Add(dial.m_ChannelResource);
        }
    }


这篇关于如何将对象从MainWindow传递到我的自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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