如何使用MVVM从WPF中的Web插座显示数据?

发布于 2025-02-02 12:09:22 字数 1836 浏览 5 评论 0原文

我想通过Web插座获取数据并将其显示在WPF应用程序中。我正在使用caliburn.micro进行MMVM。这是我的代码:

视图模型:

 private static WSFuturesResponse futures_response = WSFuturesResponse.GetInstance();

        private double? _last = futures_response.data.last;

        public double? Last
        {
            get
            {
                return _last;
            }
            set
            {
                _last = value;
                NotifyOfPropertyChange(() => Last);
            }
        }
     


这是Web套接字事件:

public void WebsocketOnMessageReceive(object o, MessageReceivedEventArgs messageReceivedEventArgs)
        {
            // FuturesResponse is a property in this class that I got with 'public WSFuturesResponse FuturesResponse =  WSFuturesResponse.GetInstance();'
            FuturesResponse = JsonConvert.DeserializeObject<WSFuturesResponse>(messageReceivedEventArgs.Message);

        }

这是XAML代码:

<TextBlock Text="{Binding Path=Last, Mode=OneWay}" Grid.Row="1" Grid.Column="3"></TextBlock>

最后,wsfuturesResponse类别

  public class WSFuturesResponse
    {
        public static WSFuturesResponse ItsInstance;
        public string channel { get; set; }
        public string market { get; set; }
        public string type { get; set; }
        public WSFuturesData data { get; set; } = new WSFuturesData();

        private static WSFuturesResponse CreateRootClass()
        {
            if (ItsInstance == null)
            {
                ItsInstance = new WSFuturesResponse();
            }

            return ItsInstance;
        }

        public static WSFuturesResponse GetInstance()
        {
            return CreateRootClass();
        }
    }

与Web套接字的连接工作,数据又回来了。我该如何显示?谢谢!

I want to get data through a web socket and display it in a WPF application. I am using Caliburn.Micro for the MMVM. Here is my code:

The view model:

 private static WSFuturesResponse futures_response = WSFuturesResponse.GetInstance();

        private double? _last = futures_response.data.last;

        public double? Last
        {
            get
            {
                return _last;
            }
            set
            {
                _last = value;
                NotifyOfPropertyChange(() => Last);
            }
        }
     


Here is the web socket event:

public void WebsocketOnMessageReceive(object o, MessageReceivedEventArgs messageReceivedEventArgs)
        {
            // FuturesResponse is a property in this class that I got with 'public WSFuturesResponse FuturesResponse =  WSFuturesResponse.GetInstance();'
            FuturesResponse = JsonConvert.DeserializeObject<WSFuturesResponse>(messageReceivedEventArgs.Message);

        }

Here is the xaml code:

<TextBlock Text="{Binding Path=Last, Mode=OneWay}" Grid.Row="1" Grid.Column="3"></TextBlock>

Lastly the class WSFuturesResponse

  public class WSFuturesResponse
    {
        public static WSFuturesResponse ItsInstance;
        public string channel { get; set; }
        public string market { get; set; }
        public string type { get; set; }
        public WSFuturesData data { get; set; } = new WSFuturesData();

        private static WSFuturesResponse CreateRootClass()
        {
            if (ItsInstance == null)
            {
                ItsInstance = new WSFuturesResponse();
            }

            return ItsInstance;
        }

        public static WSFuturesResponse GetInstance()
        {
            return CreateRootClass();
        }
    }

The connection to the web socket works and the data comes back. How can I display it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2025-02-09 12:09:22

您的Web套接字事件处理程序甚至没有尝试访问事件处理程序正在分配的 FuturesResponse 属性。如果要显示已分配给 futuresResponse 属性的数据,与您的GUI相关的内容需要以某种方式访问​​(读取) futuresResponse 属性。

您的代码在Web套接字事件处理程序之外仅尝试使用 wsfuturesResponse 实例创建并由 wsfuturesResponse getInstance()创建并返回。但这显然是不是 wsfuturesResponse 实例,由 jsonconvert.deserializeobject创建Web套接字事件处理程序之外的其余代码永远不会访问。

我不知道您想通过此 wsfuturesresponse getInstance()方法来实现什么。在我看来,它看起来非常好,就像您正在为响应数据实现单身模式 - 这根本没有任何意义,因为不仅收到了一个和唯一的响应。因此,顺便说一句,我希望您也尝试对自己了解(以便您可以从错误中学习)使您认为Singleton模式适用于真正不是一个实例情况的事物(不是一个响应,而是多个响应)。

Nothing outside your web socket event handler even attempts to access the FuturesResponse property your event handler is assigning. If you want to display data that's assigned to the FuturesResponse property, something related to your GUI needs to access (read) that FuturesResponse property in some way.

Your code outside the web socket event handler is only attempting to work with a WSFuturesResponse instance created and returned by WSFuturesResponse GetInstance(). But that is obviously not the WSFuturesResponse instance created by JsonConvert.DeserializeObject<WSFuturesResponse> and assigned to the FuturesResponse property the rest of your code outside the web socket event handler is never accessing.

I don't know what you are trying to realize with this WSFuturesResponse GetInstance() method of yours. It looks very much to me like you are implementing a singleton pattern for response data -- which does not make sense at all, because there is not just one and only single response ever received. Thus, and as an aside, i would like you to also try getting an understanding for yourself (so you can learn from your mistakes) of what made you believe that the singleton pattern would be applicable for something that is not really a single instance situation (not a single response, but multiple responses).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文