在用于显示内容对话框的 MVVM 应用程序中,我实现了 ISpeechDialogService 来显示内容对话框,我已将其注入(inject)到主页 View 模型中:
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;
}
因此,通过按下主页上的按钮 - 将显示以下内容对话框:
命令:
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(); }
}
内容对话框:
<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>
我为内容对话框(包括消息框)创建了一个单独的 View 模型,以将按钮和属性的命令绑定(bind)到 texbox。
所以目前我有主页与链接主页 View 模型和内容对话框与单独的 View 模型。
我需要做的是将内容对话框文本框中的值传递给 Main Page View Model 属性。
你能告诉我我能做到这一点的方法吗?
最佳答案
我在 VB 中为 UWP 编写了代码。但我不太确定 C#,但我会尝试在这里用 C# 编写它。根据我在 VB 中为 UWP 编写的内容,要从内容对话框传递值,您需要在主按钮单击功能内的内容对话框中添加以下代码:
this.Content = Input.Text
然后在您的mainPage
,你可以用
if (result == ContentDialogResult.Primary)
{
new MessageDialog(_dialog.Content.toString).ShowAsync();
}
关于c# - 内容对话框文本框值检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34711277/