Page 35 - MSDN Magazine, May 2018
P. 35
Figure 17 Submitting a Message
private async void RegisterUser() {
if(!App.SessionManager.IsHost)
await App.SessionManager.SendMessage("user", App.SessionManager.CurrentUser,
App.SessionManager.Host);
}
The final piece of the view model is to broadcast a new mes- sage from the user. The SubmitMessage method constructs a new UserMessage and calls the BroadcastMessage method on the RemoteSessionManager. It then clears out the NewMessage value and raises the MessageAdded event, as shown in Figure 17.
In the codebehind for MessagePage, shown in Figure 18, I need to do two things: create an instance of the MessageViewModel for the XAML to reference and handle the MessageAdded event. In the event handler I instruct the ListView to scroll to the bottom of the list where the latest message is visible.
Like MainPage, MessagePage will need a view model.
The Team Messaging app should now be ready to run. On one machine run the app and create a new session. Then launch the app on a second machine, which should show the newly created message. Once you join the session you’ll be brought to the new message page where you can begin chatting with others in the ses- sion, as shown in Figure 19. You have now created a multiuser app using the Remote System API.
Wrapping Up
Creating successful user experiences within apps often requires looking beyond a single device or platform or even user. Microsoft developed Project Rome to enable developers to provide this level of experience within their apps. In this article I built a UWP app using the Remote Systems API; however, by using the Project Rome SDKs available for other platforms, you could extend this app to work on multiple platforms. When building the next great experience for your users, remember to consider how Project Rome can help you make your app more personal. The source code for this article can
be found at bit.ly/2FWtCc5. n
Tony Champion is a software architect with more than 20 years of experience developing with Microsoft technologies. As the president of Champion DS and its lead software archi- tect, he remains active in the latest trends and technologies, creating custom solutions on Microsoft platforms. His list of clients span multiple industries and includes companies such as: Schlumberger, Microsoft, Boeing, MLB and Chevron/Philips. Champion is an active participant in the community as a six-year Microsoft MVP, international speaker, published author and blogger.
Thanks to the following Microsoft technical expert who reviewed this article: Shawn Henry
May 2018 31
public async void SubmitMessage() {
var msg = new UserMessage() {
User = App.SessionManager.CurrentUser, DateTimeStamp = DateTime.Now,
Message = NewMessage
};
await App.SessionManager.BroadcastMessage("message", msg); Messages.Add(msg);
NewMessage = "";
MessageAdded(this, null); }
Figure 18 MessagePage Codebehind
public sealed partial class MessagePage : Page {
public MessagePage() {
this.InitializeComponent();
ViewModel.MessageAdded += OnMessageAdded; }
private void OnMessageAdded(object sender, EventArgs e) {
lvMessages.ScrollIntoView(ViewModel.Messages.Last()); }
public MessageViewModel ViewModel { get; } = new MessageViewModel(); }
send as a new message. There is also a single event, MessageAdded, that will be used by the codebehind in MessagePage. In the con- structor of the view model, I need to map the Users property, invoke the StartReceivingMessages method in RemoteSession- Manager, and register for the MessageReceived event, as shown in Figure 16. The constructor also includes the implementation of INotifiyPropertyChanged.
In the constructor there’s a call to RegisterUser. This method will send the CurrentUser that was created when joining a session to the host. This announces to the host that a new user has joined and what the display name is. In response, the host will send out the current list of users to be displayed in the app:
Figure 19 Multiuser Messaging msdnmagazine.com