简体   繁体   English

获取不是MainWindow WPF的窗口

[英]Get window that is NOT MainWindow WPF

I want to call the method from the codebehind of a window that is NOT the MainWindow in my WPF application, casting the window type as I do it. 我想从WPF应用程序中不是MainWindow的窗口的代码背后调用该方法,并在执行此操作时强制转换窗口类型。

ClientCallBack.cs: ClientCallBack.cs:

using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ChatClient
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ClientCallback : IClient
    {
        public void GetMessage(string message, string userName)
        {
            //get casted instance of chat client window (NOT MainWindow!)
        }
    }
}

ChatWPFClient.xaml.cs: ChatWPFClient.xaml.cs:

using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ChatClient
{
    /// <summary>
    /// Interaction logic for ChatWPFClient.xaml
    /// </summary>
    public partial class ChatWPFClient : Window
    {
        public static IChattingService Server;
        private static DuplexChannelFactory<IChattingService> _channelFactory;
        public ChatWPFClient()
        {
            InitializeComponent();
            _channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint");
            Server = _channelFactory.CreateChannel();

        }

        private void sendMessage(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Not available yet!");
        }

        public void TakeMessage(string message, string userName)
        {
            chatBox.Text += userName + ": " + message + "\n";
        }
    }
}

How can I call the TakeMessage of this method in the other class so I can use that codebehind window to populate the XAML file for ChatWPFClient.xaml? 如何在另一个类中调用此方法的TakeMessage,以便可以使用该代码隐藏窗口为ChatWPFClient.xaml填充XAML文件? Thanks in advance! 提前致谢!

First create an interface that you can pass to the ClientCallback 首先创建一个可以传递给ClientCallback的接口

public interface IMessageHandler
{
    void TakeMessage(string message, string userName);
}

Then in the ClientCallBack take the interface as a parameter in the constructor. 然后在ClientCallBack中,将接口作为构造函数中的参数。

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
    private IMessageHandler messageHandler;

    public ClientCallBack(IMessageHandler messageHandler)
    {
        this.messageHandler = messageHandler;
    }

    public void GetMessage(string message, string userName)
    {
        messageHandler.TakeMessage(message, userName);
    }
} 

Use the interface for the ChatWpfClient and pass the instance in the constructor. 使用ChatWpfClient的接口,并在构造函数中传递实例。

public partial class ChatWPFClient : Window, IMessageHandler
{
    ... 

    public ChatWPFClient()
    {
        InitializeComponent();
        _channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(this), "ChattingServiceEndpoint");
        Server = _channelFactory.CreateChannel();

    }

    ...

    // This is a part of the interface now and needs to be implemented here
    public void TakeMessage(string message, string userName) 
    {
        chatBox.Text += userName + ": " + message + "\n";
    }
}

Also you could just implement the IClient on your ChatWPFClient class and decorate with the CallBackBehavior attribute and just pass itself as the callback. 另外,您可以只在ChatWPFClient类上实现IClient并用CallBackBehavior属性进行装饰,然后将自身作为回调传递。 But don't think this is recommended, seems weird. 但是不要认为这是推荐的,似乎很奇怪。

_channelFactory = new DuplexChannelFactory<IChattingService>(this, "ChattingServiceEndpoint");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM