(4) Analysis of the trendlineview in the market Module

Source: Internet
Author: User

In this chapter, we add the market module to ri. This chapterCodeDownload: myri_1.zip

 

Requirement Analysis:

To implement a view, we can name it trendlineview. XAML according to the selection of positionsummaryview. A ready-made linechart is used here, which comes from stocktraderri. chartcontrols. We only need to bind its itemssource attribute to the markethistory collection. The other text to be bound is the text in the upper left corner. The text is changed accordingly according to the items selected in positionsummaryview, for example, stock2.

In addition, in order to simulate the stock changes in the real world, a timer is created in the source code provided by Prism to randomly generate numbers, this affects the changes in the data in the gridview in positionsummaryview-which undoubtedly increases the difficulty of understanding the prism framework. So, in myProgram.

 

Preparations

1. Create a data folder and create two data files: Market. xml and markethistory. xml. Open the resource file resources. resx of the project, select the file option, and copy the two data files.

2. Create a service folder and create two interfaces, imarketfeedservice and imarkethistoryservice, to prepare for future dependency injection.

Public interfaceImarketfeedservice{DecimalGetprice (StringTickersymbol );LongGetvolume (StringTickersymbol );BoolSymbolexists (StringTickersymbol );}

Public interfaceImarkethistoryservice{MarkethistorycollectionGetpricehistory (StringTickersymbol );}

As you can see, all the methods provided by the interface interact with the data source.

3. In the service folder, create two services, marketfeedservice and markethistoryservice, which are derived from the above two interfaces to read the above two data files.

The following is an example of marketfeedservice debugging.

The following describes how to design the trendlineview.

1. This time we use the VM mode, that is, only view and model, and integrate the presenter into the model. Data Binding between controls in V and M is also implemented (through datacontext ).

To this end, you need to create the itrendlineview and itrendlinepresentationmodel interfaces, and create the trendlineview and trendlinepresentationmodel interfaces to implement the preceding interfaces respectively. The trendlineview is also a usercontrol interface.

2. In addition, we adopt the presenter-first (instead of view-first) method. First, modify the marketmodule, add dependency injection, and register the mapping relationship between interfaces and implementation classes.

Note that when registering the mapping relationship between region and view

Regionviewregistry. registerviewwithregion ("Marketregion",Typeof(Views.Marketview));

But now we want to change it

This. Regionmanager. registerviewwithregion (Regionnames. Researchregion, () =>This. Container. Resolve <Itrendlinepresentationmodel> (). View );

That is to say, replace iregionviewregistry with iregionmanager. I have read the prism implementation source code and found that there is no difference between the two. You can use either of them. Here we use iregionmanager, which is consistent with the original version.

The difference between P-first and V-first lies only in the 2nd parameters of the registerviewwithregion method. For typeof (views. marketview), that is, first enter the view and then instantiate the presenter in the view.

This. Container. Resolve <itrendlinepresentationmodel> (). View

That is to say, first instantiate Presenter (here is model), then instantiate view in presenter and return. This produces the order in which constructors of the two are executed.

3. Design the itrendlineview and itrendlinepresentationmodel interfaces.

Public interfaceItrendlineview{ItrendlinepresentationmodelModel {Get;Set;}}

Public interfaceItrendlinepresentationmodel{ItrendlineviewView {Get;}StringTickersymbol {Get;}MarkethistorycollectionHistorycollection {Get;}}

We should follow this design principle. First, V and m must be referenced to each other, that is, the model and view attributes in these two interfaces. Second, you need to put the attributes used for binding in the model into the interface, where tickersymbol and historycollection are used.

4. Design trendlineview

View is generally very simple. It is bound to a space in XAML. In the background CS file, implement the model attribute and bind it to the datacontext of this usercontrol.

5. design the trendlinepresentationmodel

The specific logic is here. First, if there is a linkage attribute, the model will implement the inotifypropertychanged interface. Here, tickersymbol and historycollection are both linkage attributes. Second, it is the constructor:

PublicTrendlinepresentationmodel (ItrendlineviewView,ImarkethistoryserviceMarkethistoryservice,IeventaggregatorEventaggregator ){This. View = view;This. View. Model =This;This. _ Markethistoryservice = markethistoryservice; eventaggregator. getevent <Tickersymbolselectedevent> (). Subscribe (This. Tickersymbolchanged );}

We should follow this design principle: Since it is P-first, we need to create a view in the model constructor:

This. View = view;

And specify the model attribute of this view as self:

This. View. Model =This;

Well, there are two confusing sentences, right? -- But is indispensable.

The final part of the constructor is to subscribe to the tickersymbolselectedevent event. This event is triggered when we select different rows in the positionsummaryview's gridview. Therefore, the pie chart changes with the data source. Considering this event will also be used in the positionsummaryview project, we put it in the class library stocktraderri. Infrastructure.

 

At this point, the market has been created, but no data is displayed after the program is run, because the market module is only a subscriber. That is to say, only when the source positionsummaryview implements the publish event will the data in the trendlineview be seen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.