Reputation: 192
In my MVVM application for showing content dialog I have implemented ISpeechDialogService to show Content Dialog, which I have injected into Main Page View Model:
public interface ISpeechDialogService
{
Task<ContentDialogResult> ShowAsync();
string GetText();
}
public class SpeechDialogService : ISpeechDialogService
{
private Speech contentDialog;
public async Task<ContentDialogResult> ShowAsync()
{
contentDialog = new Speech();
ContentDialogResult result = await contentDialog.ShowAsync();
return result;
}
So by pressing button on Main Page - following content dialog is being showed:
Command:
public ICommand DictateCommand { get; set; }
public async void Dictate(object obj)
{
var result = await _dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{ new MessageDialog(_dialog.GetText()).ShowAsync(); }
}
Content Dialog:
<ContentDialog
x:Class="UWP1.Views.Speech"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP1.Views"
xmlns:vm="using:UWP1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Dictate"
PrimaryButtonText="Accept"
SecondaryButtonText="Cancel"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick" VerticalAlignment="Center"
x:Name="ContentDialog"
>
<ContentDialog.DataContext>
<Binding Path="SpeechViewModel" Source="{StaticResource ViewModelLocator}" />
</ContentDialog.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Button Margin="15" Content="Dictate" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" Command="{Binding DicateCommand}"/>
<Button Margin="15" Content="Clear Text" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Command="{Binding ClearDicateCommand}"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="Tap 'Dictate', and speak" FontSize="12" />
<TextBlock Margin="0 10 0 0" Grid.Row="2" Grid.ColumnSpan="2" Text="Message Dication" HorizontalAlignment="Center" FontSize="24" />
<ScrollViewer Grid.Row="3" Grid.ColumnSpan="2" Height="300">
<TextBox x:Name="Input" Margin="5 5 5 10" AcceptsReturn="True" Text="{Binding Comment}" TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
</ContentDialog>
I have created a separate view model for content dialog (which comprises message box) to bind commands for button and property to texbox.
So currently I have Main Page with linked Main Page View Model and Content Dialog with a separate view model.
What I need to do is to pass value from content's dialog textbox to Main Page View Model property.
Could you please advise me the way I can achieve that?
Upvotes: 1
Views: 2653
Reputation: 1
It's an old topic, but I ended up here so that's my humble contribution. Basically the return type of the ContentDialog is ContentDialogResult. You can add in the ContentDialog constructor a parameter that is the object you want the user to modify within the dialog. Remember that if you use an integer, ref is needed.
Upvotes: 0
Reputation: 21
I have written code in VB for UWP. But I am not really sure of C#, but I'll try writing it here in C#. Based on what I have written in VB for UWP, to pass the value from the Content Dialog, you need to add the following code to the Content Dialog within the Primary Button Click Function:
this.Content = Input.Text
Then at your mainPage
, you can retrieve it with
if (result == ContentDialogResult.Primary)
{
new MessageDialog(_dialog.Content.toString).ShowAsync();
}
Upvotes: 1