内容对话框文本框值检索
本文关键字:检索 文本 对话框 | 更新日期: 2023-09-27 18:29:40
在我用于显示内容对话框的MVVM应用程序中,我实现了ISpeechDialogService来显示内容对话框,我已将其注入主页视图模型:
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>
我为内容对话框(包括消息框)创建了一个单独的视图模型,将按钮和属性的命令绑定到texbox。
所以目前我有一个链接的主页视图模型和一个单独的视图模型的内容对话框的主页。
我需要做的是将值从内容的对话框文本框传递到主页面视图模型属性。
你能告诉我怎样才能做到这一点吗?
我已经用VB为UWP编写了代码。但我真的不确定C#,但我会尝试用C#在这里写它。根据我在VB中为UWP编写的内容,要从内容对话框传递值,您需要在主按钮单击功能中向内容对话框添加以下代码:
this.Content = Input.Text
然后在你的mainPage
,你可以用检索它
if (result == ContentDialogResult.Primary)
{
new MessageDialog(_dialog.Content.toString).ShowAsync();
}
这是一个古老的话题,但我最终来到了这里,所以这是我微薄的贡献。基本上,ContentDialog的返回类型是ContentDialogResult。您可以在ContentDialog构造函数中添加一个参数,该参数是您希望用户在对话框中修改的对象。请记住,如果使用整数,则需要ref。