【发布时间】:2018-08-23 21:46:42
【问题描述】:
我正在尝试改编 UWP ContentDialog Invocation 帖子中的示例。 我使用相同的代码,但是当我按下按钮时,只出现一个空框:
另外我的 MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
SpeechDialogService dialog = new SpeechDialogService();
AbcViewModel = new AbcViewModel(dialog);
}
public AbcViewModel AbcViewModel { get; set; }
}
从 MainPage.xaml.cs 中截取一段:
<Page
...
<Grid>
<Button Content="Translate" Click="{x:Bind AbcViewModel.Dictate}" />
</Grid>
</Page>
我做错了什么?感谢您的帮助。
编辑:
我正在使用此服务,它在设计器中看起来不错:
Speech.xaml
<ContentDialog
x:Class="DI_sample.Views.Speech"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DI_sample.Views"
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"
>
<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"/>
<Button Margin="15" Content="Clear Text" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch"/>
<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 Margin="5 5 5 10" AcceptsReturn="True" />
</ScrollViewer>
</Grid>
我也使用answer to the question I've linked above中的代码:
public interface ISpeechDialogService
{
Task ShowAsync();
}
public class SpeechDialogService : ISpeechDialogService
{
public async Task ShowAsync()
{
var contentDialog = new Speech();
await contentDialog.ShowAsync();
}
}
public class AbcViewModel
{
readonly ISpeechDialogService _dialog;
public AbcViewModel(ISpeechDialogService dialog)
{
_dialog = dialog;
}
public async void Dictate(object obj)
{
await _dialog.ShowAsync();
}
}
【问题讨论】:
-
您单独的 ContentDialog XAML 文件在哪里?请在此处发布 xaml 代码。我会帮你检查一下。
-
@XavierXie-MSFT 感谢您的回复,我已添加代码。
标签: c# xaml mvvm dependency-injection uwp