-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathMessengerRequestPage.xaml
More file actions
85 lines (76 loc) · 3.54 KB
/
MessengerRequestPage.xaml
File metadata and controls
85 lines (76 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<Page
x:Class="MvvmSampleUwp.Views.MessengerRequestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:MvvmSampleUwp.Controls"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="Messenger" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<ScrollViewer Padding="{StaticResource DocumentationPageContentPadding}" CanContentRenderOutsideBounds="True">
<StackPanel Spacing="16">
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Using request messages'), Mode=OneWay}" />
<controls:InteractiveSample xml:space="preserve">
<controls:InteractiveSample.Content>
<StackPanel xml:space="default" Spacing="8">
<TextBlock Text="{x:Bind ViewModel.Username, Mode=OneWay}" />
<Button Click="{x:Bind ViewModel.RequestCurrentUsername}" Content="Click to request the username!" />
<Button Click="{x:Bind ViewModel.ResetCurrentUsername}" Content="Click to reset the local username!" />
</StackPanel>
</controls:InteractiveSample.Content>
<controls:InteractiveSample.XamlCode>
<StackPanel Spacing="8">
<TextBlock Text="{x:Bind ViewModel.Username, Mode=OneWay}"/>
<Button
Content="Click to request the username!"
Click="{x:Bind ViewModel.RequestCurrentUsername}"/>
<Button
Content="Click to reset the local username!"
Click="{x:Bind ViewModel.ResetCurrentUsername}"/>
</StackPanel>
</controls:InteractiveSample.XamlCode>
<controls:InteractiveSample.CSharpCode>
// Simple viewmodel for a module responding to a request username message.
// Don't forget to set the IsActive property to true when this viewmodel is in use!
public class UserSenderViewModel : ObservableRecipient
{
public string Username { get; private set; } = "Bob";
protected override void OnActivated()
{
Messenger.Register<UserSenderViewModel, CurrentUsernameRequestMessage>(this, (r, m) => m.Reply(r.Username));
}
}
private string username;
public string Username
{
get => username;
private set => SetProperty(ref username, value);
}
// Sends a message to request the current username, and updates the property
public void RequestCurrentUsername()
{
Username = WeakReferenceMessenger.Default.Send<CurrentUsernameRequestMessage>();
}
// Resets the current username
public void ResetCurrentUsername()
{
Username = null;
}
// A sample request message to get the current username
public sealed class CurrentUsernameRequestMessage : RequestMessage<string>
{
}
</controls:InteractiveSample.CSharpCode>
</controls:InteractiveSample>
</StackPanel>
</ScrollViewer>
</Page>